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

  1. /**************************************************
  2. // capture.js
  3. // Implementation file for capture.xul
  4. // 
  5. // Description: 
  6. // Author: Gomita
  7. // Contributors: 
  8. // 
  9. // Version: 
  10. // License: see LICENSE.txt
  11. **************************************************/
  12.  
  13.  
  14.  
  15. var SBstring;
  16. var SBbrowser;
  17. var SBmessage;
  18. var SBlistbox;
  19.  
  20. var gURLs = [];
  21. var gRefURL = "";
  22. var gShowDetail   = false;
  23. var gDocumentOnly = false;
  24. var gContResName = "";
  25. var gContResIdx  = 0;
  26. var gInterval    = 5;
  27.  
  28.  
  29.  
  30. function SB_trace(aMessage)
  31. {
  32.     SBmessage.value = aMessage;
  33. }
  34.  
  35.  
  36. function SB_initCapture()
  37. {
  38.     SBstring  = document.getElementById("ScrapBookString");
  39.     SBbrowser = document.getElementById("ScrapBookCaptureBrowser");
  40.     SBmessage = document.getElementById("ScrapBookCaptureTextbox");
  41.     SBlistbox = document.getElementById("ScrapBookCaptureListbox");
  42.     gURLs         = window.arguments[0];
  43.     gRefURL       = window.arguments[1];
  44.     gShowDetail   = window.arguments[2];
  45.     gDocumentOnly = window.arguments[3];
  46.     gContResName  = window.arguments[4];
  47.     gContResIdx   = window.arguments[5];
  48.     if ( gURLs.length < 1 ) {
  49.         return;
  50.     } else if ( gURLs.length == 1 ) {
  51.         SBlistbox.hidden = true;
  52.     } else if ( gURLs.length < 10 ) {
  53.         SBlistbox.setAttribute("rows", gURLs.length + 2);
  54.     } else {
  55.         SBlistbox.setAttribute("rows", 10);
  56.     }
  57.     gURLs = SB_checkDuplicatedURL(gURLs);
  58.     SBbrowser.webProgress.addProgressListener(SBdocumentLoadListener, Components.interfaces.nsIWebProgress.NOTIFY_ALL);
  59.     SBtask.init();
  60.     SBtask.start();
  61. }
  62.  
  63.  
  64. function SB_checkDuplicatedURL(aURLs){
  65.  
  66.     var URLhash = {};
  67.     for ( var i = 0; i < aURLs.length; i++ )
  68.     {
  69.         if ( !aURLs[i].match(/^(file|http|https):\/\//i) ) continue;
  70.         aURLs[i] = ( (pos = aURLs[i].indexOf("#")) != -1 ) ? aURLs[i].substring(0, pos) : aURLs[i];
  71.         URLhash[aURLs[i]] = true;
  72.     }
  73.     aURLs = [];
  74.     for ( var aURL in URLhash ) aURLs.push(aURL);
  75.     return aURLs;
  76. }
  77.  
  78.  
  79.  
  80.  
  81. var SBtask = {
  82.  
  83.     index : 0,
  84.  
  85.     URL         : "",
  86.     contentType : "",
  87.     isDocument  : false,
  88.     canRefresh  : true,
  89.  
  90.     init : function()
  91.     {
  92.         this.index = 0;
  93.         for ( var i = 0; i < gURLs.length; i++ )
  94.         {
  95.             SBlistbox.appendItem("[" + i + "] " + gURLs[i]);
  96.         }
  97.     },
  98.  
  99.     start : function()
  100.     {
  101.         this.URL = gURLs[this.index];
  102.         this.contentType = "";
  103.         this.isDocument = true;
  104.         this.canRefresh = true;
  105.         SBlistbox.focus();
  106.         SBlistbox.ensureIndexIsVisible(this.index);
  107.         SBlistbox.selectedIndex = this.index;
  108.         SB_trace(SBstring.getString("CONNECT") + "... " + this.URL);
  109.         if ( this.URL.substring(0,7) == "file://" ) {
  110.             SB_loadDocument();
  111.         } else {
  112.             var sniffer = new SBheaderSniffer(this.URL, gRefURL);
  113.             sniffer.httpHead();
  114.         }
  115.     },
  116.  
  117.     toggleShowButtons : function(willShow)
  118.     {
  119.         document.getElementById("ScrapBookCaptureRetryButton").hidden = !willShow;
  120.     },
  121.  
  122.     succeed : function()
  123.     {
  124.         if ( gURLs.length > 1 ) SBlistbox.getItemAtIndex(this.index).setAttribute("style", "color:#33CC33;");
  125.         this.skip(false);
  126.     },
  127.  
  128.     fail : function(aErrorMsg)
  129.     {
  130.         if ( aErrorMsg ) SB_trace(aErrorMsg);
  131.         if ( gURLs.length > 1 ) {
  132.             SBlistbox.getItemAtIndex(this.index).label = "[" + this.index + "] " + aErrorMsg;
  133.             SBlistbox.getItemAtIndex(this.index).setAttribute("style", "color:#FF0000;font-weight:bold;");
  134.             this.skip(true);
  135.         } else {
  136.             this.toggleShowButtons(true);
  137.         }
  138.     },
  139.  
  140.     skip : function(quickly)
  141.     {
  142.         if ( ++this.index >= gURLs.length ) {
  143.             this.finalize();
  144.         } else {
  145.             SB_trace(SBstring.getFormattedString("WAITING", [gInterval]) + "...");
  146.             window.setTimeout(function(){ SBtask.start(); }, quickly ? 0 : gInterval * 1000);
  147.         }
  148.     },
  149.  
  150.     finalize : function()
  151.     {
  152.         try {
  153.             if ( gURLs.length > 1 ) window.opener.SBcapture.notifyOnComplete(false, false, false);
  154.         } catch(ex) {
  155.         }
  156.         window.setTimeout(function(){ window.close(); }, 1500);
  157.     },
  158.  
  159. };
  160.  
  161.  
  162.  
  163.  
  164. function SBheaderSniffer(aURLSpec, aRefURLSpec)
  165. {
  166.     this.URLSpec    = aURLSpec;
  167.     this.refURLSpec = aRefURLSpec;
  168. }
  169.  
  170.  
  171. SBheaderSniffer.prototype = {
  172.  
  173.     _URL     : Components.classes['@mozilla.org/network/standard-url;1'].createInstance(Components.interfaces.nsIURL),
  174.     _channel : null,
  175.     _headers : null,
  176.  
  177.     httpHead : function()
  178.     {
  179.         this._channel = null;
  180.         this._headers = {};
  181.         try {
  182.             this._URL.spec = this.URLSpec;
  183.             this._channel = SBservice.IO.newChannelFromURI(this._URL).QueryInterface(Components.interfaces.nsIHttpChannel);
  184.             this._channel.loadFlags = this._channel.LOAD_BYPASS_CACHE;
  185.             this._channel.setRequestHeader("User-Agent", navigator.userAgent, false);
  186.             if ( this.refURLSpec ) this._channel.setRequestHeader("Referer", this.refURLSpec, false);
  187.         }
  188.         catch(ex) {
  189.             this.onHttpError("Invalid URL");
  190.         }
  191.         try {
  192.             this._channel.requestMethod = "HEAD";
  193.             this._channel.asyncOpen(this, this);
  194.         }
  195.         catch(ex) {
  196.             this.onHttpError(ex);
  197.         }
  198.     },
  199.  
  200.     getHeader : function(aHeader)
  201.     {
  202.          try { return this._channel.getResponseHeader(aHeader); } catch(ex) {}
  203.         return "";
  204.     },
  205.  
  206.     getStatus : function()
  207.     {
  208.         try { return this._channel.responseStatus; } catch(ex) {}
  209.         return "";
  210.     },
  211.  
  212.     visitHeader : function(aHeader, aValue)
  213.     {
  214.         this._headers[aHeader] = aValue;
  215.     },
  216.  
  217.     onDataAvailable : function(aRequest, aContext, aInputStream, aOffset, aCount) {},
  218.     onStartRequest  : function(aRequest, aContext) {},
  219.     onStopRequest   : function(aRequest, aContext, aStatus) { this.onHttpSuccess(); },
  220.  
  221.     onHttpSuccess : function()
  222.     {
  223.         SBtask.contentType = this.getHeader("Content-Type");
  224.         var httpStatus = this.getStatus();
  225.         SB_trace(SBstring.getString("CONNECT_SUCCESS") + " (Content-Type: " + SBtask.contentType + ")");
  226.         switch ( httpStatus )
  227.         {
  228.             case 404 : SBtask.fail(SBstring.getString("HTTP_STATUS_404") + " (404 Not Found)"); return;
  229.             case 403 : SBtask.fail(SBstring.getString("HTTP_STATUS_403") + " (403 Forbidden)"); return;
  230.             case 500 : SBtask.fail("500 Internal Server Error"); return;
  231.         }
  232.         if ( !SBtask.contentType )
  233.         {
  234.             SBtask.fail(SBstring.getString("CONTENT_TYPE_FAILURE")); return;
  235.         }
  236.         if ( SBtask.contentType.match(/(text|html|xml)/i) )
  237.         {
  238.             SBtask.isDocument = true;
  239.             SB_loadDocument();
  240.         }
  241.         else
  242.         {
  243.             SBtask.isDocument = false;
  244.             gDocumentOnly ? SBtask.skip(true) : SB_execCapture();
  245.         }
  246.     },
  247.     onHttpError : function(aErrorMsg)
  248.     {
  249.         SBtask.fail(SBstring.getString("CONNECT_FAILURE") + " (" + aErrorMsg + ")");
  250.     },
  251. };
  252.  
  253.  
  254. function SB_loadDocument()
  255. {
  256.     SBbrowser.docShell.allowJavascript = false;
  257.     SBbrowser.docShell.allowMetaRedirects = false;
  258.     SBbrowser.docShell.QueryInterface(Components.interfaces.nsIDocShellHistory).useGlobalHistory = false;
  259.     SBbrowser.removeEventListener("load", SB_execCapture, true);
  260.     SBbrowser.addEventListener("load", SB_execCapture, true);
  261.     SBbrowser.contentWindow.location.href = SBtask.URL;
  262.     SBdocumentLoadListener.init();
  263. }
  264.  
  265.  
  266. var SBdocumentLoadListener = {
  267.  
  268.     fileCount : 0,
  269.  
  270.     init : function()
  271.     {
  272.         this.fileCount = 0;
  273.     },
  274.  
  275.     QueryInterface : function(aIID)
  276.     {
  277.         if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  278.             aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  279.             aIID.equals(Components.interfaces.nsIXULBrowserWindow) ||
  280.             aIID.equals(Components.interfaces.nsISupports))
  281.             return this;
  282.         throw Components.results.NS_NOINTERFACE;
  283.     },
  284.     onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
  285.     {
  286.         if ( aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_START )
  287.         {
  288.             SB_trace(SBstring.getString("LOADING") + "... " + ++this.fileCount + " " + SBtask.URL);
  289.         }
  290.     },
  291.     onProgressChange : function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  292.     {
  293.         if ( aCurTotalProgress != aMaxTotalProgress )
  294.         {
  295.             SB_trace(SBstring.getString("TRANSFER_DATA") + "... (" + aCurTotalProgress + " Bytes)");
  296.         }
  297.     },
  298.     onStatusChange   : function() {},
  299.     onLocationChange : function() {},
  300.     onSecurityChange : function() {},
  301. }
  302.  
  303.  
  304. function SB_execCapture()
  305. {
  306.     SB_trace(SBstring.getString("CAPTURE_START"));
  307.     if ( SBbrowser.contentDocument.body && SBtask.isDocument )
  308.     {
  309.         var metaElems = SBbrowser.contentDocument.getElementsByTagName("meta");
  310.         for ( var i=0; i<metaElems.length; i++ )
  311.         {
  312.             if ( metaElems[i].hasAttribute("http-equiv") && metaElems[i].hasAttribute("content") &&
  313.                  metaElems[i].getAttribute("http-equiv").toLowerCase() == "refresh" && 
  314.                  metaElems[i].getAttribute("content").match(/URL\=(.*)$/i) )
  315.             {
  316.                 var newURL = SBcommon.resolveURL(SBtask.URL, RegExp.$1);
  317.                 if ( newURL != SBtask.URL && SBtask.canRefresh )
  318.                 {
  319.                     SBtask.URL = newURL;
  320.                     SBtask.canRefresh = false;
  321.                     SBbrowser.contentWindow.location.href = SBtask.URL;
  322.                     return;
  323.                 }
  324.             }
  325.         }
  326.         var success = SBcapture.doCaptureDocument(SBbrowser.contentWindow, false, gShowDetail, gContResName, gContResIdx);
  327.     }
  328.     else
  329.     {
  330.         var myType = SBtask.contentType.match(/image/i) ? "image" : "file";
  331.         var success = SBcapture.doCaptureFile(SBtask.URL, myType, gRefURL ? gRefURL : SBtask.URL, gShowDetail, gContResName, gContResIdx);
  332.     }
  333.     if ( !success )
  334.     {
  335.         SB_trace(SBstring.getString("CAPTURE_ABORT"));
  336.         SBtask.fail();
  337.     }
  338. }
  339.  
  340.  
  341.  
  342. SBcapture.notifyOnComplete = function(aID, aIcon, aTitle)
  343. {
  344.     if ( gURLs.length == 1 )
  345.     {
  346.         window.opener.SBcapture.notifyOnComplete(aID, aIcon, aTitle);
  347.     }
  348.     SBtask.succeed();
  349. }
  350.  
  351.  
  352. SBdownloadListenerCallback = {
  353.  
  354.     onCompleteDownload : function(aItem)
  355.     {
  356.         SB_trace(SBstring.getString("CAPTURE") + "... (" + SBcapture.httpTask[aItem.id] + ") " + aItem.title);
  357.     },
  358.     onCompleteAllDownloads : function(aItem)
  359.     {
  360.         SB_trace(SBstring.getString("CAPTURE_COMPLETE") + " : " + aItem.title);
  361.         SBcapture.notifyOnComplete(aItem.id, aItem.icon, aItem.title);
  362.     },
  363.     onProgressDownload : function(aItem, aFileName, aProgress)
  364.     {
  365.         SB_trace(SBstring.getString("TRANSFER_DATA") + "... (" + SBcapture.httpTask[aItem.id] + ") " + aProgress + " : " + aFileName);
  366.     },
  367. };
  368.  
  369.  
  370.