17

sample extension background.js code

chrome.runtime.onInstalled.addListener(function() {
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    chrome.declarativeContent.onPageChanged.addRules([{
      conditions: [
        // When a page contains a <video> tag...
        new chrome.declarativeContent.PageStateMatcher({
          pageUrl: { hostEquals: 'www.youtube.com'}
        })
      ],
      // ... show the page action.
      actions: [new chrome.declarativeContent.ShowPageAction() ]
    }]);
  });

});

and I got Cannot read property 'onPageChanged' of undefined in console. No problem running the code, why is that happening?

user3522749
  • 309
  • 3
  • 10

2 Answers2

32

Check that you have added the declarativeContent permission to your manifest.json file.
This will give your extension access to the chrome.declarativeContent API and should resolve your problem.

rd3n
  • 4,440
  • 1
  • 33
  • 45
  • 17
    If you're still seeing the error after completing this step, try clicking "Clear All" on the extension Errors page. The error messages persist even after you've corrected them. Try clearing, then reloading the extension. If you don't see the error anymore, you're good. – J.D. Mallen Jan 29 '20 at 13:13
  • 2
    Thank you @J.D.Mallen! I was beginning to thing I was going crazy! – Chris Stahl May 15 '20 at 14:22
  • how to see all permission available? – uncle bob Dec 13 '20 at 16:07
  • Holy cow, nobody else online--SO answers nor blog posts--ever seemed to explicitly spell out that you need to declare permissions for this. The permissions needed are listed at the top of the offiical docs ( https://developer.chrome.com/docs/extensions/reference/declarativeContent/#type-PageStateMatcher ), tucked away in a totally ignorable tiny little header space, but nowhere did I ever read anything akin to "Declare `declarativeContent` permissions in your `manifest.json`". WOW! How exhausting. Now on to getting my code to actually work – velkoon Feb 15 '22 at 22:46
2

If you just added delarativeContent permission to the manifest.json, remember to remove the extension and add it back again. Permissions might be added during installation only.

juan Isaza
  • 3,646
  • 3
  • 31
  • 37