Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable "content_scripts.css" in Chrome Extension

For example:

  ...
  "content_scripts": [
    {
      "matches": ["*://*/*"],
      "css": ["style.css"]
    }
  ]
  ...

I would like to inject this stylesheet by default. Which is business as usual.

I would also like to enable/disable, when the user when the user clicks on the icon in the toolbar.

Any pointer would be helpful. Thanks!

like image 931
Sarbbottam Avatar asked Jun 29 '26 15:06

Sarbbottam


1 Answers

I had to implement this in one of my extensions as well. I used a checkbox in my popup.html to manipulate chrome.storage, which would store whether the state was enabled or disabled.

The content script was always running, but would check chrome.storage first before performing any actions. Feel free to refer to my repository here, particularly inject.js.

This isn't the only way to do it, but it was pretty simple for me to implement. Note that you can't use localstorage because the content script exists in a separate environment.

like image 114
Noam Hacker Avatar answered Jul 02 '26 04:07

Noam Hacker