我正在努力改善Chrome扩展程序的功能。我的目标是从浏览器启动时全局保存xhr GET响应,然后在必要时将其传递到发布请求(主机和url规范的一部分)。我怎样才能做到这一点?我尝试将chrome.runtime.onStartup.addListener与get响应一起使用,但无法正常工作。在我的代码下面(我是一个真正的JS新手)
function onCommit(info) {
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:5000/background',true);
xhr.send(info.url);
xhr.onload = () => {alert(xhr.responseText)};
};
chrome.webNavigation.onCommitted.addListener(onCommit, {
url: [
{hostEquals: window.resp}, //<- I want to pass xhr2.response text here
{urlPrefix: window.resp} //<- and here
],
});
var xhr2 = new XMLHttpRequest();
xhr2.open("GET", "http://localhost:5000/get", true);
xhr2.onreadystatechange = function() {
if (xhr2.readyState == 4) {
var resp= xhr2.responseText;
if (resp.StartsWith("http://")){
console.log(resp)
}
}
xhr2.send()
};