/*
* Copyright (c) 2009 Eric Lawrence
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* */
var fiddlerhook = {
iInterval: 0,
iFiddlerPort: 8888,
isFiddlerListening: false,
bWatchSSL: true,
prefsProxy: null,
prefsHook: null,
oDynamicKey: null,
//
// This function is executed when our addon is loaded
//
onLoad: function() {
this.initialized = true;
this.strings = document.getElementById("fiddlerhook-strings");
document.getElementById('status-bar-panel-FiddlerHook').label = "Loading FiddlerHook...";
// https://developer.mozilla.org/En/NsIPrefBranch2
var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
this.prefsHook = prefManager.getBranch("extensions.fiddlerhook.");
this.prefsHook.QueryInterface(Components.interfaces.nsIPrefBranch2);
this.prefsHook.addObserver("", this, false);
this.prefsProxy = prefManager.getBranch("network.proxy.");
this.prefsProxy.QueryInterface(Components.interfaces.nsIPrefBranch2);
this.prefsProxy.addObserver("", this, false);
// Set up a monitor the Fiddler 2.2.1 Dynamic\Attached key
this.hookFiddlersDynamicKey();
this.iInterval = setInterval(this.queryRegistry, 1000, this);
this.doAutoUpdate();
this.updateFiddlerHookUI();
},
//
// This function is called when our addon is unloaded. It stops monitoring the keys.
//
onUnload: function() {
window.clearInterval(this.iInterval);
if (null != this.oDynamicKey){
this.oDynamicKey.stopWatching();
this.oDynamicKey.close();
}
if (null != this.prefsHook){
this.prefsHook.removeObserver("", this);
}
if (null != this.prefsProxy){
this.prefsProxy.removeObserver("", this);
}
},
//
// Try to open the Fiddler's registry keys and read its settings.
// Then, get the isFiddlerListening value and set up an observer for changes.
//
hookFiddlersDynamicKey: function() {
try {
var wrk = Components.classes["@mozilla.org/windows-registry-key;1"].createInstance(Components.interfaces.nsIWindowsRegKey);
wrk.open(wrk.ROOT_KEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Fiddler2", wrk.ACCESS_READ); // The required ACCESS_NOTIFY bit is in the ACCESS_READ value
this.iFiddlerPort = wrk.readIntValue("ListenPort");
this.bWatchSSL = ("True" == wrk.readStringValue("CaptureCONNECT"));
this.oDynamicKey = wrk.openChild("Dynamic", wrk.ACCESS_READ);
this.oDynamicKey.startWatching(false);
try {
this.isFiddlerListening = (1 == this.oDynamicKey.readIntValue("Attached"));
}
catch(e){}
return true;
}
catch(e) {
Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService).logStringMessage("FiddlerHook Monitoring Error: " + e.message);
return false;
}
},
//
// This function updates UI elements to match the current monitoring mode
//
updateFiddlerHookUI: function() {
if (null == document) return;
// Determine the current hook mode
var iHookMode = 0;
try{
iHookMode = this.prefsHook.getIntPref("hookmode");
}
catch(e){}
// Update status bar and menu state
switch (iHookMode){
case 0:
document.getElementById('rdoMainFHOptionDisable').setAttribute('checked', 'true');
document.getElementById('rdoFHOptionDisable').setAttribute('checked', 'true');
document.getElementById('status-bar-panel-FiddlerHook').label = "Fiddler: Disabled";
break;
case 1:
document.getElementById('rdoMainFHOptionForce').setAttribute('checked', 'true');
document.getElementById('rdoFHOptionForce').setAttribute('checked', 'true');
document.getElementById('status-bar-panel-FiddlerHook').label = "Fiddler: ForceOn";
break;
case 2:
document.getElementById('rdoMainFHOptionOpportune').setAttribute('checked', 'true');
document.getElementById('rdoFHOptionOpportune').setAttribute('checked', 'true');
document.getElementById('status-bar-panel-FiddlerHook').label =
(this.isFiddlerListening == 1) ? "Fiddler: ON (auto)" : "Fiddler: OFF (auto)";
break;
}
},
//
// This callback function executes if the \Software\Microsoft\Fiddler2\Dynamic\Attached DWORD changes
//
queryRegistry: function(oObj){
if ((null == oObj.oDynamicKey) || !oObj.oDynamicKey.hasChanged()) return; // Bail quickly
var i = 0;
try {
i = oObj.oDynamicKey.readIntValue("Attached");
}
catch (e) {
i = 0;
}
// "this" isn't the object you might expect, so we pass 'ourselves' using oObj. There's a good (complicated) reason for that;
// see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/this_Operator#Method_binding
oObj.isFiddlerListening = (1 == i);
oObj.doAutoUpdate();
oObj.updateFiddlerHookUI();
},
//
// This callback function executes if any of the preference values we care about have changed.
//
observe: function(subject, topic, data){
if (topic != "nsPref:changed") {
return;
}
switch(data)
{
// network.proxy.*
case "type":
case "http":
case "http_port":
// The user modified the network.proxy.type preferences using the UI.
// TODO: If settings are not pointing at us but should be, we should change UI to "disabled"?
// if ( (this.prefsProxy.getIntPref("type") == 1) && (this.prefsProxy.getCharPref("http") == "localhost") && (this.prefsProxy.getIntPref("http_port") == 8888))
break;
// extension.fiddlerhook.*
case "hookmode":
this.updateFiddlerHookUI();
break;
}
},
//
// This function executes when the user changes the monitoring option using the status bar menu or tools menu
//
onMenuCommand: function(e) {
//Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService).logStringMessage("MenuCommand: " + e.target.id);
switch(e.target.id){
case "rdoMainFHOptionOpportune":
case "rdoFHOptionOpportune":
this.prefsHook.setIntPref("hookmode", 2);
this.doAutoUpdate();
break;
case "rdoMainFHOptionForce":
case "rdoFHOptionForce":
this.prefsHook.setIntPref("hookmode", 1);
this.doAttach();
break;
case "rdoMainFHOptionDisable":
case "rdoFHOptionDisable":
this.prefsHook.setIntPref("hookmode", 0);
this.doDetach();
break;
default:
return; // throw exception?
break;
}
this.updateFiddlerHookUI();
},
//
// This function is called to handle Fiddler attach/detach changes
//
doAutoUpdate: function(){
// Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService).alert(window, "AutoUpdate", "hookmode: " + this.prefsHook.getIntPref("hookmode")+"\n"+ this.isFiddlerListening );
if (2 != this.prefsHook.getIntPref("hookmode")) return;
if (null == this.oDynami
- 1
- 2
前往页