Using loadFrameScript, how can inject file before

2019-05-24 07:11发布

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条回答
贼婆χ
2楼-- · 2019-05-24 07:56

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.

查看更多
成全新的幸福
3楼-- · 2019-05-24 08:09

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.

查看更多
登录 后发表回答