Javascript 防止由于页面iFrame而多次加载内容脚本
我的扩展名的Javascript 防止由于页面iFrame而多次加载内容脚本,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我的扩展名的contentscript正在包含多个iframe的页面中加载多次。有没有办法防止这种情况 我尝试过做这篇相关文章的建议:但它仍然加载了多次 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { if (tab.status === 'complete' && changeInfo.status === "complete") { chrome.browserA
contentscript
正在包含多个iframe的页面中加载多次。有没有办法防止这种情况
我尝试过做这篇相关文章的建议:但它仍然加载了多次
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (tab.status === 'complete' && changeInfo.status === "complete") {
chrome.browserAction.getBadgeText({}, function(result) {
if (result === "ON") {
setTimeout(function() {
chrome.tabs.executeScript(tab.id, { file: "js/contentscript.js", allFrames: true, runAt: "document_idle" }, result => {
// Ignores the permission issue when trying to inject the
// content script to local file of chrome extension.
const lastErr = chrome.runtime.lastError;
if (lastErr) return;
});
}, 100);
}
});
} else {
chrome.tabs.executeScript(tab.id, { code: "(c = document.querySelectorAll('#button')) ? c.forEach(panel => { panel.parentNode.removeChild(panel); }) : null;" },
result => {
// Ignores the permission issue when trying to inject the
// content script to local file of chrome extension.
const lastErr = chrome.runtime.lastError;
if (lastErr) return;
});
}
});
删除它,默认情况下为false您指定的是
allFrames:true
,因此每个帧都会获得内容脚本的一个实例-这就是内容脚本的工作方式。我也尝试过更改它,但问题仍然存在编辑背景脚本时需要重新加载扩展。无论如何,没有看到分机,我只能猜测。也许您可以在manifest.json中声明内容_脚本,executeScript不需要它。