How do I dynamically change a manifest file file for a Chrome extension, or how to enable or disable a stylesheet correctly?

In response to my Other question, the user successfully made me serve as a stylesheet, but this is the wrong way; this is not what i needed to do.

So, I had a more precise question:

Basically, you can make the extension permanently modify the web page by specifying the script content in manifest.json.

I need the ability, by clicking a button in popup.html, to enable or disable the stylesheet.

The answer to my other question I'm connected to only allows me to serve the stylesheet, but it is deleted again as soon as the page reloads.

I want to basically set the extension option to change the stylesheet for the web page or disable it by clicking the switch button located in the browser icon popup.html.

See screenshot:

popup screenshot



I think I need the button to change the setting in the extension , which will either enable or disable the css contents of the script stylesheet , or swap with other contents of the CSS script table .

Hope I made it clear enough to understand.

0
source share
1 answer

Why do you want to reinvent the wheel?

See Stylish for Chrome , UserScript Manager (from Stylish for Firefox).

If you still want to continue developing such an extension, the following methods can be used in addition to the one I described in the previous answer:

  • chrome.tabs.onUpdated - Detect tab state changes (put this code in the man page ):

     chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { chrome.tabs.executeScript(tab.id, {code: 'document.documentElement.setAttribute("RWchooseStyle", "style1.css");' }); // myscript.js defined in https://stackoverflow.com/a/9647059 chrome.tabs.executeScript(tab.id, {file: 'myscript.js'}); }); 
  • localStorage - for saving (so preferences can be "remembered" in your extension):
    localStorage.getItem('name') for preferences, localStorage.setItem('name','value') for setting preferences.

    You can only remember strings, so string = JSON.stringify(some_variable) and original_variable = JSON.parse(string) can be used to store numbers, arrays, and other primitives.

+1
source

Source: https://habr.com/ru/post/912096/


All Articles