Vik*_*esh 9 google-chrome google-chrome-extension
我正在开发一个Chrome扩展程序,它需要打包XML文件(trie数据结构),并能够从内容脚本中读取该文件.因此,每次加载扩展时,内容脚本将在从XML文件读取数据后实例化trie.
如何通过内容脚本(或后台页面)读取此XML文件?我需要使用localStorage吗?
对XML文件的简单ajax请求应该为您提供XML DOM节点:
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");
}
Run Code Online (Sandbox Code Playgroud)
您需要使用基于js的xml解析器或编写自己的解析器.将数据保存为JSON对象可能更容易.
| 归档时间: |
|
| 查看次数: |
2733 次 |
| 最近记录: |