FiddlerScript 是Fiddler 的一項非常強大的功能,它允許你增強Fiddler UI,添加新的特性,修改請求與響應內容等等。。。
1.編寫FiddlerScript
FiddlerScript 基於JScript.NET 語言。在Fiddler 中點擊菜單“Rules > Customize Rules”打開FiddlerScript Editor 編輯器,在這里可以編寫Fiddler Script 腳本,只要一保存,Fiddler 將會重新編譯腳本並自動加載,如果加載成功,則會播放聲音並在Fiddler 狀態欄顯示"CustomRules.js was loaded at <datetime>" 提示信息,如果編譯失敗,將會顯示錯誤提示信息。
如果你沒有安裝FiddlerScript Editor 插件,也可以在其他編輯器編寫腳本,但是推薦使用FiddlerScript Editor編輯器,因為它具有語法高亮以及自動完成功能,下載地址http://fiddler2.com/fiddlerscript-editor。
如果你想使用其他編輯器編寫FiddlerScript,可以在Tools > Fiddler Options 中設置,如下設置使用notepad++作為編輯器:
如果編寫腳本時需要引用第三方.net程序集,可以在按如下方式添加:
a.在References文本框中填寫需要引用的程序集地址,如果有多個以分號分隔。
b.也可將程序集注冊到GAC中,或者將程序集復制到Fidder.exe安裝的目錄下。
添加好程序集之后,就可以在編寫腳本時用Import語句導入程序集的命名空間。
編寫的自定義腳本文件CustomRules.js 保存在用戶的我的文檔目錄下:
win7系統位於:C:\Users\[your user]\Documents\Fiddler2\Scripts\
xp系統位於: C:\Documents and Settings\[your user]\My Documents\Fiddler2\Scripts\
如果不想再使用自定義規則時,可以刪除些文件,下次啟動Fiddler 時,FiddlerScript Editor將會使用安裝目錄下的SampleRules.js 重新生成該文件。
2.FiddlerScript 中的主要方法
你的FiddlerScript 文件中包含一個靜態類Handlers,一般來說,你的代碼應該寫在這個類的靜態方法中。
Fiddler 運行時會自動執行一些" Application event methods":
static function OnBoot | fiddler 啟動時調用 |
static function OnShutdown | fiddler關閉時調用 |
static function OnAttach | fiddler注冊成系統代理時調用 |
static function OnDetach | fiddler 取消注冊系統代理時調用 |
static function Main | 在每次fiddler啟動時和編譯CustomRules.js 腳本時調用。 |
// 在這個方法中修改Request的內容, 我們用得最多
static function OnBeforeRequest(oSession: Session)
// 在這個方法中修改Response的內容
static function OnBeforeResponse(oSession: Session)
// 在個方法中包含Fiddler 命令,在Fiddler界面中左下方的QuickExec Box,如果你的腳本處理了指定的命令,則返回true,否則返回false.
static function OnExecAction(sParams: String[])
3.FiddlerScript 與Attributes
Handlers 類允許你使用attributes 注解對外暴露的字段或者方法,可以使這些字段出現在Fiddler UI界面中。
使用BindUIColumn attribute 為方法添加注解,可以添加列到web 會話列表中:
public static BindUIColumn("Method", 60) function static FillMethodColumn(oS: Session): String { return oS.RequestMethod; }
只要保存腳本就可以看到fiddler會話列表多了一個請求方式列,標記當前請求的方式。
使用QuickLinks attribute 添加最頂層的菜單:
QuickLinkMenu("&Links") QuickLinkItem("財付通", "https://www.tenpay.com") QuickLinkItem("支付寶", "https://www.alipay.com") public static function DoLinksMenu(sText: String, sAction: String) { Utilities.LaunchHyperlink(sAction); }
保存腳本將會看到Fiddler 最上面多了一個菜單項,點擊菜單項並可以打開相應的網站
4.修改Session在Fiddler的顯示樣式
我們可以控制Session在Fiddler中顯示的樣式, 具體文檔是: http://www.fiddlerbook.com/Fiddler/dev/SessionFlags.asp
把這段腳本放在OnBeforeRequest(oSession: Session) 方法下,並且點擊"Save script", 這樣所有的www.tenpay.com 的會話都會顯示紅色.
if (oSession.HostnameIs("www.tenpay.com")){ oSession["ui-color"] = "red"; }
5.Fiddler Script中修改Cookie
cookie其實就是request 中的一個header.
// 刪除所有的cookie
oSession.oRequest.headers.Remove("Cookie");
// 新建cookie
oSession.oRequest.headers.Add("Cookie", "username=testname;loginpwd=123456");
注意: Fiddler script不能直接刪除或者編輯單獨的一個cookie, 你需要用replace方法或者正則表達式的方法去操作cookie的string
static function OnBeforeRequest(oSession: Session) { if (oSession.HostnameIs('www.example.com') && oSession.uriContains('pagewithCookie') && oSession.oRequest.headers.Contains("Cookie")) { var sCookie = oSession.oRequest["Cookie"]; // 用replace方法或者正則表達式的方法去操作cookie的string sCookie = sCookie.Replace("cookieName=", "ignoreme="); oSession.oRequest["Cookie"] = sCookie; }
6.在Fiddler Script中修改Request 中的body
方法一:
static function OnBeforeRequest(oSession: Session) { if(oSession.uriContains("http://www.cnblogs.com/TankXiao/")) { // 獲取Request 中的body字符串 var strBody=oSession.GetRequestBodyAsString(); // 用正則表達式或者replace方法去修改string strBody=strBody.replace("1111","2222"); // 彈個對話框檢查下修改后的body FiddlerObject.alert(strBody); // 將修改后的body,重新寫回Request中 oSession.utilSetRequestBody(strBody); } }
方法二:提供了一個非常簡單的方法,可以直接替換body中的數據
oSession.utilReplaceInRequest("1111", "2222");
7.其它參考資料
JScript.NET語言參考:http://msdn.microsoft.com/en-us/library/z688wt03(VS.80).aspx
控制Session 的UI樣式:http://www.fiddlerbook.com/Fiddler/dev/SessionFlags.asp
Fiddler Script 的官方幫助文檔:http://www.fiddlerbook.com/Fiddler/dev/ScriptSamples.asp
修改Fiddler 請求與響應:http://fiddler2.com/documentation/KnowledgeBase/FiddlerScript/ModifyRequestOrResponse
Fiddler Blog: http://www.telerik.com/automated-testing-tools/blog/eric-lawrence.aspx