【问题标题】:Setting browserAction icon in a concrete Chrome window在具体的 Chrome 窗口中设置 browserAction 图标
【发布时间】:2013-02-12 00:30:04
【问题描述】:

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

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

但它不需要上下文。它会更改每个窗口中的图标。我知道这是有可能的,因为 adBlock 就是这样做的。有人知道怎么做吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2018-08-05
      • 2022-10-23
      • 1970-01-01
      相关资源
      最近更新 更多