简体   繁体   English

Firefox扩展:在加载DOM之前访问它

[英]Firefox Extension: Access the DOM Before It's Loaded

I'm trying to create a Firefox extension that fires my Javascript code before any of the current page's Javascript is fired. 我正在尝试创建一个Firefox扩展,以在触发任何当前页面的Javascript之前触发我的Javascript代码。 My Javascript code will basically control whether or not the page's Javascript code can be executed or denied. 我的Javascript代码基本上将控制是否可以执行或拒绝页面的Javascript代码。

I first started out by trying to follow this answer , but I couldn't really figure out how to get it to work and realized I was relying on onDOMContentLoaded , which loads after the Javascript has already executed. 我首先尝试遵循此答案 ,但是我真的无法弄清楚如何使其工作,并意识到我依靠的是onDOMContentLoaded ,它已在Javascript执行后加载。

I then turned my attention toward XPCOM, but once again didn't really understand what the Firefox tutorials were telling me. 然后,我将注意力转向XPCOM,但又一次并没有真正理解Firefox教程告诉我的内容。

I've recently been trying to make an extension through Firebug , but I seem to hit the same problem... only having access to the Javascript after it's been parsed/executed. 我最近一直在尝试通过Firebug进行扩展 ,但是我似乎遇到了同样的问题……只有在解析/执行Java脚本后才能访问Java脚本。 Here's the resulting code that I wrote. 这是我编写的结果代码 I think if I could access the file's objects in the onExamineResponse event, my problem could be solved, but I don't know how to do that... I'm talking about this code: 我认为如果可以在onExamineResponse事件中访问文件的对象,则可以解决我的问题,但是我不知道该怎么做...我在说这段代码:

BeepbopListener.prototype = {
    onRequest: function(context, file) {
       ...
    },

    onExamineResponse: function(context, file) {
        FBTrace.sysout("onexamineresponse " + file);  // this returns something like
        // '[xpconnect wrapped (nsISupports, nsIHttpChannel, nsIRequest, nsIUploadChannel, nsITraceableChannel, nsIHttpChannelInternal)]'
        // but I don't know how to access those elements...
        var pattern = /\.js$/;
        if (pattern.test(file.href) && FBTrace.DBG_BEEPBOP) {
          FBTrace.sysout("ONEXAMINE DOESN'T EVEN GET IN THIS IF SO YOU WON'T SEE THIS");
        }
    },

    ...
};

So my question is... is there a tutorial out there that shows me how I can get access to all Javascript code on a page before it's executed? 所以我的问题是...是否有一个教程可以向我展示如何在执行页面之前访问页面上的所有Javascript代码? Also, if anyone has any helpful insight, I'd love to hear it. 另外,如果有人有任何有用的见解,我也希望听到。 Oh, and if y'all need more code from me, just let me know, and I'll post it. 哦,如果你们都需要我提供更多代码,请告诉我,我将其发布。

You can access a new document before any JavaScript code runs by listening to the content-document-global-created observer notification . 您可以在运行任何JavaScript代码之前访问新文档,方法是侦听content-document-global-created观察者通知 However, the document will be empty at this point and JavaScript code will run as soon as the parser adds a <script> tag - you cannot really prevent it. 但是,此时文档将为空,并且在解析器添加<script>标记后,JavaScript代码将立即运行-您无法真正阻止它。 Here are the options to control script execution that I am aware of. 这是我知道的控制脚本执行的选项。

1) Disable all JavaScript for a window using nsIDocShell.allowJavascript : 1)使用nsIDocShell.allowJavascript禁用窗口的所有JavaScript:

wnd.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
   .getInterface(Components.interfaces.nsIWebNavigation)
   .QueryInterface(Components.interfaces.nsIDocShell)
   .allowJavascript = false;

This is an all or nothing approach. 这是一种全有或全无的方法。 Note that JavaScript stays disabled even when a new document loads into the same frame. 请注意,即使新文档加载到同一框架中,JavaScript也会保持禁用状态。

2) Implement the nsIContentPolicy interface in an XPCOM component and register it in the content-policy category (via nsICategoryManager ). 2)在XPCOM组件中实现nsIContentPolicy接口 ,并将其注册在content-policy类别中(通过nsICategoryManager )。 Your shouldLoad() function will be able to block scripts selectively - but it will only called for external scripts (meaning <script src="..."> ), not for inline scripts on the page. 您的shouldLoad()函数将能够有选择地阻止脚本-但它只会调用外部脚本(即<script src="..."> ),而不会调用页面上的内联脚本。

3) Use JavaScript debugger service to intercept script execution. 3)使用JavaScript调试器服务来拦截脚本执行。 You could use jsdIDebuggerService.interruptHook to step through JavaScript execution and abort the script whenever you like. 您可以使用jsdIDebuggerService.interruptHook逐步执行JavaScript,并在需要时中止脚本。 But that would slow down JavaScript execution very significantly of course. 但这当然会大大降低JavaScript的执行速度。 At the very least you should use jsdIDebuggerService.addFilter() to restrict it to a particular document, otherwise you will slow down the entire browser (including browser UI). 至少应使用jsdIDebuggerService.addFilter()将其限制为特定文档,否则,将降低整个浏览器(包括浏览器UI)的速度。

I'm trying to create a Firefox extension that fires my Javascript code before any of the current page's Javascript is fired. 我正在尝试创建一个Firefox扩展,以在触发任何当前页面的Javascript之前触发我的Javascript代码。 My Javascript code will basically control whether or not the page's Javascript code can be executed or denied. 我的Javascript代码基本上将控制是否可以执行或拒绝页面的Javascript代码。

Start by completely preventing the document from getting parsed altogether then on the side, fetch the same document, do any processing on this document and then inject the resulting document in the page. 首先,完全防止文档被完全解析,然后从侧面开始,获取相同的文档,对该文档进行任何处理,然后将生成的文档注入页面中。 Here is how I currently do just that https://stackoverflow.com/a/36097573/6085033 这是我目前的工作方式https://stackoverflow.com/a/36097573/6085033

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM