Is it possible to inject a javascript file into the DOM and immediately execute it ?
I wish to invoke javascript functions within the page/DOM. A single content script will not work because of the isolated worlds. A background page is required to use chrome.tabs.executeScript().
Simple Example:
DOM javascript
function sayHello(){
alert('Hello World');
}
Javascript file to inject
console.log('Injection complete. Now calling DOM script.');
sayHello();
Here’s my two favorite ways…
Also
chrome.tabs.executeScript()can be used from a browser/page action popup and the above code works in a content script aswell.EDIT
Thanks to comments by @renocor, here’s a variation of the first method that allows you to send arguments to the injected function….