Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting JavaScript into head element of website using Fiddler

I was thinking of using Fiddler for the following purpose...

I have a JavaScript based service I want to demonstrate to potential clients. In order to show them what their website could look like if they install (i.e. include) my script, I want to set up Fiddler on my PC, so that when fetching the client's website, the

<script type="text/JavaScript" src="myscript.js"></script> 

line will be included in the HTML <head> section.

Can this be easily done with Fiddler? Could someone point me to where I may find the documentation covering that, if it is?

Thanks!

----Update----

For the time being I have resorted to using a BHO to add my script to the page. I use execScript(), upon onDocumentComplete, to run a simple piece of JavaScript which appends the .js file I need to the page. But EricLaw's pointers and jitter's answer seem like the way to go for a more complete (and elegant) way to do what I need.

If someone is interested I could upload the BHO code here. -Thanks!

like image 305
odedbd Avatar asked Dec 19 '09 19:12

odedbd


1 Answers

Open fiddler -> Menu Rules -> Customize Rules (or hit Ctrl+R)

The CustomRule.js file opens. Scroll down until you find the line

static function OnBeforeResponse(oSession: Session) 

This is where your code goes. Here you can change the server response before the browser sees it.

The following code sample shows how to include a custom piece of jQuery code which replaces the Unanswered link in the horizontal menu with a link which serves as short cut to Unanswered jQuery Questions

I first show you the jQuery code I want to include

<script type='text/javascript'>     $(function() {         var newLink = '<a href="/unanswered/tagged/jquery">Unanswered jQuery</a>';         $('div#hmenus div.nav:first ul li:last a').replaceWith(newLink);     }); </script> 

Now the fiddler code (based on code found in CustomRules.js and code samples from the FiddlerScript CookBook)

//is it a html-response and is it from stackoverflow.com if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html") &&     oSession.HostnameIs("stackoverflow.com")) {      // Remove any compression or chunking     oSession.utilDecodeResponse();      var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);      // Match the jQuery script tag     var oRegEx = /(<script[^>]*jquery.min.js"><\/script>)/gi;     // replace the script tag withitself (no change) + append custom script tag     oBody = oBody.replace(oRegEx, "$1<script type='text/javascript'>$(function() {$('div#hmenus div.nav:first ul li:last a').replaceWith('<a href=\"/unanswered/tagged/jquery\">Unanswered jQuery</a>');})</script>");      // Set the response body to the changed body string     oSession.utilSetResponseBody(oBody);  } 

I think you should now able to hackyourself together a piece of code which fits your problem.

Example

// Match the head end var oRegEx = /(<\/head>)/gi; // replace with new script oBody = oBody.replace(oRegEx, "<script type='text/javascript' src='http://url/myscript.js'></script>$1"); 
like image 197
jitter Avatar answered Oct 05 '22 22:10

jitter