Jacques Blom
Jacques Blom

Reputation: 1822

Execute js to change CSS in background - Chrome Extensions

I am designing a Chrome plugin. I have a script that injects CSS into a web page. It is linked to the popup.html for now, because it only works like that. 

The problem is, I have to click on the icon to open the popup before the css is injected. Because that loads the javascript file to inject the CSS. I don't want to have to do that. 

Oh, content scripts aren't an option, because they don't support the chrome.tabs API.

What I want to do: inject the CSS without ANY user interaction. Because to inject the CSS I have to click on the icon to open the popup to load the js to load the css.

How can I load that javascript file in the background to inject the css without ANY user interaction? 

Upvotes: 0

Views: 2384

Answers (1)

Rob W
Rob W

Reputation: 349132

As I have answered earlier, you can inject CSS via a Content script definition in manifest.json. Your content script can communicate with the background page using chrome.extension.sendRequest (in the content script) and chrome.extension.onRequest (at the background page).

So, content scripts are the way to solve your problem.

Examples:

Upvotes: 1

Related Questions