Eamonn
Eamonn

Reputation: 187

Firefox extenion: Inject javascript into page before standard page javascript is loaded

I need to inject javascript into specific webpages that can be accessible by the webpages existing javascript. I also need the injected javascript to be the first that's loaded.

Currently I am able to inject javascript into the page however, it is not loaded until after the page has loaded. Below is my current solution:

The firefox extension javascript - Note: page.js is the intended script to be injected

function attach(worker) {
    worker.port.emit('init', data.url('./page.js'));
    // ...
}

pageMod.PageMod({
    include: ['*'],
    contentScriptFile: './content.js',
    onAttach: attach
});

content.js

self.port.on('init', function(url) {
    var pageScript = document.createElement('script');
    pageScript.type = 'text/javascript';
    pageScript.src = url;
    window.document.body.appendChild(pageScript);
});

Upvotes: 1

Views: 375

Answers (1)

Noitidart
Noitidart

Reputation: 37238

You can add event listener for the beforescriptexecute - https://developer.mozilla.org/en-US/docs/Web/Events/beforescriptexecute

Upvotes: 1

Related Questions