2

I'm trying to reproduce this example with the manifest v3. But my action is always active - I expext it to be disabled on all pages without 'g' in a URL.

Thanks in advance!

manifest.json

{
   "name":"Example",
   "description":"description",
   "version":"0.1",
   "manifest_version":3,
   "background":{
      "service_worker":"background.js"
   },
   "permissions":[
      "declarativeContent"
   ],
   "action":{
      "default_icon":{
         "16":"/images/get_started16.png",
         "32":"/images/get_started32.png",
         "48":"/images/get_started48.png",
         "128":"/images/get_started128.png"
      },
      "default_title":"press here to open"
   },
   "icons":{
      "16":"/images/get_started16.png",
      "32":"/images/get_started32.png",
      "48":"/images/get_started48.png",
      "128":"/images/get_started128.png"
   }
}

background.js

chrome.runtime.onInstalled.addListener(() => {
  // 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: 'g' },
          })
        ],
        // And shows the extension's page action.
        actions: [ new chrome.declarativeContent.ShowPageAction() ]
      }
    ], function callback(details) {
      console.log("chrome.declarativeContent.onPageChanged.addRules callback");
      console.log(details);
    });
  });
});
user2809176
  • 1,042
  • 12
  • 29
  • 1
    See [How to disable (gray out) page action for Chrome extension?](https://stackoverflow.com/a/64475504) – wOxxOm Jun 05 '21 at 14:42
  • @wOxxOm that link refers to manifest v2, the question asks for manifest v3. – user2677034 Aug 20 '21 at 04:59
  • I've updated the answer. The API almost didn't change, just a couple of things were renamed. – wOxxOm Aug 20 '21 at 06:31
  • For further info see: https://developer.chrome.com/docs/extensions/reference/action/ - Emulating pageActions with declarativeContent this specifically references manifest V3. – user2677034 Aug 20 '21 at 17:27
  • As wOxxOm mentioned in his comment: [this answer helped](https://stackoverflow.com/a/64475504). – user2809176 Sep 14 '22 at 11:28

1 Answers1

0

as wOxxOm mentioned this answer helped.

user2809176
  • 1,042
  • 12
  • 29