Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scope CSS in chrome extension content scripts

I am developing a chrome extension that injects a button onto a page. When this button is clicked, it launches a twitter bootstrap modal. However, the css that is injected with the twitter bootstrap is affecting the pages themselves and I only want them to affect the modal divs. How can I achieve this?

Thank you!

like image 938
d9120 Avatar asked Mar 24 '14 20:03

d9120


People also ask

How do I add CSS extensions to chrome?

Once the manifest, CSS and JavaScript files are ready, head over to chrome://extensions/ from the browser's address bar and enable developer mode. That activates the “Load unpacked” button to add the extension files. It's also possible to toggle whether or not the developer version of the extension is active.

What is content script Chrome extension?

A content script is a part of your extension that runs in the context of a particular web page (as opposed to background scripts which are part of the extension, or scripts which are part of the website itself, such as those loaded using the <script> element).

Can Chrome extensions edit HTML?

Page Edit is an extension that let you make changes to any HTML webpage. To work with this add-on, simply open the toolbar popup UI and then click on the big toggle button at the left side.


1 Answers

There is only one true way to do that and it involves LESS

See this answer: https://stackoverflow.com/a/14145510/1060487

Or this one: https://stackoverflow.com/a/15115171/1060487

Both approaches should work fine.

I've experimented with 1 other solution:

http://css-tricks.com/saving-the-day-with-scoped-css/

Which DOES NOT work in Chrome but there is a polyfill here, which doesn't really seem to work that well...

Check the support here: http://caniuse.com/style-scoped

Summary: the LESS route seems to be the best solution right now...

like image 128
mattdlockyer Avatar answered Oct 30 '22 00:10

mattdlockyer