3

I want to develop an extension which works on scripts coming from HTTP response. I know that whole HTML code first goes to rendering engine inside browser where it is parsed to create a DOM tree. Any script embedded inside is passed to the JavaScript Engine.(Correct me if I am wrong. :) ) So I wanted to intercept the JavaScript code before it is sent to the JavaScript Engine in order to modify them accordingly.

Are there any APIs for Mozilla Firefox which would allow me to do this? How can I do it?

Makyen
  • 31,849
  • 12
  • 86
  • 121
Naman
  • 991
  • 2
  • 10
  • 20
  • https://developer.mozilla.org/en-US/Add-ons/Overlay_Extensions/XUL_School/Intercepting_Page_Loads – megawac Feb 04 '14 at 05:16
  • I think here they are talking about tempering data coming or going from the browser. But what I want is to access javascript which is going to javascript engine every time. – Naman Feb 04 '14 at 06:08

5 Answers5

2

while doing some stuff i stumbled across this: https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/NsITraceableChannel?redirectlocale=en-US&redirectslug=NsITraceableChannel

this allows you to modify stuff before it is parsed. see this topic here: http://forums.mozillazine.org/viewtopic.php?f=19&t=2800541

here is a working example of getting the content before it is shown to user. it doesnt change it though, thats what im asking in the mozillazine topic. the writeBytes should modify it, once you figure it out please share as im interested as well https://github.com/Noitidart/demo-nsITraceableChannel

Noitidart
  • 35,443
  • 37
  • 154
  • 323
1

You can follow this answer on how to intercept each request and modify before sending it to the page itself. You can do transpilation or whatever you'd like there.

Community
  • 1
  • 1
jsantell
  • 1,268
  • 8
  • 10
  • I have read about it, I think it intercept HTML data before parsing. What I want is after parsing script. – Naman Feb 05 '14 at 08:05
0

take a look at this guys addons code. he does exactly what you are looking for: https://addons.mozilla.org/en-US/firefox/addon/javascript-deminifier/

Noitidart
  • 35,443
  • 37
  • 154
  • 323
0

You can try invade before HTML'll be parsed and take all tags, work with them and put it back.

Tim Marinin
  • 338
  • 1
  • 9
0

...I wanted to intercept these javascript code before Javascript Engine and modify them accordingly. Is there any APIs for mozilla firefox? How can I do it?

You can use page-mod of the Addon-SDK by setting contentScriptWhen: "start"

Then after completely preventing the document from getting parsed you can fetch the same document on the side, do any modifications and inject the resulting document in the page. Here is an answer which does just that https://stackoverflow.com/a/36097573/6085033

Community
  • 1
  • 1