Chrome Browser Action Popup and icon switch does n

2019-01-20 07:44发布

问题:

While using a default popup for my Chrome extension, I can't change the icon for the state of the extension. If I disable the popup.html the state changing icons are working. I used a toggle method for changing the icons, which worked perfectly before without using the popup.html. How is that possible? Can anyone help me please?

thanks in advance!

回答1:

When you have a popup set, chrome.browserAction.onClicked is not fired.

You will need to message your extension's background page from the popup to inform it about the click, e.g.:

// background script
chrome.runtime.onMessage.addListener( function (message, sender, sendResponse) {
  if (message.clicked) {
    /* Do the usual onClicked stuff */
  }
});

// popup script
chrome.runtime.sendMessage({clicked : true});