How to enable a pageAction icon for a chrome extension in incognito mode?

 

Questions


Even after selecting the ‘Allow in incognite mode’ my extension which uses pageaction to render in certain urls doesn’t show up in incognito mode. background.js has the following.

chrome.runtime.onInstalled.addListener(function() {
  // Replace all rules ...
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    // With a new rule ...
    chrome.declarativeContent.onPageChanged.addRules([
      {
        // That fires when a page's URL contains a 'g' ...
        conditions: [
          new chrome.declarativeContent.PageStateMatcher({
            pageUrl: { urlContains: 'sears' },
          })
        ],
        // And shows the extension's page action.
        actions: [ new chrome.declarativeContent.ShowPageAction() ]
      }
    ]);
  });
});

 

 

————————————————-

Answer

Looks like a bug, so I’ve reported it here: crbug.com/408326

As a work-around, you could enable split incognito mode by adding the following to the manifest file:

"incognito": "split"

Unfortunately, chrome.runtime.onInstalled is not fired for extensions in incognito mode, so you should avoid using this event when the extension is running in incognito mode, as follows:

if (chrome.extension.inIncognitoContext) {
    doReplaceRules();
} else {
    chrome.runtime.onInstalled.addListener(doReplaceRules);
}
function doReplaceRules() {
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    // ... add rules
  });
}

google-chrome,google-chrome-extension,javascript

Facebook Comments

Post a comment