在具体的Chrome窗口中设置browserAction图标

时间:2013-02-12 00:30:05

标签: windows google-chrome google-chrome-extension icons browser-action

我想在不同的窗口中设置不同的browserAction图标。我找到的唯一方法是:

http://developer.chrome.com/extensions/browserAction.html#method-setIcon

但它没有上下文。它会在每个窗口中更改图标。我知道这在某种程度上是可能的,因为adBlock会这样做。有谁知道怎么做?

1 个答案:

答案 0 :(得分:3)

这是我的解决方案,我希望它会有所帮助:

function setIcon(state, getIconNameCallback) {

    // we need to set the icon globally first, 
    // to avoid blinking to default icon

    chrome.windows.getLastFocused(null, function(window) {
        chrome.tabs.getSelected(null, function(tab) {
            chrome.browserAction.setIcon({
                'path': getIconNameCallback(state, tab.url)
            });
        });
    });

    // then we must set the icon for each tab,
    // without that the extension wont behave
    // properly with multiple windows

    chrome.tabs.query({}, function(tabs) {
        for (var i = 0; i < tabs.length; i++) {
            chrome.browserAction.setIcon({
                'path': getIconNameCallback(state, tabs[i].url), 
                'tabId': tabs[i].id
            });
        }
    });

}
相关问题