home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / navigator / viewsource.js < prev    next >
Text File  |  2003-06-08  |  7KB  |  221 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.  *    Neil Rashbrook (neil@parkwaycc.co.uk)
  22.  */
  23.  
  24. const pageLoaderIface = Components.interfaces.nsIWebPageDescriptor;
  25. var gBrowser = null;
  26. var appCore = null;
  27. var gPrefs = null;
  28.  
  29. try {
  30.   var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  31.                               .getService(Components.interfaces.nsIPrefService);
  32.   gPrefs = prefService.getBranch(null);
  33. } catch (ex) {
  34. }
  35.  
  36. function onLoadViewSource() 
  37. {
  38.   viewSource(window.arguments[0]);
  39.   window._content.focus();
  40. }
  41.  
  42. function getBrowser()
  43. {
  44.   if (!gBrowser)
  45.     gBrowser = document.getElementById("content");
  46.   return gBrowser;
  47. }
  48.  
  49. function viewSource(url)
  50. {
  51.   if (!url)
  52.     return false; // throw Components.results.NS_ERROR_FAILURE;
  53.  
  54.   try {
  55.     appCore = Components.classes["@mozilla.org/appshell/component/browser/instance;1"]
  56.                         .createInstance(Components.interfaces.nsIBrowserInstance);
  57.  
  58.     // Initialize browser instance..
  59.     appCore.setWebShellWindow(window);
  60.   } catch(ex) {
  61.     // Give up.
  62.     window.close();
  63.     return false;
  64.   }
  65.  
  66.   var loadFromURL = true;
  67.   //
  68.   // Parse the 'arguments' supplied with the dialog.
  69.   //    arg[0] - URL string.
  70.   //    arg[1] - Charset value in the form 'charset=xxx'.
  71.   //    arg[2] - Page descriptor used to load content from the cache.
  72.   //
  73.   if ("arguments" in window) {
  74.     var arg;
  75.     //
  76.     // Set the charset of the viewsource window...
  77.     //
  78.     if (window.arguments.length >= 2) {
  79.       arg = window.arguments[1];
  80.  
  81.       try {
  82.         if (typeof(arg) == "string" && arg.indexOf('charset=') != -1) {
  83.           var arrayArgComponents = arg.split('=');
  84.           if (arrayArgComponents) {
  85.             //we should "inherit" the charset menu setting in a new window
  86.             getMarkupDocumentViewer().defaultCharacterSet = arrayArgComponents[1];
  87.           } 
  88.         }
  89.       } catch (ex) {
  90.         // Ignore the failure and keep processing arguments...
  91.       }
  92.     }
  93.     //
  94.     // Use the page descriptor to load the content from the cache (if
  95.     // available).
  96.     //
  97.     if (window.arguments.length >= 3) {
  98.       arg = window.arguments[2];
  99.  
  100.       try {
  101.         if (typeof(arg) == "object" && arg != null) {
  102.           var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  103.  
  104.           //
  105.           // Load the page using the page descriptor rather than the URL.
  106.           // This allows the content to be fetched from the cache (if
  107.           // possible) rather than the network...
  108.           //
  109.           PageLoader.LoadPage(arg, pageLoaderIface.DISPLAY_AS_SOURCE);
  110.           // The content was successfully loaded from the page cookie.
  111.           loadFromURL = false;
  112.         }
  113.       } catch(ex) {
  114.         // Ignore the failure.  The content will be loaded via the URL
  115.         // that was supplied in arg[0].
  116.       }
  117.     }
  118.   }
  119.  
  120.   if (loadFromURL) {
  121.     //
  122.     // Currently, an exception is thrown if the URL load fails...
  123.     //
  124.     var loadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
  125.     var viewSrcUrl = "view-source:" + url;
  126.     getBrowser().webNavigation.loadURI(viewSrcUrl, loadFlags, null, null, null);
  127.   }
  128.  
  129.   //check the view_source.wrap_long_lines pref and set the menuitem's checked attribute accordingly
  130.   if (gPrefs) {
  131.     try {
  132.       var wraplonglinesPrefValue = gPrefs.getBoolPref("view_source.wrap_long_lines");
  133.  
  134.       if (wraplonglinesPrefValue)
  135.         document.getElementById('menu_wrapLongLines').setAttribute("checked", "true");
  136.     } catch (ex) {
  137.     }
  138.     try {
  139.       document.getElementById("menu_highlightSyntax").setAttribute("checked", gPrefs.getBoolPref("view_source.syntax_highlight"));
  140.     } catch (ex) {
  141.     }
  142.   } else {
  143.     document.getElementById("menu_highlightSyntax").setAttribute("hidden", "true");
  144.   }
  145.  
  146.   window._content.focus();
  147.   return true;
  148. }
  149.  
  150. function ViewSourceClose()
  151. {
  152.   window.close();
  153. }
  154.  
  155. // Strips the |view-source:| for editPage()
  156. function ViewSourceEditPage()
  157. {
  158.   var url = window._content.location.href;
  159.   url = url.substring(12,url.length);
  160.   editPage(url,window, false);
  161. }
  162.  
  163. // Strips the |view-source:| for saveURL()
  164. function ViewSourceSavePage()
  165. {
  166.   var url = window._content.document.location.href;
  167.   url = url.substring(12,url.length);
  168.  
  169.   saveURL(url, null, "SaveLinkTitle");
  170. }
  171.  
  172. //function to toggle long-line wrapping and set the view_source.wrap_long_lines 
  173. //pref to persist the last state
  174. function wrapLongLines()
  175. {
  176.   //get the first pre tag which surrounds the entire viewsource content
  177.   var myWrap = window._content.document.getElementById('viewsource');
  178.  
  179.   if (myWrap.className == '')
  180.     myWrap.className = 'wrap';
  181.   else myWrap.className = '';
  182.  
  183.   //since multiple viewsource windows are possible, another window could have 
  184.   //affected the pref, so instead of determining the new pref value via the current
  185.   //pref value, we use myWrap.className  
  186.   if (gPrefs){
  187.     try {
  188.       if (myWrap.className == '') {
  189.         gPrefs.setBoolPref("view_source.wrap_long_lines", false);
  190.       }
  191.       else {
  192.         gPrefs.setBoolPref("view_source.wrap_long_lines", true);
  193.       }
  194.     } catch (ex) {
  195.     }
  196.   }
  197. }
  198.  
  199. //function to toggle syntax highlighting and set the view_source.syntax_highlight
  200. //pref to persist the last state
  201. function highlightSyntax()
  202. {
  203.   var highlightSyntaxMenu = document.getElementById("menu_highlightSyntax");
  204.   var highlightSyntax = (highlightSyntaxMenu.getAttribute("checked") == "true");
  205.   gPrefs.setBoolPref("view_source.syntax_highlight", highlightSyntax);
  206.  
  207.   var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  208.   PageLoader.LoadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  209. }
  210.  
  211. // Fix for bug 136322: this function overrides the function in
  212. // browser.js to call PageLoader.LoadPage() instead of BrowserReloadWithFlags()
  213. function BrowserSetForcedCharacterSet(aCharset)
  214. {
  215.   var docCharset = getBrowser().docShell.QueryInterface(
  216.                             Components.interfaces.nsIDocCharset);
  217.   docCharset.charset = aCharset;
  218.   var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  219.   PageLoader.LoadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  220. }
  221.