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

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

问题描述

我目前在 bootstrap.js 文件中使用它。用于在我的情况下编辑客户端的JavaScript代码我正在使用 recentBrowserWindow.messageManager.loadFrameScript(file,true)

  const {类:Cc,接口:Ci,utils:Cu,结果:Cr} =组件; 
Cu.import('resource://gre/modules/Services.jsm');

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

函数启动(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被定义:,jQuery);
// true,jQuery已经分配在客户端页面上。

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

lockquote

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



解决方案

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



它使用插入文档元素,this将确保您的脚本在页面脚本运行之前运行。



我没有太多关于框架脚本的经验,其他人将不得不验证我这一点。


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?

解决方案

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.

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

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