2

I'm currently using this in the bootstrap.js file. Is used to edit the client's JavaScript code in my case I'm using recentBrowserWindow.messageManager.loadFrameScript(file, true):

const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import('resource://gre/modules/Services.jsm');

var file = 'chrome://testAddon/content/inject.js';

function startup(aData, aReason) {
    var recentBrowserWindow = Services.wm.getMostRecentWindow('navigator:browser');
    if(recentBrowserWindow.document.readyState == 'complete') {
        recentBrowserWindow.messageManager.loadFrameScript(file, true);
    }
}

inject.js:

var window = content;
var pageJS = window.wrappedJSObject;
console.log("jQuery is defined:", "jQuery" in window);
//true, jQuery is already assigned on client page.

But here the script inject.js is running after of the client page. The question is:

How can inject inject.js just before the page's script execution?. What method should I use?

2 Answers2

2

From: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFrameScriptLoader

It says to use document-element-inserted, this will ensure your script runs before the page scripts run.

I don't have much experience with frame scripts, others will have to verify me on this.

yatg
  • 1,121
  • 1
  • 9
  • 15
0

This is a really nice example of simple loadFrameScript: https://github.com/mdn/e10s-example-addons/tree/master/run-script-in-all-pages/ported-message-manager/src/chrome/content

See here how he does DOMContentLoaded: https://github.com/mdn/e10s-example-addons/blob/master/run-script-in-all-pages/ported-message-manager/src/chrome/content/frame-script.js

Change that to document-element-inserted but i think this is not an event listener but an observer service.

Noitidart
  • 35,443
  • 37
  • 154
  • 323