css - How to inject a into the head section of a page with a Chrome extension, using a content script -
css - How to inject a <style> into the head section of a page with a Chrome extension, using a content script -
i'm trying create simple extension, inserts this;
<style> span.watch-view-count:hover {opacity: 1;} span.watch-view-count {opacity: 0;} </style>
right before body on youtube page visit. tried using content script inject code above, first tried putting code in css file called mycsscode.css
, adding manifest.json
file this:
"js": ["script.js"]
but i'm pretty sure nil happened, since viewed source , couldn't find code anywhere.
then tried next first method in reply question changed script.js script.css hoping work, nope didn't i'm stuck.
this codes have far; manifest.json file:
{ "name": "youtube views hider", "version": "1.0", "manifest_version": 2, "description": "a plain text description", "permissions": [ "*://youtube.com/*/", "tabs"], "content_scripts": [{ "matches": ["*://youtube.com/*/"], "js": ["myscript.js"]} ] }
myscript.js:
var s = document.createelement('script'); s.src = chrome.extension.geturl("script.css"); s.onload = function() { this.parentnode.removechild(this); }; (document.head||document.documentelement).appendchild(s);
note: i'm illiterate when comes coding lingo, please set in layman's terms.
if inserting/changing css, don't bother javascript. alter manifest to:
{ "name": "youtube views hider", "version": "1.0", "manifest_version": 2, "description": "a plain text description", "content_scripts": [ { "matches": ["*://*.youtube.com/*"], "css": ["mycss.css"] } ] }
where mycss.css
just:
span.watch-view-count {opacity: 0 !important;} span.watch-view-count:hover {opacity: 1 !important;}
note:
changedmatches
value work on actual youtube url's -- have form: http://www.youtube.com/watch?...
note utilize of !important
keyword. if insist on programmatic injection, see "how inject css using content script file in chrome extension?". ps: if want alter page's or css, the stylish extension fastest easiest way in either chrome or firefox. there thousands of pre-made styles available @ userstyles.org.
css google-chrome-extension content-script
Comments
Post a Comment