Chrome extension to change chrome settings

1.7k Views Asked by At

I am trying to create an extension to set certain settings on chrome such as popups etc.

I asked for contentSettings permission in my manifesto :

 "permissions": [
    "tabs", "http://*/*", "https://*/*","contentSettings"
  ],

and I have the following js code to change the settings

var url = 'http://google.com';

var pattern = /^file:/.test(url) ? url : url.replace(/\/[^\/]*?$/, '/*');
var setting = 'popups';
console.log(' setting for '+pattern+': '+setting);

chrome.contentSettings[setting].set({
        'primaryPattern': pattern,
        'setting': 'allow'
      });

and I get the following error in my console:

Uncaught TypeError: Cannot read property 'popups' of undefined

What am I doing wrong?

2

There are 2 best solutions below

0
On

Most chrome.* APIs are not available to content scripts. They can only be used from background or event pages, popups, or other extension views you define. If you need to initiate your action in response to something that depends on the contents of a page, you can send a message from your content script to a background or event page to carry it out.

7
On

Your script looks OK but you’ve spelled the setting wrong! It should be “pop-ups” not “popups”.

These strings are predefined constants so you gotta stick to what the Chrome API has declared.