Search code examples
google-chrome-extension

chrome extension dynamically change icon (without clicking)


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.


Solution

  • Updated Answer: For Manifest V3

    Use chrome.action.setIcon({ path: "/example/path/image.png" }).

    Source

    Original Answer: For Manifest V2 and Under

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