gpt4 book ai didi

google-chrome - 在内容脚本中读取与Chrome扩展程序打包在一起的文件

转载 作者:行者123 更新时间:2023-12-04 04:18:55 26 4
gpt4 key购买 nike

我正在开发一个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");
}

您将需要使用基于js的xml解析器或编写自己的解析器。将数据另存为JSON对象可能会更容易。

关于google-chrome - 在内容脚本中读取与Chrome扩展程序打包在一起的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8550383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com