Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 976
malyutki Avatar asked Nov 15 '17 14:11

malyutki


People also ask

How do I change the icon for a Chrome extension?

To the right of your address bar, find your extensions' icons. On your computer, open Chrome. Drag the extension's icon to its new place.

What is packing extension in Chrome?

Packed are extensions that have been compiled into a downloadable . crx file. These are the files you download and install from such places like the Chrome Web Store. Unpacked are extensions that are available from your computer.


2 Answers

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"});
        }
    }
});
like image 93
Troy Wray Avatar answered Oct 12 '22 19:10

Troy Wray


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

like image 1
Gabriel Bleu Avatar answered Oct 12 '22 19:10

Gabriel Bleu