home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / navigator / viewsource.js < prev    next >
Encoding:
JavaScript  |  2002-04-10  |  5.5 KB  |  191 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is mozilla.org code.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s): 
  20.  *    Doron Rosenberg (doronr@naboonline.com) 
  21.  */
  22.  
  23. var gBrowser = null;
  24. var appCore = null;
  25. var gPrefs = null;
  26.  
  27. try {
  28.   var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  29.                               .getService(Components.interfaces.nsIPrefService);
  30.   gPrefs = prefService.getBranch(null);
  31. } catch (ex) {
  32. }
  33.  
  34. function onLoadViewSource() 
  35. {
  36.   viewSource(window.arguments[0]);
  37.   window._content.focus();
  38. }
  39.  
  40. function getBrowser()
  41. {
  42.   if (!gBrowser)
  43.     gBrowser = document.getElementById("content");
  44.   return gBrowser;
  45. }
  46.  
  47. function viewSource(url)
  48. {
  49.   if (!url)
  50.     return false; // throw Components.results.NS_ERROR_FAILURE;
  51.  
  52.   try {
  53.     appCore = Components.classes["@mozilla.org/appshell/component/browser/instance;1"]
  54.                         .createInstance(Components.interfaces.nsIBrowserInstance);
  55.  
  56.     // Initialize browser instance..
  57.     appCore.setWebShellWindow(window);
  58.   } catch(ex) {
  59.     // Give up.
  60.     window.close();
  61.     return false;
  62.   }
  63.  
  64.   var loadFromURL = true;
  65.   //
  66.   // Parse the 'arguments' supplied with the dialog.
  67.   //    arg[0] - URL string.
  68.   //    arg[1] - Charset value in the form 'charset=xxx'.
  69.   //    arg[2] - Page descriptor used to load content from the cache.
  70.   //
  71.   if ("arguments" in window) {
  72.     var arg;
  73.     //
  74.     // Set the charset of the viewsource window...
  75.     //
  76.     if (window.arguments.length >= 2) {
  77.       arg = window.arguments[1];
  78.  
  79.       try {
  80.         if (typeof(arg) == "string" && arg.indexOf('charset=') != -1) {
  81.           var arrayArgComponents = arg.split('=');
  82.           if (arrayArgComponents) {
  83.             //we should "inherit" the charset menu setting in a new window
  84.             getMarkupDocumentViewer().defaultCharacterSet = arrayArgComponents[1];
  85.           } 
  86.         }
  87.       } catch (ex) {
  88.         // Ignore the failure and keep processing arguments...
  89.       }
  90.     }
  91.     //
  92.     // Use the page descriptor to load the content from the cache (if
  93.     // available).
  94.     //
  95.     if (window.arguments.length >= 3) {
  96.       arg = window.arguments[2];
  97.  
  98.       try {
  99.         if (typeof(arg) == "object" && arg != null) {
  100.           var pageLoaderIface = Components.interfaces.nsIWebPageDescriptor;
  101.           var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  102.  
  103.           //
  104.           // Load the page using the page descriptor rather than the URL.
  105.           // This allows the content to be fetched from the cache (if
  106.           // possible) rather than the network...
  107.           //
  108.           PageLoader.LoadPage(arg, pageLoaderIface.DISPLAY_AS_SOURCE);
  109.           // The content was successfully loaded from the page cookie.
  110.           loadFromURL = false;
  111.         }
  112.       } catch(ex) {
  113.         // Ignore the failure.  The content will be loaded via the URL
  114.         // that was supplied in arg[0].
  115.       }
  116.     }
  117.   }
  118.  
  119.   if (loadFromURL) {
  120.     //
  121.     // Currently, an exception is thrown if the URL load fails...
  122.     //
  123.     var loadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
  124.     var viewSrcUrl = "view-source:" + url;
  125.     getBrowser().webNavigation.loadURI(viewSrcUrl, loadFlags, null, null, null);
  126.   }
  127.  
  128.   //check the view_source.wrap_long_lines pref and set the menuitem's checked attribute accordingly
  129.   if (gPrefs) {
  130.     try {
  131.       var wraplonglinesPrefValue = gPrefs.getBoolPref("view_source.wrap_long_lines");
  132.  
  133.       if (wraplonglinesPrefValue)
  134.         document.getElementById('menu_wrapLongLines').setAttribute("checked", "true");
  135.     } catch (ex) {
  136.     }
  137.   }
  138.  
  139.   window._content.focus();
  140.   return true;
  141. }
  142.  
  143. function ViewSourceClose()
  144. {
  145.   window.close();
  146. }
  147.  
  148. // Strips the |view-source:| for editPage()
  149. function ViewSourceEditPage()
  150. {
  151.   var url = window._content.location.href;
  152.   url = url.substring(12,url.length);
  153.   editPage(url,window, false);
  154. }
  155.  
  156. // Strips the |view-source:| for saveURL()
  157. function ViewSourceSavePage()
  158. {
  159.   var url = window._content.document.location.href;
  160.   url = url.substring(12,url.length);
  161.  
  162.   saveURL(url, null, "SaveLinkTitle");
  163. }
  164.  
  165. //function to toggle long-line wrapping and set the view_source.wrap_long_lines 
  166. //pref to persist the last state
  167. function wrapLongLines()
  168. {
  169.   //get the first pre tag which surrounds the entire viewsource content
  170.   var myWrap = window._content.document.getElementById('viewsource');
  171.  
  172.   if (myWrap.className == '')
  173.     myWrap.className = 'wrap';
  174.   else myWrap.className = '';
  175.  
  176.   //since multiple viewsource windows are possible, another window could have 
  177.   //affected the pref, so instead of determining the new pref value via the current
  178.   //pref value, we use myWrap.className  
  179.   if (gPrefs){
  180.     try {
  181.       if (myWrap.className == '') {
  182.         gPrefs.setBoolPref("view_source.wrap_long_lines", false);
  183.       }
  184.       else {
  185.         gPrefs.setBoolPref("view_source.wrap_long_lines", true);
  186.       }
  187.     } catch (ex) {
  188.     }
  189.   }
  190. }
  191.