The new context menu items created by your script are added to the top of the context menu.
Extending the Rules Menu
Your script may add new entries to the Rules menu, but they will work somewhat differently than the items added to the Tools menu and Web Sessions context menu. Instead of annotating a function using an attribute, you will instead annotate a variable (field) in your script using a RulesOption attribute. For instance, to add the Hide 304s menu item, the following script is used:
RulesOption("Hide 304s")
public static var m_Hide304s: boolean = false;
Two types of fields in your script may be bound to Rules menu items: booleans, and strings.
Note: The default value of the field is used to set the initial state of the menu item when the script is compiled. After that initial setting, the binding between the UI and the field is one-directional. That means that when the user clicks a field-bound menu item, the value of the bound boolean or string is changed, but if the script itself changes the boolean’s or string’s value, no change is made to the menu item’s state.
Boolean-bound Rules
When Fiddler encounters a boolean variable in the script that is annotated with a RulesOption attribute, a menu item will be created using the text provided. The menu item’s checked state will track the value of the boolean.
In the prior example, the boolean’s value is false, so the menu item will be unchecked by default. When the user clicks the menu item, it will become checked, and Fiddler will change the value of the bound m_Hide304s variable to true. Every time your script is recompiled, the default values of your boolean variables are restored, and the checkmarks on the Rules menu will be updated accordingly.
Simply declaring the attributed m_Hide304s variable doesn’t yet do anything useful—the variable simply tracks the state of the menu item. The functionality of the rule is provided by a block of code added to the OnBeforeResponse method found later in the script:
177 | FiddlerScript if (m_Hide304s && (304 == oSession.responseCode)) {
oSession["ui-hide"] = "true";
// Note: This block could be placed in the OnPeekAtResponseHeaders method, // since it does not depend upon the availability of the response body.
}
This block first checks to see if the rule is enabled and if so, checks that the server returned a HTTP/304. If so, the block sets the ui-hide flag on the Session, which causes it to be hidden from the Web Sessions list.
You can also use different forms of the RulesOption attribute to create submenus of options. To do so, provide the name of the submenu as the second parameter of the attribute. For instance, the following three fields create a Performance submenu that exposes the three options:
RulesOption("Simulate &Modem Speeds", "Per&formance") public static var m_SimulateModem: boolean = false;
RulesOption("&Disable Caching", "Per&formance") public static var m_DisableCaching: boolean = false;
RulesOption("&Show Time-to-Last-Byte", "Per&formance") public static var m_ShowTTLB: boolean = false;
If you would like some of the items on your submenu to be mutually exclusive (showing as a radio group instead of a set of checkboxes), you can set a third boolean parameter to true, and you can add a splitter after a menu item by setting yet a fourth boolean parameter to true.
To create this menu:
Add the following script:
RulesOption("Option A", "MyMenu", true) public static var m_OptionA: boolean = true;
RulesOption("Option B", "MyMenu", true) public static var m_OptionB: boolean = false;
RulesOption("Option C", "MyMenu", true, true) // Splitter after option public static var m_OptionC: boolean = false;
178 | FiddlerScript
RulesOption("Some other setting", "MyMenu", false) public static var m_OtherSetting: boolean = true;
String-bound Rules
When Fiddler encounters a string in the script that is annotated with a RulesString attribute, a new submenu item will be created. The first parameter to the RulesString constructor is a string containing the submenu’s name, while the second parameter is a boolean indicating whether an item labeled Disabled should be added to the submenu.
Possible values for the string’s value are provided by adding one or more RulesStringValue attributes to the same variable. Each RulesStringValue will create a new mutually-exclusive menu item on the new submenu. When the user checks one of the menu items, the variable will be updated to the string value selected and all other menu items on the menu will be unchecked.
For instance, this script:
RulesString("MyStringRule", true)
RulesStringValue("MyMenuText1", "MyValue1") RulesStringValue("MyMenuText2", "MyValue2") RulesStringValue("MyMenuText3", "MyValue3")
public static var m_StringRule: String = String.Empty;
…creates the following menu:
By default, the menu’s items will be sorted alphabetically by the menu text. In some cases, you may wish to specify a different order. To do so, use the RulesStringValue overload that accepts an integer ordering parameter. For instance, you can create a menu that looks like this:
…using this script:
RulesString("MyStringRule", true)
179 | FiddlerScript RulesStringValue(1, "First Item", "one")
RulesStringValue(2, "Second Item", "two", true) RulesStringValue(3, "Third Item", "three") RulesStringValue(4, "Fourth Item", "four") RulesStringValue(5, "Ask me...", "%CUSTOM%") public static var m_StringRule: String = "two";
This script also shows how to set one of the entries as the default (see the last parameter on the “Second Item”
RulesStringValue). Also, note the magic value %CUSTOM% used for the “Ask me…” menu item. When clicked, the user will be prompted to enter a value for the variable:
When the prompt is shown, the current value of the string variable will be presented for the user to modify.
Fiddler’s Rules > User-Agents feature is implemented using a string variable named sUA bound to a RulesString submenu. Inside the OnBeforeRequest method, script checks whether the bound variable is set, and if so, uses its value to overwrite the request’s User-Agent header:
if (null != sUA) {
oSession.oRequest["User-Agent"] = sUA;
}