从浏览器开始就使XMLHttpRequest GET响应可用,并在浏览器关闭之前全局保存

时间:2019-01-16 07:11:13

标签: javascript google-chrome-extension xmlhttprequest

我正在努力改善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()
};

0 个答案:

没有答案
相关问题