Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isolating CSS for Chrome extension

I'm building a Chrome extension that does some UI injection using content scripts. The problem is that since every website is different and may try to screw around with the default positioning of certain elements (divs, lists) etc, my ui looks different depending on which page it is being used.

I've tried using YUI reset v3 and that helped but didn't remove all the weirdness. Does anybody know of an even more aggressive reset method that does more than just clearing margin/padding and reset text sizes?

Thanks in advance.

like image 393
Rui Jiang Avatar asked Feb 28 '11 17:02

Rui Jiang


People also ask

What is isolation CSS?

The isolation property in CSS is used to define whether an element must create a new stacking content. It prevents an element from acquiring mix-blend-mode from elements in the backdrop by creating a different stack element. Note: Isolate property is applied to the elements parent.

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.


1 Answers

We've had a similar issue, we've tried CSS resets and also using specific id tags for the elements and CSS rules, but it was never robust enough...

The best solution was to inject the elements into the DOM as Shadow DOM elements that contain the style inline. You can read your CSS file via AJAX requests and inject them to the Shadow DOM dynamically, just make sure that they are within the web_accessible_resources files (you can use a wildcard to your CSS folder).

In case that you are not familiar with Shadow DOM, here is a good example of how it works. It might take a bit of re-factoring on your end, but it's really the only solution that works a 100%.

like image 96
sagibb Avatar answered Nov 09 '22 23:11

sagibb