小编use*_*678的帖子

如何根据页面网址更改扩展程序图标并在不同图标中以不同方式发出警报

我正在构建一个 chrome 扩展,如果 url 与某些特定模式不匹配,我需要显示一些指示禁用的图标,并在用户单击禁用图标并且不显示弹出窗口时发出警报。图标是chrome浏览器的操作图标。但是,如果用户单击指示已启用的图标,则应显示默认弹出窗口。
基本上,当页面 url 不匹配时,弹出窗口不应打开,并应发出警报。
我目前在后台页面中使用它,但它看起来效率很低,大多数时候它可以工作,但仅在页面重新加载后显示警报:

background.js

    var alertError = function(arg){
                    alert('Something');
                };

chrome.tabs.onActivated.addListener(function(info){
    chrome.tabs.get(info.tabId, function(change){
        if(change.url == undefined){
            chrome.browserAction.setPopup({tabId: info.tabId, popup: ''});
            chrome.browserAction.setIcon({path: '../icons/icon-disabled.png', tabId: info.tabId});
            chrome.browserAction.onClicked.removeListener(alertError);
            chrome.browserAction.onClicked.addListener(alertError);
            console.log('undefined');
        }
        else if(change.url.match(/https:\/\/google\.com\/*/) == null){
            chrome.browserAction.setPopup({tabId: info.tabId, popup: ''});
            chrome.browserAction.setIcon({path: '../icons/icon-disabled.png', tabId: info.tabId});
            chrome.browserAction.onClicked.removeListener(alertError);
            chrome.browserAction.onClicked.addListener(alertError);
            console.log('not matching');
        }
        else{
            chrome.browserAction.setPopup({tabId: info.tabId, popup: '../html/popup.html'});
            chrome.browserAction.setIcon({path: '../icons/icon.png', tabId: info.tabId});
            console.log('matched');
        }
    });
});
chrome.tabs.onUpdated.addListener(function (tabId, change, tab){
    if(change.url == undefined){
        return;
    }
    else if(/https:\/\/google\.com\/*/) == null){
        chrome.browserAction.setPopup({tabId: tabId, popup: …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome-extension

6
推荐指数
1
解决办法
4862
查看次数