0

I need to rewrite a JavaScript function with Fiddler. I believe this answer partially addresses the problem; however, I'm still searching for RegEx information. For example:

function traceThis(message) {
   //setTimeout(function() { window.opener.childWindowMessageHandler(message);}, 100);
   //if (!window.console) console = {};
   //console.log(message);
}

needs to become:

function traceThis(message) {
   setTimeout(function() { window.opener.childWindowMessageHandler(message);}, 100);
   if (!window.console) console = {};
   console.log(message);
}

I imagine it would be simpler to match and replace the individual lines within the traceThis() function; however, I think it would be most useful to learn how to select and replace any desired function from the "function" to its closing "}" regardless of statements within it.

Community
  • 1
  • 1
  • That is not generally possible with regex, since JavaScript (or for that matter most programming languages) are not regular languages (in particular because you have nested structures). Hence, you should look for a JavaScript parser. – Martin Ender Nov 07 '12 at 17:30

2 Answers2

0

The source of a function can be gotten by traceThis.toString () From there you can do whatever changes you want then recreate the function using new Function

HBP
  • 15,685
  • 6
  • 28
  • 34
0

HBP's answer won't work, because the toString() function isn't available until the script is actually running. But he does point to a simple way to solve this problem that probably won't require either a JS parser or a complicated Regular Expression.

Namely, simply do a straight string-replacement of:

function traceThis(message) {

with

// Here's my NewFunction
function traceThis(message)
{
   // My new instructions here
}

function OriginalVersionOfTraceThis(message) {

That way, when your injector runs, it overwrites the front of the old function with your desired script code, and renames the old function to something else (that will never be called).

EricLaw
  • 56,563
  • 7
  • 151
  • 196