I'm in the process of writing a chrome extension which displays some custom UI in multiple web pages via script injection. The script which I am injecting into the page appends some Dom items onto the document's body which can be interacted with by the user. The problem is, the items' styles are being affected by the page's css. I have solved this for the most part by applying css to each item as it's inserted into the dom.
$('item').css({....});
The problem with this is, I end up with huge blocks of styling that look like this:
$('.item').css({
'-webkit-box-sizing' : 'border-box',
'box-sizing' : 'border-box',
'resize' : 'both',
'overflow' : 'hidden',
'position' : 'absolute',
'padding-top' : '.2em',
'padding-right' : '.2em',
'padding-left' : '.2em',
'padding-bottom' : '.4em',
'opacity' : '.8',
'background' : 'rgba(0,0,0,.5)',
'background-image' : '-webkit-gradient(linear, 0 0, 0 100%, from(#444444), to(#111111))',
'background-image' : '-webkit-linear-gradient(#444444, #111111)',
'background-image' : 'linear-gradient(#444444, #111111)',
'border-radius' : '4px',
'box-shadow' : '3px 2px 4px 1px #111111',
});
this.$el.children(".default").css({
'font-size':'medium',
'-webkit-box-sizing' : 'border-box',
'box-sizing' : 'border-box',
'resize' : 'none',
'overflow' : 'auto',
'display' : 'inherit',
'position' : 'relative',
'padding' : '.25em',
'width' : '100%',
'height' : '90%',
'background' : '#efefef',
'border-radius' : '2px',
});
This whole method is going to become a problem because:
- First: any property which I don't apply here is applied to my items by the page's css, this changes the style and usability of my elements.
- Second: I feel like this method is inefficient.
- Third: My content script contains jquery and some others which i'm afraid will also interfere with the page and it's own version of these
Is there a better way to display these dom elements, inject these scripts, and style them? I would like as little clash with the parent page as possible. An idea that I came up with was covering the entire page in a transparent iframe and injecting everything into that, but I don't believe the items would display since the iframe would be transparent.