防止内容脚本由于页面iframe而多次加载

时间:2019-09-20 03:17:49

标签: javascript google-chrome-extension

我的扩展程序的contentscript正在包含多个iframe的页面中多次加载。有办法防止这种情况吗?

我已经尝试过做有关该帖子的建议:chrome extension script is loading twice even more on some pages,但它仍然加载多次。

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;
            });
    }
});

1 个答案:

答案 0 :(得分:1)

enter image description here

删除,这是默认假