home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 March / PCpro_2005_03.ISO / files / firefox / ieview.xpi / chrome / ieview.jar / content / ieviewOverlay.js < prev    next >
Encoding:
JavaScript  |  2004-10-28  |  10.0 KB  |  410 lines

  1. /* 
  2.  *
  3.  * Copyright (c) 2003  Paul Roub <paul@roub.net>
  4.  *
  5.  * $Header: /cvs/ieview/ieview/content/ieviewOverlay.js,v 1.4 2004/10/28 12:41:22 roub Exp $
  6.  *
  7.  * Portions based on GPLed code by
  8.  *     Ted Mielczarek
  9.  *
  10.  * This library is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU Lesser General Public
  12.  * License as published by the Free Software Foundation; either
  13.  * version 2.1 of the License, or (at your option) any later version.
  14.  * 
  15.  * This library is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.  * Lesser General Public License for more details.
  19.  * 
  20.  * You should have received a copy of the GNU Lesser General Public
  21.  * License along with this library; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23.  *
  24.  * *****
  25.  *
  26.  * Notes:
  27.  *
  28.  *    Since we can't know ahead of time where IE will be installed, we Kiboze the
  29.  *    Start Menu tree.  If the shortcut has been renamed (i.e. it is no longer
  30.  *    titled "Internet Explorer"), we're out of luck.  The only option at that
  31.  *    point would be to open and dereference every symlink we find, and see if the
  32.  *    target leaf name is "iexplore.exe".  Not doing that at the moment, since
  33.  *    it just seems insane.
  34.  */
  35.  
  36. var ieviewMenuItems = new Array("ieview-do-view", "ieview-do-viewlink");
  37. var userPrograms = "Progs";
  38. var allUserPrograms = "CmPrgs";
  39. var applicationData = "AppData";
  40.  
  41. var gIeViewBundle;
  42.  
  43. function isJsLink(href)
  44. {
  45.   return(href && 
  46.      (href.substr(0, 11).toLowerCase() == "javascript:")
  47.         );
  48. }
  49.  
  50. function ieviewContext() {
  51.   if(gContextMenu) {
  52.     for(var i=0; i<ieviewMenuItems.length; i++) {
  53.       var menuitem = document.getElementById(ieviewMenuItems[i]);
  54.       if(menuitem && (i == 0))        // click-on-page item
  55.       {
  56.         menuitem.hidden = (gContextMenu.isTextSelected || gContextMenu.onLink || gContextMenu.onImage || gContextMenu.onTextInput );
  57.       }
  58.       else if (menuitem)        // click-on-link item
  59.       {
  60.         menuitem.hidden = (! gContextMenu.onLink);    // no link, no link item
  61.  
  62.     //    disable the link if it's javascript
  63.     //
  64.     var disable = ((! menuitem.hidden) && isJsLink(gContextMenu.linkURL()));
  65.  
  66.     if (disable)
  67.         menuitem.setAttribute('disabled', 'true');
  68.     else
  69.         menuitem.setAttribute('disabled', 'false');
  70.       }
  71.     }
  72.   }
  73. }
  74.  
  75. function ieView() {
  76.   if(gContextMenu) {
  77.      var href = gBrowser.currentURI.spec;
  78.  
  79.      ieViewLaunch("Internet Explorer.lnk", href);
  80.   }
  81. }
  82.  
  83.  
  84. function ieViewLink() {
  85.   if(gContextMenu) {
  86.       var href = gContextMenu.linkURL();
  87.  
  88.       ieViewLaunch("Internet Explorer.lnk", href);
  89.   }
  90. }
  91.  
  92.  
  93.  
  94. // attempt to grab the real path of a predefined directory
  95. //
  96. function tryDir(dsp, key)
  97. {
  98.    try
  99.    {
  100.       var nif = dsp.get(key, Components.interfaces.nsIFile);
  101.       return(nif.path);
  102.    }
  103.    catch (ar)
  104.    {
  105.       return("");
  106.    }
  107. }
  108.  
  109.  
  110. function searchPath(path, fname)
  111. {
  112.    var result = null;
  113.  
  114.    try
  115.    {
  116.       var f = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  117.  
  118.       f.initWithPath(path);
  119.  
  120.       if (f.exists() && f.isDirectory())
  121.       {
  122.          var entries = f.directoryEntries;
  123.  
  124.          while (entries.hasMoreElements())
  125.          {
  126.             var ent = entries.getNext().QueryInterface(Components.interfaces.nsIFile);
  127.  
  128.             if (ent.isDirectory())
  129.             {
  130.                result = searchPath(ent.path, fname);
  131.  
  132.                if (result)
  133.                {
  134.                   break;
  135.                }
  136.             }
  137.             else if (ent.isSymlink())
  138.             {
  139.                if (ent.leafName.toLowerCase() == fname.toLowerCase())
  140.                {
  141.                   result = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  142.                   result.followLinks = true;
  143.                   result.initWithPath(ent.path);
  144.  
  145.                   if (result.target == "")
  146.                   {
  147.                      result = null;
  148.                   }
  149.                   else
  150.                   {
  151.                      break;
  152.                   }
  153.                }
  154.             }
  155.             else if (ent.isFile())
  156.             {
  157.                if (ent.leafName.toLowerCase() == fname.toLowerCase())
  158.                {
  159.                   result = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  160.                   result.initWithPath(ent.path);
  161.                   break;
  162.                }
  163.             }
  164.          }
  165.       }
  166.    }
  167.    catch (ar)
  168.    {
  169.       return(null);
  170.    }
  171.  
  172.    return( result );
  173. }
  174.  
  175.  
  176. function ievtrim(st) 
  177. {
  178.   result = st.replace( /^\s+/g, "" );
  179.   result = result.replace( /\s+$/g, "" );
  180.   return(result);
  181. }
  182.  
  183.  
  184. function deQuote(st)
  185. {
  186.     var result = ievtrim(st);
  187.     
  188.     if ((result.length >= 2) &&
  189.         (result.charAt(0) == '"') &&
  190.         (result.charAt(result.length - 1) == '"') 
  191.        ) 
  192.     {
  193.         result = ievtrim(result.substr(1, result.length - 2));
  194.     }
  195.     
  196.     return(result);
  197. }
  198.  
  199.  
  200.  
  201. function ieViewLaunch (path,argumentstext)
  202. {
  203.     var cantMessage = "can't find";
  204.  
  205.     try
  206.     {
  207.         cantMessage = gIeViewBundle.getString("ieview.cantFindExplorer");
  208.     }
  209.     catch(e)
  210.     {
  211.         alert(e);
  212.     }
  213.  
  214.   var prefservice = Components.classes["@mozilla.org/preferences-service;1"].
  215.                 getService(Components.interfaces.nsIPrefService);   
  216.  
  217.   var prefs = prefservice.getBranch("");
  218.  
  219.    try{
  220.       if(path=="") return false;
  221.  
  222.       var ieloc = null;
  223.  
  224.       if (prefs.getPrefType("ieview.ieapp") == prefs.PREF_STRING)
  225.       {
  226.         ieloc = deQuote(prefs.getCharPref("ieview.ieapp"));
  227.       }
  228.  
  229.       var   natTarget = null;
  230.       var   usePath = null;
  231.  
  232.       if (ieloc != null)
  233.       {
  234.          natTarget = ieloc;
  235.       }
  236.       else
  237.       {
  238.          var dsprops = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties);
  239.  
  240.          usePath = tryDir(dsprops, userPrograms);    // try user-specific program menu first
  241.  
  242.          var file = null;
  243.  
  244.          if (usePath != "")
  245.          {
  246.             file = searchPath(usePath, path);
  247.          }
  248.  
  249.          if (! file)
  250.          {
  251.             usePath = tryDir(dsprops, allUserPrograms);   // no joy?  try "all users" program menu
  252.  
  253.             if (usePath != "")
  254.             {
  255.                file = searchPath(usePath, path);
  256.             }
  257.          }
  258.  
  259.          if (! file)
  260.          {
  261.             usePath = tryDir(dsprops, applicationData);   // last resort, check the "quick start" bar
  262.  
  263.             if (usePath != "")
  264.             {
  265.                var   quickPath = "\\microsoft\\internet explorer\\quick launch";
  266.  
  267.                usePath = usePath + quickPath;
  268.  
  269.                file = searchPath(usePath, path);
  270.  
  271.                if (! file)    // check alternate QuickLaunch bar title
  272.                {
  273.                   var launchLink = "Launch Internet Explorer Browser.lnk";
  274.  
  275.                   file = searchPath(usePath, launchLink);
  276.                }
  277.             }
  278.          }
  279.  
  280.          // last ditch -- find the windows directory
  281.          // assume that the main Program Files directory is on the same drive
  282.          // look in there, under Program Files\Internet Explorer, for iexplore.exe
  283.          //
  284.          if (! file)
  285.          {
  286.             var usePath = tryDir(dsprops, "WinD");
  287.  
  288.             if ((usePath != "") && (usePath.charAt(1) == ":"))
  289.             {
  290.                usePath = usePath.substr(0, 2) + "\\program files\\internet explorer";
  291.  
  292.                file = searchPath(usePath, "iexplore.exe");
  293.             }
  294.          }
  295.  
  296.          if ((! file) || (! file.exists()))
  297.          {
  298.             alert(cantMessage);
  299.             return false;
  300.          }
  301.  
  302.          natTarget = file.target;
  303.       }
  304.  
  305.       var targetFile = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  306.  
  307.       try
  308.       {
  309.         targetFile.initWithPath(natTarget);
  310.       }
  311.       catch(e)
  312.       {
  313.     alert(cantMessage);
  314.     return(false);
  315.       }
  316.  
  317.       if (! targetFile.exists())
  318.       {
  319.          alert(cantMessage);
  320.          return(false);
  321.       }
  322.  
  323.       usePath = targetFile.target;
  324.  
  325.       var process = Components.classes['@mozilla.org/process/util;1'].getService(Components.interfaces.nsIProcess);
  326.       process.init(targetFile);
  327.       var arguments= [] ;
  328.  
  329.       arguments.push(argumentstext);
  330.  
  331.       process.run(false, arguments, arguments.length, {});
  332.       return true;
  333.  
  334.    }catch(e){ 
  335.       alert(e);return false;
  336.    }
  337.  
  338.    return false;        // avoid JavaScript Error.
  339. }
  340.  
  341.  
  342. function ieviewInit() {
  343.   var menu = document.getElementById("contentAreaContextMenu");
  344.   menu.addEventListener("popupshowing",ieviewContext,false);
  345.  
  346.   gIeViewBundle = document.getElementById("bundle_ieview");
  347.  
  348.   if (document.location.href.substring(0, 7) != 'chrome:')
  349.   {
  350.     if (! gIeViewBundle)
  351.     {
  352.       alert("no bundle");
  353.     }
  354.   }
  355. }
  356.  
  357. function setIeviewOptions()
  358. {
  359.     if (document.getElementById('ieloc').value)
  360.     {
  361.        var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  362.                                .getService(Components.interfaces.nsIPrefService);
  363.  
  364.        var prefs = prefService.getBranch("");
  365.  
  366.          prefs.setCharPref("ieview.ieapp", document.getElementById('ieloc').value);
  367.     }
  368.  
  369.     window.close();
  370. }
  371.  
  372. function pickIe()
  373. {
  374.     var picker = Components.classes["@mozilla.org/filepicker;1"].getService(Components.interfaces.nsIFilePicker);
  375.  
  376.     picker.init(window, "Choose Browser", 0);
  377.     picker.appendFilters(64);
  378.     
  379.     if (picker.show() == 0)
  380.     {
  381.         document.getElementById('ieloc').value = picker.file.target;
  382.     }
  383. }
  384.  
  385.  
  386. function initPath()
  387. {
  388.     var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);   
  389.     
  390.     var prefs = prefservice.getBranch("");
  391.  
  392.     var ieloc = null;
  393.  
  394.     if (prefs.getPrefType("ieview.ieapp") == prefs.PREF_STRING)
  395.     {
  396.       ieloc = prefs.getCharPref("ieview.ieapp");
  397.     }
  398.  
  399.     if ((ieloc != null) && (ieloc.length > 0)) 
  400.     {
  401.         document.getElementById('ieloc').value = ieloc;
  402.     }
  403.  
  404.     prefs = null;
  405. }
  406.  
  407. // do the init on load
  408. window.addEventListener("load", ieviewInit, false); 
  409.  
  410.