Confine DOM injected content from any existing web page styles


Problem statement: 
Extensions generally add some UI components to existing web pages. Those components which you create thru contentScripts get influenced by the webpage css styles. Their appearance is therefore not guaranteed and sometimes compromised. This becomes an important problem:
-  if you inject complex UI (sidebar/toolbar based addons for example)
-  if you target many websites (broad permissions)
-  if page structure and styles vary frequently for the sites you target.

Solutions
- solution 0: I have seen people setting every possible attribute for all their addon UI elements in gigantic stylesheets to prevent any potential influence.
Pros: simple
Cons: tedious and cumbersome.

- solution 1: inject an iframe (portable across all browsers)
Pros: isolate also from CSP perspective. JS UI libraries can be used without side-effect.
Cons: complexify the architecture of the addon since it introduces a new agent to handle the iframe. Communication between the various parts can get very quickly messy and difficult to follow/maintain.

- solution 2: use shadow DOM to store UI elements see here for example. Main intent of shadow DOM is to provide scoped styling.

Pros: simple to implement. With React you can use ShadowDom plugin to even simplify it farther.
Cons: not all plugins libraries are compatible with shadow DOM. Some Jquery-ui plugins for instance create other elements  outside of the shadowed scope. Other do not handle well focus events (like chosen-js).
Third disadvantage is that shadowed DOM elements are still constrained by the webpage CSPs and you may have to disable CSPs at addon level to get them to work properly (see this extension on how to disable CSPs).

Comments

Popular posts from this blog

Extending an extension with a native app.

Intercepting file download in chrome

Filtering console messages in the chrome devtools window