0

I am still new to Chrome extension development, and I am currently playing a bit with it. I have now come across an error that I cannot explain. My guess is that some permission is missing. But according to the API specs, I have set all the necessary permissions. My test extension should change the value of chrome.contentSettings['javascript'].set. Actually, I want to disable JavaScript via this extension.

manifest.json

{
  "manifest_version": 3,
  "name": "Block JavaScript",
  "description": "BlockJavascript",
  "version": "0.0.1",
  "icons": {
    "16": "images/shield_green.png",
    "48": "images/shield_green.png",
    "128": "images/shield_green.png"
  },
  "action": {
    "default_icon": {
      "16": "images/shield_green.png",
      "24": "images/shield_green.png",
      "32": "images/shield_green.png"
    },
    "default_popup": "popup.html",
    "default_title": "Block JavaScript"
  },
  "permissions": ["tabs", "contentSettings"],
  "options_page": "options.html",
  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "all_frames": true,
      "run_at": "document_start",
      "js": ["inject.js"]
    }
  ]
}

inject.js

chrome.contentSettings['javascript'].set({
  primaryPattern: '<all_urls>',
  setting: 'block'
});

Chrome Browser Error Log

Uncaught TypeError: Cannot read properties of undefined (reading 'javascript')

Stack Trace inject.js:1 (anonymous function)

chrome.contentSettings['javascript'].set({ //Error in this line
  primaryPattern: '<all_urls>',
  setting: 'block',
});

I hope that someone can help me and explain to me why this is happening. If you have any useful learning materials (besides the Chrome dev docs), I would be pleased if you could provide me with some. I generally find it very difficult to find learning material/videos explicitly for Chrome extension (manifest v3) development.

Shiro
  • 11
  • 4
  • Your `permissions` is inside `action` but it shouldn't be. – wOxxOm Mar 02 '22 at 15:38
  • Oh, OK. Thank you. Didn't see that. I moved the permissions in the right place, but I got the same error (edited the post). – Shiro Mar 02 '22 at 15:45
  • OK, I figured out the problem. Content scripts cannot perform tasks like interacting with Chrome APIs' ContentSettings. They only can use i18n, storage and runtime (can be found in the APIs' docs [Chrome Extension - Content scripts](https://developer.chrome.com/docs/extensions/mv3/content_scripts/)). I solved it by sending a `runtime.sendMessage` to my backend script on page_start. My backend handle than the disabling of the JavaScript for the current tab. – Shiro Mar 02 '22 at 19:48

0 Answers0