home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / phoenx05.zip / phoenix / chrome / browser.jar / content / browser / viewsource.js < prev    next >
Text File  |  2002-12-07  |  6KB  |  198 lines

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