The chrome extension I developed injects Polymer and other web components into the host page via HTML imports as opposed to the typical method which is to use content scripts - which automatically run in an isolated world.
The reason behind this has to do with two things:
- Chrome extensions cannot register custom elements from content scripts
- The chrome extension manifest does not support HTML imports running in an isolated environment, only javascript
Due to these limitations, I had to resort to loading my components into the host page's <head>
as described here.
The obvious problem I am facing is having my javascript come in conflict with the host page's javascript [on certain sites] since the method I am using to inject my dependencies does not run in an "isolated world".
So far I have resolved most of these issues by having a gulp task rename Polymer & my components to avoid conflicts but, unfortunately it is not perfect and a more robust approach is needed.
Finally to my question: Is it possible to create an "isolated world" for my javascript? Perhaps another document object? If not, are there any strategies I can employ to ensure my code run in isolation?
Update:
A few of you suggested using IIFE which is what I have been using thus far. I am looking for an answer along the lines of an isolated world, similar to how google chrome runs extensions under. This is mainly because Polymer must be attached to the global window object.