Chrome content scripts aren’t working: DOMContentLoaded listener does not execute

You are injecting your script after the event you are listening for fires (in this case, DOMContentLoaded). Thus, any code that you have in the listener will not be executed, because the event never fires after you have added your listener. In Chrome extensions and Firefox WebExtensions, when specifying a time for a content script … Read more

Access variables and functions defined in page context from an extension

Underlying cause: A content script is executed in an ISOLATED “world” environment, meaning it can’t access JS functions and variables in the MAIN “world” (the page context), and can’t expose its own JS stuff, like the state() method in your case. Solution: Inject the code into the JS context of the page (MAIN “world”) using … Read more

Adding complex HTML using a Chrome content script

It’s relatively easy to add whole web pages by having your content script inject them in an iframe. Just follow these guidelines: Place the *.htm or *.html files in your extension’s source folder(s). Place any *.css and *.js files, that the HTML uses, in the extension folder(s) too. Declare the HTML file(s) as resources. EG: … Read more

Injecting multiple scripts through executeScript in Google Chrome

This is my proposed solution: function executeScripts(tabId, injectDetailsArray) { function createCallback(tabId, injectDetails, innerCallback) { return function () { chrome.tabs.executeScript(tabId, injectDetails, innerCallback); }; } var callback = null; for (var i = injectDetailsArray.length – 1; i >= 0; –i) callback = createCallback(tabId, injectDetailsArray[i], callback); if (callback !== null) callback(); // execute outermost function } Subsequently, the … Read more

How to use jquery in google chrome extension page action background.js?

The “background” specification in manifest.json should specify jquery.js so that it is loaded before background.js: … “background”: { “scripts”: [“jquery.js”,”background.js”] }, … This should do the job. Remember the js files are loaded in the order they are specified. test if jquery is loaded. in background.js if (jQuery) { // jQuery loaded } else { … Read more

How to get a content script to load AFTER a page’s Javascript has executed?

“run_at”: “document_end” is the equivalent to DOMContentLoaded. That is, it fires after the static HTML is loaded, but before slow images and slow finishing javascript. So you cannot set a content script to fire after the page’s JS, just by setting the manifest alone. You must code for this in the content script itself. For … Read more

Overriding !important with css or jquery

Here you go: $( ‘.someclass’ ).each(function () { this.style.setProperty( ‘border’, ‘none’, ‘important’ ); }); Live demo: http://jsfiddle.net/Gtr54/ The .setProperty method of an element’s style object enables you to pass a third argument which represents the priority. So, you’re overriding an !important value with your own !important value. As far as I know, it is not … Read more

How to inject CSS using content script file in Chrome extension?

You could add to the manifest’s permissions field; See web_accessible_resources. So you would add this to the manifest: , “web_accessible_resources”: [ “fix.css” ] See also “Programmatic injection”. and insertCSS(). For most applications, forget all that createElement code and just add the CSS file to the manifest: “content_scripts”: [ { “matches”: [“http://www.google.com/*”], “css”: [“fix.css”], “js”: [“script.js”] … Read more