相关疑难解决方法(0)

chrome 扩展中的后台脚本究竟何时运行?

在我的 chrome 扩展中,我有一个后台脚本,它将使用XMLHttpRequest.

// note that this code is in the global scope i.e. outside of any function
// also note that I got this code from the page talking about XMLHttpRequest
var myData = [];

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("GET", "...", true);
xhr.send();

function handleStateChange() {
  myData = Object.values(JSON.parse(xhr.responseText));
}
Run Code Online (Sandbox Code Playgroud)

我想知道什么时候xor.send()运行。

我观察到每次我通过按 在此处输入图片说明按钮,xhr.send()将被调用。我还观察到打开新选项卡/窗口不会导致后台脚本再次运行。

我还发现这个页面,后台页面被“加载”和“卸载”,但它几乎没有说明后台脚本全局范围内的代码何时运行。

它是否仅在安装/重新加载扩展时运行?

javascript google-chrome-extension

6
推荐指数
1
解决办法
1132
查看次数