我正在编写一个 chrome 扩展程序,在一些网站中 chrome.tabs.onUpdated.addListener() 被调用了两次。
它主要发生在我点击链接而不是我自己输入 URL 时。
从我在网上找到的内容以及在 StackOverflow 上提出的许多问题来看,chrome 上存在一个错误,但几年前已修复。
有些人声称如果页面中有多个 iframe 就会发生这种情况,但就我而言,我的页面中没有 iframe。
这是我的代码:
var storage = chrome.storage.local;
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete' && tab.status == 'complete' && tab.url != undefined) {
storage.get('URLs', function(URLs){
for (var item in URLs)
{
if (tab.url == item)
{
return;
}
else
{
//alert ("tab load complete");
storage.set({URLs: [tab.url]});
chrome.tabs.executeScript(tab.id, {
"file": "flashblocker.js"
}, function () { // Execute your code
console.log("Script Executed .. "); // Notification …Run Code Online (Sandbox Code Playgroud)