右クリックメニューでn階層上がる

現ページの1階層上のページを開くスクリプトは数多くあるけれど、ロケーションバーの右クリックメニューで任意の上位URLに飛べるものは見あたらなかったので作りました。

// ==UserScript==
// -*- mode:JScript; Encoding:utf8n -*-
// @name           upper URI
// @namespace      http://d.hatena.ne.jp/p-arai/
// @description    Add upper URIs to the context menu in Location bar.
//
// @author         p-arai
// @version        2007.08.05


(function()
{
  var attr_name = "item-type";
  var attr_value = "upper-uri-menuitem";
  
  // ポップアップ消去時に URIs とセパレータを削除
  document.getElementById("urlbar").addEventListener("popuphidden", function(event)
  {
    var menupopup = event.originalTarget;
    var items = menupopup.getElementsByAttribute(attr_name, attr_value);
    while(items.length){
      menupopup.removeChild(items[0]);
    }
  }, false);

  // ポップアップ表示時に URIs を調査、メニュー項目を生成
  document.getElementById("urlbar").addEventListener("popupshowing", function(event)
  {
    var menupopup = event.originalTarget;
    var items = menupopup.getElementsByAttribute(attr_name, attr_value);
    var uri_array = getUpURIs();
    if (items.length == 0 && uri_array.length > 0){
      var goUp = function(event)
      {
        if (event.type == "click" && event.button != 1) return;
        loadURI(event.originalTarget.getAttribute("label"));
        menupopup.hidePopup();
      };
      // セパレータ
      var sep = document.createElement("menuseparator");
      sep.setAttribute(attr_name, attr_value);
      menupopup.insertBefore(sep, menupopup.firstChild);
      // URIs
      for(var i=0; i < uri_array.length; i++){
        var menuitem = document.createElement("menuitem");
        menuitem.id = attr_value + i;
        menuitem.setAttribute("label", uri_array[i]);
        menuitem.setAttribute(attr_name, attr_value);
        menuitem.addEventListener("command", goUp, false);
        menuitem.addEventListener("click",   goUp, false);
        menupopup.insertBefore(menuitem, sep);
      }
    }
  }, false);

  var getUpURIs = function()
  {
    var uri_array = [];
    var uri = gBrowser.currentURI;
    var uri_path = uri.path;
    if (uri_path == "/") return [];
    uri_path = uri_path.replace(/\/$/, "");
    var path_array = uri_path.split("/");
    path_array.pop();
    while(path_array.length){
      uri_array.push( uri.prePath + path_array.join("/") + "/" );
      path_array.pop();
    }
    return uri_array;
  };

})();

ロケーションバーのアイコンをダブルクリックでIEを開く

Sleipnir 仕様に。前の全選択スクリプトと合わせて使ってます。

(function MyLocationBar() {

  /* URL の左にあるアイコンをダブルクリックすると IE で同じページを表示する。
    (Sleipnir 仕様)
  */
  var IE_PATH = "C:\\Program Files\\Internet Explorer\\iexplore.exe";

  function openIE() {
    var exe = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
    var ss = Cc["@mozilla.org/browser/shell-service;1"].getService(Ci.nsIShellService_MOZILLA_1_8_BRANCH);
    exe.initWithPath(IE_PATH);
    ss.openApplicationWithURI(exe, locationbar.value);
  }
  var locationicon = document.getElementById("page-proxy-deck");
  locationicon.addEventListener('dblclick', openIE, false);

})();

ロケーションバーをクリックすると全選択になるようにする。

(function MyLocationBar() {
  
  /* LocationBar をクリックすると全選択。
     キーボードショートカットなら全選択になるが、クリックだとならないので。
   */
  var locationbar = document.getElementById("urlbar");
  locationbar.addEventListener('focus', function(){ this.select(); }, true);
   
})();

新規タブを、リンクから開く場合は右隣に、それ以外は右端に作成。

まだまだ未完成。
何から呼ばれるか判断するいい方法が分からないので addTab を呼ぶ関数の名前で判断している。
これだとかなり環境に依存するし、全然スマートじゃない。(´Д`;)
TMP を使っていることが前提だし、外部アプリから呼ばれた場合の判別ができない。

他にいい判別方法がないでしょうか・・・。

(function TailNewTab(){

  function is_not_from_link(the_method, the_uri)
    {
      // for debug
      if(typeof the_method.caller.caller.name != 'undefined' &&
         typeof the_method.caller.name != 'undefined'){
        dump("\n**** " + the_method.caller.caller.name + ">");
        dump(the_method.caller.name + ">" + the_uri + "\n");
      }

      var ret = (
        (the_uri == "about:blank") ||                        // 外部呼び出し
        (the_method.caller.caller.name == "openUILinkIn") || // 履歴、ブックマーク
        (the_method.caller.name == "__TMP_LoadBarURL")       // URLバー、検索バー
        );
      return ret;
    }

  gBrowser.__addTabOriginal    = gBrowser.addTab;
  gBrowser.addTab = function(aURI, aReferrerURI, aCharset, aPostData,
                                          aOwner, aAllowThirdPartyFixup)
    {
      var oldTabPos = this.mCurrentTab._tPos;
      var t = this.__addTabOriginal(aURI, aReferrerURI, aCharset, aPostData,
                                          aOwner, aAllowThirdPartyFixup);
      if (is_not_from_link(this.addTab, aURI)){
        this.moveTabTo(t, this.mTabs.length-1);
      }else{
        this.moveTabTo(t, oldTabPos + 1);
      }
      return t;
    };
})();

sleipnirのショートカット検索をfirefoxで実現するための UserChrome.js

検索バーで「w ほげ」と打つとWikiPediaで「ほげ」を検索する機能を追加する。

firefoxにはアドレスバーで同じようなことができるスマートキーワードという機能があるが、やはり使い慣れた検索バーでできた方が嬉しいし、検索プラグインとスマートキーワード用のサイトが別管理なのも面倒くさい。

準備として、各サーチエンジンの定義ファイル (profile/searchplugins/*.xml) 内の
または でキーワードを定義しておく。


w


(function MySearchBar() {
  
  /* 1. SearchBar をクリックすると全選択。
        (キーボードショートカットなら全選択になるが、クリックだとならないので。)
     2. sleipnir のショートカット検索を実装。
        - アドレスバーでなく検索バーで keyword + 検索語
   */
  var RETURN_TO_ORIG_ENGINE = true; // 検索後にデフォルトのエンジンに戻るか?

  
  var bar = document.getElementById("searchbar");
  var textbox = bar._textbox;
  
  var quick_search = function(){
    var qs_engines = bar._engines;
    var alias = null;
    var qs_num = -1;
    for(var i=0; i < qs_engines.length; i++){
      alias = qs_engines[i].alias;
      if(alias != null && textbox.value.indexOf(alias + " ") == 0){
        qs_num = i;
        break;
      }
    }
    dump("QS: " + qs_num + "\n");
    if(qs_num > -1){
      bar._origEngine = bar.currentEngine;
      bar.currentEngine = qs_engines[qs_num];
      textbox.value = textbox.value.substr(alias.length+1);
      setTimeout(function(){bar.focus();}, 0);
    }
  };

  if(RETURN_TO_ORIG_ENGINE){
    eval("bar.handleSearchCommand = " + bar.handleSearchCommand.toString().
       replace('return;', 'if(this._origEngine){this.currentEngine = this._origEngine; this._origEngine = null;} return;'));
    dump(bar.handleSearchCommand.toString());
    dump("\n---\n");
  }
  
  bar.addEventListener('focus', function(){textbox.select();}, true);
  bar.addEventListener('input', function(){quick_search();}, true);
})();

[2007.3.21]

  • 普通の検索の時に例外が発生していたのを修正。

[2007.5.17]

  • 検索語が入力済みの状態でも先頭にキーワードを入れるとエンジンが切り替わるようにした。
  • フォーカスが外れる問題の回避策を当てずっぽうで仕込んでみた。