2

I'm trying to create a Firefox extension that fires my Javascript code before any of the current page's Javascript is fired. My Javascript code will basically control whether or not the page's Javascript code can be executed or denied.

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.

I then turned my attention toward XPCOM, but once again didn't really understand what the Firefox tutorials were telling me.

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. 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:

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? 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.

Community
  • 1
  • 1
incutonez
  • 3,241
  • 9
  • 43
  • 92

2 Answers2

2

You can access a new document before any JavaScript code runs by listening to the content-document-global-created observer notification. 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. Here are the options to control script execution that I am aware of.

1) Disable all JavaScript for a window using nsIDocShell.allowJavascript:

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.

2) Implement the nsIContentPolicy interface in an XPCOM component and register it in the content-policy category (via 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.

3) Use JavaScript debugger service to intercept script execution. You could use jsdIDebuggerService.interruptHook to step through JavaScript execution and abort the script whenever you like. But that would slow down JavaScript execution very significantly of course. 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).

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Hmm, not the answer I was hoping for, but a solid answer nonetheless. The route I'm trying right now is making a proxy, and that seems to be doing the trick, but I was really hoping there was an easier way of doing this. Oh well. – incutonez Apr 19 '12 at 23:42
  • @incutonez: Well, if you want to intercept HTTP traffic, [nsITraceableChannel](http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/) might be a simpler solution. You should be aware of [bug 653533](https://bugzilla.mozilla.org/show_bug.cgi?id=653533) however. – Wladimir Palant Apr 20 '12 at 05:26
  • Quantum killed this. – beppe9000 Jun 02 '19 at 14:34
  • Of course it did. Pretty much any question tagged firefox-addons can be ignored now. I've got 692 answers there, and I'm not going to update all of them. – Wladimir Palant Jun 02 '19 at 18:13
2

I'm trying to create a Firefox extension that fires my Javascript code before any of the current page's Javascript is fired. My Javascript code will basically control whether or not the page's Javascript code can be executed or denied.

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

Community
  • 1
  • 1
  • While this link may answer the question, it is [better to include the essential parts of the answer here](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers/8259#8259) and provide the link for reference. Link-only answers can become invalid if the linked page changes. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – Makyen Mar 19 '16 at 06:03