chrome extension dynamically change icon (without clicking)

我们两清 提交于 2019-12-21 17:20:05

问题


how can I make my chrome extension change icon (without clicking on it). I've got script that's checking if page has certain string and if it has I want my extension icon change from grey to colored one.


回答1:


The content script will need to send a message when it wants to set the icon e.g.

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

Then in the background script:

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:


In background you can do stuff like :

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

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



来源:https://stackoverflow.com/questions/47310292/chrome-extension-dynamically-change-icon-without-clicking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!