在内容脚本中阅读Chrome扩展程序打包的文件 [英] Read file packaged with Chrome extension in content script
问题描述
我正在开发需要打包XML文件(trie数据结构)并能够从内容脚本中读取该文件的Chrome扩展程序。因此,每次加载扩展后,内容脚本都会在从XML文件读取数据后实例化trie。
如何通过内容脚本(或背景页面)读取此XML文件?我需要使用localStorage吗?
对XML文件的简单ajax请求应该为您提供XML DOM节点:$ b
$ b
函数请求(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);
函数init(){
request(sample.xml);
}
您需要使用基于js的xml解析器或编写自己的解析器。将数据保存为JSON对象可能更容易。
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?
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.
这篇关于在内容脚本中阅读Chrome扩展程序打包的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!