Google chrome extension chrome扩展动态更改图标(无需单击)

Google chrome extension chrome扩展动态更改图标(无需单击),google-chrome-extension,Google Chrome Extension,如何更改我的chrome扩展图标(无需单击)。我有一个脚本,它检查页面是否有特定的字符串,如果有,我希望我的扩展图标从灰色变为彩色 在后台,您可以执行以下操作: const updateIcon = tabId => { const icon = isDisabled() ? icons.disabled : icons.enabled; chrome.pageAction.setIcon({ tabId, path: icon }); }; chrome.tabs.onUpdat

如何更改我的chrome扩展图标(无需单击)。我有一个脚本,它检查页面是否有特定的字符串,如果有,我希望我的扩展图标从灰色变为彩色

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

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

ref:

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

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