Read file packaged with Chrome extension in content script

人走茶凉 提交于 2019-12-19 16:58:51

问题


I am developing a Chrome extension that needs to package an XML file (a trie data structure) and be able to read that file from the content script. So the content script will instantiate the trie after reading the data from the XML file every time the extension is loaded.

How to read this XML file through content script (or background page)? Do I need to use localStorage?


回答1:


A simple ajax request to the XML file should give you the XML DOM nodes:

function request(url) {
    var xhr = new XMLHttpRequest();
    try {
        xhr.onreadystatechange = function(){
            if (xhr.readyState != 4)
                return;

            if (xhr.responseXML) {
                console.debug(xhr.responseXML);
            }
        }

        xhr.onerror = function(error) {
            console.debug(error);
        }

        xhr.open("GET", url, true);
        xhr.send(null);
    } catch(e) {
        console.error(e);
    }
}

function init() {
    request("sample.xml");
}

You would need to use a js-based xml parser or write your own. It might be easier to save your data as a JSON object.



来源:https://stackoverflow.com/questions/8550383/read-file-packaged-with-chrome-extension-in-content-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!