Compat inspector fiddler rule

Origional post here. My version of the fiddler CustomRules.js file below.

import System;
import System.Windows.Forms;
import Fiddler;

// INTRODUCTION
// This is the FiddlerScript Rules file, which creates some of the menu commands and
// other features of Fiddler. You can edit this file to modify or add new commands.
//
// The original version of this file is named SampleRules.js and it is in the
// \Program Files\Fiddler\ folder. When Fiddler first starts, it creates a copy named
// CustomRules.js inside your \Documents\Fiddler2\Scripts folder. If you make a 
// mistake in editing this file, simply delete the CustomRules.js file and restart
// Fiddler. A fresh copy of the default rules will be created from the original
// sample rules file.

// GLOBALIZATION NOTE:
// Be sure to save this file with UTF-8 Encoding if using any non-ASCII characters
// in strings, etc.

// JScript Reference
// http://www.fiddler2.com/redir/?id=msdnjsnet
//
// FiddlerScript Reference
// http://www.fiddler2.com/redir/?id=fiddlerscriptcookbook
//
// FiddlerScript Editor: 
// http://www.fiddler2.com/redir/?id=fiddlerscripteditor
class Handlers
{
 static function OnBeforeResponse(oSession: Session) {
// Snippet 1: Place this inside the "OnBeforeResponse" handler in your FiddlerScript
InjectInspectorScript(oSession);
}

// Snippet 2: Place this near the end of your FiddlerScript (within the Handlers class)
public static RulesOption("Use Compat Inspector")
var m_UseCompatInspector: boolean = false;
static function InjectInspectorScript(oSession: Session)
{
	if(!m_UseCompatInspector) return;
	
	// Ensure we only inject into HTML
	if (oSession.url.EndsWith(".js")) return;
	if (oSession.url.EndsWith(".css")) return;
	if (!oSession.oResponse.MIMEType.Contains("text/html")) return;
	
	// Retrieve the response body
	var sBody = oSession.GetResponseBodyAsString();
	// One final check to ensure the content looks like HTML
	if (!/^\uFEFF?\s*</.exec(sBody)) return;
	
	// Prepare to inject
	var pos = 0; // Initial position is start of document
	
	// Locate important elements in the page
	var doctype = sBody.IndexOf("<!doctype", StringComparison.OrdinalIgnoreCase);
	var meta = sBody.IndexOf("X-UA-Compatible", StringComparison.OrdinalIgnoreCase);
	var script = sBody.IndexOf("<script", StringComparison.OrdinalIgnoreCase);
	
	// Place after doctype (if present)
	if (doctype != -1) doctype = sBody.IndexOf(">", doctype + 1);
	if (doctype != -1 && doctype != sBody.Length - 1) pos = doctype + 1;
	
	// Place after first X-UA-Compatible meta tag (if present)
	if (meta != -1) meta = sBody.IndexOf(">", meta + 1);
	if (meta != -1 && meta != sBody.Length - 1) pos = meta + 1;
	
	// Place before any script tags that occur before the current position (if present)
	if (script != -1 && script < pos) pos = script;
	
	// Perform the injection at the detected location
	oSession.utilSetResponseBody(
		sBody.Insert(pos, "<script src='http://ie.microsoft.com/testdrive/HTML5/CompatInspector/inspector.js'></script>")
	);
}
}

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Tooling and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s