// ==UserScript==
// @name           closeBlankTabOnCloseDownloadDialog.uc.js
// @namespace      http://space.geocities.yahoo.co.jp/gl/alice0775
// @description    ダウンロードをした際に、空白のタブを自動で閉じる
// @include        main
// @compatibility  Firefox 2.0 3.0
// @author         Alice0775
// @version        2008/04/15 12:00 メインウインドウが複数合った場合に動作しないのを修正
// @Note           need 000-windowhook.uc.js
// ==/UserScript==
// @version        2008/03/29 12:00 trunkで保存できない場合があったのでWindowHookにした

if(!('TM_init' in window))
WindowHook.register("chrome://mozapps/content/downloads/unknownContentType.xul",
  function(aWindow) {
    aWindow.addEventListener('unload',closeBlankTabOnCloseDownloadDialog,false);
    function closeBlankTabOnCloseDownloadDialog(event){
        var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                           .getService(Components.interfaces.nsIWindowMediator);
        var browserWindow = wm.getMostRecentWindow("navigator:browser");
        //ブラウザ
        var tab = browserWindow.gBrowser.mTabs;
        var max = tab.length;
        if(max <= 1) return;
        for(var i = max -1 ;i > -1 ; i--){
          if(tab[i].linkedBrowser.contentDocument.URL != 'about:blank')continue;
          browserWindow.setTimeout(function(aTab){browserWindow.gBrowser.removeTab(aTab);},500,tab[i]);
          return;
        }
    }
  }
);

if(!('TM_init' in window))
WindowHook.register("chrome://mozapps/content/downloads/downloads.xul",
  function(aWindow) {
    aWindow.addEventListener('unload',closeBlankTabOnCloseDownloadDialog,false);
    function closeBlankTabOnCloseDownloadDialog(event){
        var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                           .getService(Components.interfaces.nsIWindowMediator);
        var browserWindow = wm.getMostRecentWindow("navigator:browser");
        //ブラウザ
        var tab = browserWindow.gBrowser.mTabs;
        var max = tab.length;
        if(max <= 1) return;
        for(var i = max -1 ;i > -1 ; i--){
          if(tab[i].linkedBrowser.contentDocument.URL != 'about:blank')continue;
          browserWindow.setTimeout(function(aTab){browserWindow.gBrowser.removeTab(aTab);},500,tab[i]);
          return;
        }
    }
  }
);
/*
window.addEventListener('unload',closeBlankTabOnCloseDownloadDialog,false);
function closeBlankTabOnCloseDownloadDialog(event){
    //ブラウザ
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                       .getService(Components.interfaces.nsIWindowMediator);
    var browserWindow = wm.getMostRecentWindow("navigator:browser");
    var tab = browserWindow.getBrowser().mTabs;
    var max = tab.length;
    if(max <= 1) return;
    for(var i = max -1 ;i > -1 ; i--){
      if(tab[i].linkedBrowser.contentDocument.URL != 'about:blank')continue;
      browserWindow.getBrowser().removeTab(tab[i]);
      return;
    }
}

*/