Change the Chrome extension icon

Unchecked runtime.lastError: Icon invalid.

Just in case that any one encounter the above error when change browserAction icon, I need to clarify: not all sizes of image is supported, but 48x48 is a valid one.


Content scripts don't have access to most extension APIs. Instead, you'll need to use message passing to have the content script alert notify the background page of what works needs to be done.

Your content script should send a message using chrome.runtime.sendMessage, and the background page should listen using chrome.runtime.onMessage.addListener:

Content script:

if(shouldChangeIcon) {
    // send message to background script
    chrome.runtime.sendMessage({ "newIconPath" : folder + icons[2] });
}

Background page:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        // read `newIconPath` from request and read `tab.id` from sender
        chrome.browserAction.setIcon({
            path: request.newIconPath,
            tabId: sender.tab.id
        });
    });