I'm trying to get my Chrome Extension to inject some javascript with content_scripts
, using this previous answer as a reference.
"name": "My Chrome Extension", "version": "1.0", "manifest_version": 2, "content_scripts": [{ "matches": ["http://pagetoinject/script/into/*"], "js": ["contentscript.js"] }]
var s = document.createElement('script'); s.src = chrome.extension.getURL("script.js"); (document.head||document.documentElement).appendChild(s); s.parentNode.removeChild(s);
( also tried this method with no success. )
var s = document.createElement('script'); s.src = chrome.extension.getURL("script.js"); s.onload = function() { this.parentNode.removeChild(this); }; (document.head||document.documentElement).appendChild(s);
I keep getting this javascript error. Here's a screenshot.
GET chrome-extension://invalid/ (anonymous function)
Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.
Open your Chrome developer tools by pressing F12, then identify the element with the pop-up. In this example, the iframe element with ID wallIframe contains the pop-up with some fading background in the back. Now, we'll be using a small JavaScript snippet to add custom CSS and check if we can hide the pop-up.
"manifest_version": 2
is specified. This automatically activates a stricter mode, in which all extension's files are not available to web pages by default.<script>
element is immediately removed after injection (the script file does not have a chance to load).As a result of 1., the following error shows up in the console:
Failed to load resource chrome-extension://invalid/
To fix the problem, add script.js
to the whitelist, "web_accessible_resources"
in your manifest file
:
{ "name": "Chrome Extension", "version": "1.0", "manifest_version": 2, "content_scripts": [{ "matches": ["http://pagetoinject/script/into/*"], "js": ["contentscript.js"] }], "web_accessible_resources": ["script.js"] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With