加载页面时运行js



我正在尝试使谷歌chrome扩展,我需要JavaScript在加载的每个页面上运行。

我试着到处寻找答案,但我到处都找不到。一切都过时了。

这是我当前的代码:

function reddenPage() {
alert("yay!")
}

chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: reddenPage
});
}
);

使用chrome.tabs.onUpdated.addListener,然后检查TabStatus

function reddenPage() {
alert("yay!")
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
// checking status
if (changeInfo.status === 'complete') {
chrome.scripting.executeScript({
target: { tabId },
func: reddenPage // should be "func" instead of "function"
});
}
});

注意,你不需要{ tabId: tabId },因为{ tabId }是相同的。它被称为"简写属性名"。

最新更新