【问题标题】:chrome extension dynamically change icon (without clicking)chrome扩展动态更改图标(无需点击)
【发布时间】:2017-11-15 23:26:35
【问题描述】:

我怎样才能使我的 chrome 扩展更改图标(不点击它)。我有脚本检查页面是否有特定的字符串,如果有,我希望我的扩展图标从灰色变为彩色。

【问题讨论】:

    标签: google-chrome-extension


    【解决方案1】:

    内容脚本在设置图标时需要发送消息,例如

    chrome.runtime.sendMessage({
        action: 'updateIcon',
        value: false
    });
    

    然后在后台脚本中:

    chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
        if (msg.action === "updateIcon") {
            if (msg.value) {
                chrome.browserAction.setIcon({path: "/assets/tick.png"});
            } else {
                chrome.browserAction.setIcon({path: "/assets/cross.png"});
            }
        }
    });
    

    【讨论】:

      【解决方案2】:

      您可以在后台执行以下操作:

      const updateIcon = tabId => {
        const icon = isDisabled() ? icons.disabled : icons.enabled;
        chrome.pageAction.setIcon({ tabId, path: icon });
      };
      chrome.tabs.onUpdated.addListener(updateIcon);
      

      参考:https://github.com/gbleu/opteamissed/blob/master/background.js#L38

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-31
        • 1970-01-01
        • 2015-01-01
        • 1970-01-01
        • 2016-11-05
        • 2021-07-09
        相关资源
        最近更新 更多