我有一个在我的 background.js
中启动的内容脚本:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
var url = (new String(tab.url)).valueOf();
if(url.indexOf("chrome") == 0 || url == undefined || url == 'undefined')
return;
if(changeInfo.status == 'complete') {
chrome.tabs.executeScript(tabId,
{file: "js/control.js", runAt:"document_end"},
function() { console.log("Control sent to page");
});
console.log("ChangeInfo " + changeInfo.status);
});
我需要在页面更改或窗口关闭之前执行一个函数。我环顾四周,不知道该怎么做。我能找到的最接近的东西是:
chrome.runtime.onSuspend.addListener(function () {
someObjectDefinedInScript.afunction("stopping");
});
因为我让 chrome.runtime.sendMessage
在内容脚本中工作。
但这会返回一个错误:
Uncaught TypeError: Cannot call method 'addListener' of undefined
最佳答案
好的旧window.onunload
将在内容脚本中工作。
关于javascript - 选项卡关闭或页面更改之前的 Chrome 扩展内容脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21652539/