いまさらMozReplでfirefoxのUAなどをいじる

http://wiki.github.com/bard/mozrepl
MozRepl HTMLDocumentまでのアクセス - hibomaのはてなダイアリー

初期化?

replinit.js

var hogehgoe = {

  Cc: Components.classes,
  Ci: Components.interfaces,
  startUpService: Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup),
  observerService: Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService),
  preferencesService: Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefService).getBranch(''),
  // FUEL
  application: Cc["@mozilla.org/fuel/application;1"].getService(Ci.fuelIApplication),
  
  _observer: null,
  
  // UAを設定する。
  setUserAgent: function(ua) {
    var supportsStringInterface = Ci.nsISupportsString;
    var string = Cc["@mozilla.org/supports-string;1"].createInstance(supportsStringInterface);
    string.data = decodeURIComponent(ua);
    this.preferencesService.setComplexValue('general.useragent.override', supportsStringInterface, string);
    application.console.log('set useragent: ' + ua);
  },
  
  // リファラーとhttpヘッダ用のUAを設定する。
  setReferer: function(ref, ua) {
    var sRef = ref;
    var myObserver = {
      observe: function(subject,topic,data){
        if(topic != 'http-on-modify-request') return;
        var http = subject.QueryInterface(Ci.nsIHttpChannel);
        http.setRequestHeader('Referer', sRef, false);
        // http headerのuaを設定
        http.setRequestHeader('User-Agent', ua, false);
        if (http.referrer)
            http.referrer.spec = sRef;
    };

    if ( this._observer ) {
    try {
        this.observerService.removeObserver(this._observer,'http-on-modify-request',false);
        } catch (e) {}
    }
      
    this._observer = myObserver;
    this.observerService.addObserver(this._observer,'http-on-modify-request',false);
    
    application.console.log('set referer: ' + sRef);
  },
  
  // クッキー、リファラーをリセットする。
  reset: function() {
    Cc['@mozilla.org/cookiemanager;1'].getService(Ci.nsICookieManager).removeAll();
    try {
        this.observerService.removeObserver(this._observer,'http-on-modify-request',false);
        application.console.log('reset cookie, referer.');
        } catch (e) {}
  },

  // firefoxを終了する。
  stop: function() {
    this.startUpService.quit(Ci.nsIAppStartup.eForceQuit); 
  }
  
};

MozReplの設定

/patn/to/プロファイル名/extensions/mozrepl@hyperstruct.net/defaults/preferences/mozrepl.js

pref("extensions.mozrepl.initUrl", "file:///path/to/replinit.js");
pref("extensions.mozrepl.autoStart", true);

呼び出し

repl>repl.hogehoge.setUserAgent('oreore')
repl>repl.hogehoge.setReferer('http://www.example.com','oreore')
repl>repl.hogehoge.reset()
repl>repl.hogehoge.stop()