home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 August / PCpro_2005_08.ISO / files / firefox / ScrapBook.xpi / chrome / scrapbook.jar / content / captureURL.js < prev    next >
Encoding:
JavaScript  |  2005-04-02  |  6.0 KB  |  216 lines

  1. /**************************************************
  2. // captureURL.js
  3. // Implementation file for captureURL.xul
  4. // 
  5. // Description: 
  6. // Author: Gomita
  7. // Contributors: 
  8. // 
  9. // Version: 
  10. // License: see LICENSE.txt
  11. **************************************************/
  12.  
  13.  
  14. //////////////////////////////////////////////////
  15. // global parameters
  16. //////////////////////////////////////////////////
  17.  
  18. var SBstring;
  19. var SBstatus;
  20. var SBURL;
  21.  
  22.  
  23. //////////////////////////////////////////////////
  24. // functions
  25. //////////////////////////////////////////////////
  26.  
  27. function SB_initURL()
  28. {
  29.     // XUL
  30.     SBstring = document.getElementById("ScrapBookString");
  31.     SBstatus = document.getElementById("ScrapBookStatus");
  32.     SBURL    = document.getElementById("ScrapBookCaptureURLTextbox");
  33.     // OKâ{â^âôé╠âëâxâïé≡É▌ÆΦ
  34.     document.documentElement.getButton("accept").label = SBstring.getString("CAPTURE_OK_BUTTON");
  35.     document.documentElement.getButton("accept").accesskey = "C";
  36.     // âtâHü[âJâX
  37.     SBURL.focus();
  38.     // âNâèâbâvâ{ü[âhé⌐éτURLĵô╛
  39.     setTimeout(SB_pasteClipboardURL, 0);
  40.     // âtâHâïâ_âèâXâgÅëè·ë╗
  41.     setTimeout(SB_initFolderWithDelay, 100);
  42. }
  43.  
  44.  
  45. function SB_acceptURL()
  46. {
  47.     // URLé╠özù±é≡É╢ɼ
  48.     var URLs = [];
  49.     var lines = SBURL.value.split("\n");
  50.     for ( var i = 0; i < lines.length; i++ )
  51.     {
  52.         if ( lines[i].length < 3 ) continue;
  53. ///        if ( !lines[i].match(/^(http|https|file):\/\//i) ) lines[i] = "http://" + lines[i];    // add default HTTP protocol
  54.         URLs.push(lines[i]);
  55.     }
  56.     if ( URLs.length < 1 ) return;
  57.     // â^ü[âQâbâgé╠âRâôâeâiâèâ\ü[âXû╝
  58.     var tarResName = SBarguments.resName ? SBarguments.resName : "urn:scrapbook:root";
  59.     // browser.xulé⌐éτopenDialog
  60.     if ( !SB_ensureWindowOpener() ) return;
  61.     window.opener.openDialog(
  62.         "chrome://scrapbook/content/capture.xul", "", "chrome,centerscreen,all,resizable,dialog=no",
  63.         URLs, "", false, false, tarResName, 0
  64.     );
  65. }
  66.  
  67.  
  68. function SB_ensureWindowOpener()
  69. {
  70.     // window.openeré╠èmöFüibrowser.xulé⌐éτopenDialogé│éΩé╚é»éΩé╬é╚éτé╚éóüj
  71.     var flag = false;
  72.     try {
  73.         if ( window.opener.location.href != "chrome://browser/content/browser.xul" ) flag = true;
  74.     } catch(ex) {
  75.         flag = true;
  76.     }
  77.     if ( flag ) { alert("ScrapBook ERROR: can't specify window.opener"); return false; }
  78.     return true;
  79. }
  80.  
  81.  
  82.  
  83. //////////////////////////////////////////////////
  84. // functions for URL tools
  85. //////////////////////////////////////////////////
  86.  
  87. function SB_clearURL()
  88. {
  89.     SBURL.value = "";
  90. }
  91.  
  92.  
  93. function SB_pasteClipboardURL()
  94. {
  95.     try {
  96.         var myClip  = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
  97.         var myTrans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
  98.         myTrans.addDataFlavor("text/unicode");
  99.         myClip.getData(myTrans, myClip.kGlobalClipboard);
  100.         // âfü[â^é≡ĵô╛
  101.         var str = new Object();
  102.         var len = new Object();
  103.         myTrans.getTransferData("text/unicode", str, len);
  104.         // ò╢ÄÜù±é╓ò╧è╖
  105.         if ( str ) {
  106.             str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
  107.             if ( str.toString().match(/^(http|https|file):\/\//) ) SBURL.value = str + "\n";
  108.         }
  109.     } catch(ex) {    // quiet warning
  110.     }
  111. }
  112.  
  113.  
  114. function SB_detectURLTabs()
  115. {
  116.     SB_clearURL();
  117.     var nodeList = window.opener.gBrowser.mTabContainer.childNodes;
  118.     for ( var i = 0; i < nodeList.length; i++ )
  119.     {
  120.         SBURL.value += window.opener.gBrowser.getBrowserForTab(nodeList[i]).contentDocument.location + "\n";
  121.     }
  122. }
  123.  
  124.  
  125. var SBlocalURLDetector = {
  126.  
  127.     index : 0,
  128.  
  129.     init : function()
  130.     {
  131.         this.index = 0;
  132.         // âtâ@âCâïâsâbâJü[
  133.         var FP = Components.classes['@mozilla.org/filepicker;1'].createInstance(Components.interfaces.nsIFilePicker);
  134.         FP.init(window, "", FP.modeGetFolder);
  135.         var answer = FP.show();
  136.         if ( answer == FP.returnOK )
  137.         {
  138.             SB_clearURL();
  139.             this.inspectDirectory(FP.file, 0);
  140.         }
  141.     },
  142.  
  143.     inspectDirectory : function(aDir, curIdx)
  144.     {
  145.         SBstatus.value = SBstring.getString("SCANNING") + " (" + curIdx + "/" + this.index + ")... " + aDir.path;
  146.         // âfâBâîâNâgâèé⌐éτæSâtâ@âCâïé≡ĵô╛
  147.         var aEntries = aDir.directoryEntries;
  148.         while ( aEntries.hasMoreElements() )
  149.         {
  150.             var aEntry = aEntries.getNext().QueryInterface(Components.interfaces.nsILocalFile);
  151.             ///alert(aEntry.leafName);
  152.             if ( aEntry.isDirectory() ) {
  153.                 // âTâuâfâBâîâNâgâèé≡ì─ïAôIé╔îƒì╕
  154.                 this.inspectDirectoryWithDelay(aEntry, ++this.index);
  155.             } else {
  156.                 // âtâ@âCâïû╝é╔â}âbâ`âôâO
  157.                 if ( aEntry.leafName.match(/\.(html|htm)$/i) ) SBURL.value += SBcommon.convertFilePathToURL(aEntry.path) + "\n";
  158.             }
  159.         }
  160.         if ( curIdx == this.index ) SBstatus.value = "";    // è«ù╣
  161.     },
  162.  
  163.     inspectDirectoryWithDelay : function(aDir, aIndex)
  164.     {
  165. ///        dump(aIndex + " " + aDir.leafName + "\n");
  166.         setTimeout(function(){ SBlocalURLDetector.inspectDirectory(aDir, aIndex); }, 200 * aIndex);
  167.     },
  168.  
  169. };
  170.  
  171.  
  172. var SBweboxURLDetector = {
  173.  
  174.     index : 0,
  175.     baseURL : "",
  176.  
  177.     init : function()
  178.     {
  179.         this.index = 0;
  180.         this.baseURL = "";
  181.         // âtâ@âCâïâsâbâJü[
  182.         var FP = Components.classes['@mozilla.org/filepicker;1'].createInstance(Components.interfaces.nsIFilePicker);
  183.         FP.init(window, "Select default.html for WeBoX.", FP.modeOpen);
  184.         FP.appendFilters(FP.filterHTML);
  185.         var answer = FP.show();
  186.         if ( answer == FP.returnOK )
  187.         {
  188.             SB_clearURL();
  189.             this.baseURL = FP.file.parent.path + '\\Data\\';
  190.             var lines = SBcommon.readFile(FP.file).split("\n");
  191.             for ( this.index = 0; this.index < lines.length; this.index++ )
  192.             {
  193.                 this.inspectWithDelay(lines[this.index], this.index);
  194.             }
  195.         }
  196.     },
  197.  
  198.     inspect : function(aLine, curIdx)
  199.     {
  200.         SBstatus.value = SBstring.getString("SCANNING") + "... (" + curIdx + "/" + (this.index-1) + ")";
  201.         if ( aLine.match(/ LOCALFILE\=\"([^\"]+)\" /) )
  202.         {
  203.             SBURL.value += SBcommon.convertFilePathToURL(this.baseURL + RegExp.$1) + "\n";
  204.         }
  205.         if ( curIdx == (this.index-1) ) SBstatus.value = "";    // è«ù╣
  206.     },
  207.  
  208.     inspectWithDelay : function(aLine, curIdx)
  209.     {
  210.         setTimeout(function(){ SBweboxURLDetector.inspect(aLine, curIdx); }, 50 * curIdx);
  211.     },
  212.  
  213. };
  214.  
  215.  
  216.