使用loadFrameScript,如何在执行页面脚本之前注入文件? [英] Using loadFrameScript, how can inject file before the page's script execution?

查看:65
本文介绍了使用loadFrameScript,如何在执行页面脚本之前注入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前在 bootstrap.js 文件中使用它.在我使用 recentBrowserWindow.messageManager.loadFrameScript(file,true):

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.

但是这里的脚本 inject.js 在客户端页面之后运行.问题是:

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

如何在执行页面脚本之前注入 inject.js ?我应该使用哪种方法?

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

推荐答案

来自:它说要使用 document-element-inserted ,这将确保您的脚本在页面脚本运行之前运行.

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.

这篇关于使用loadFrameScript,如何在执行页面脚本之前注入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆