home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * Copyright (c) 2003 Paul Roub <paul@roub.net>
- *
- * $Header: /cvs/ieview/ieview/content/ieviewOverlay.js,v 1.27 2005/06/02 20:41:15 roub Exp $
- *
- * Portions based on GPLed code by
- * Ted Mielczarek
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * *****
- *
- * Notes:
- *
- * Since we can't know ahead of time where IE will be installed, we Kiboze the
- * Start Menu tree. If the shortcut has been renamed (i.e. it is no longer
- * titled "Internet Explorer"), we're out of luck. The only option at that
- * point would be to open and dereference every symlink we find, and see if the
- * target leaf name is "iexplore.exe". Not doing that at the moment, since
- * it just seems insane.
- */
-
- var gIeViewBundle;
- var gOldOpenOneBookmark = null;
-
- function ieviewAlert(msg)
- {
- // alert(msg);
- }
-
- var IeView = {
-
- ieviewMenuItems: new Array("ieview-do-view", "ieview-do-forcepage"),
- ieviewLinkMenuItems: new Array("ieview-do-viewlink", "ieview-do-forcelink", "ieview-do-forcelink-menu"),
- userPrograms: "Progs",
- allUserPrograms: "CmPrgs",
- applicationData: "AppData",
-
- isJsLink: function(href)
- {
- return(! this.isForceable(href));
- },
-
- enableForceIE: function()
- {
- var enabled = true;
- var pref = this.getBoolPref("ieview.disableForce", false);
-
- if (pref)
- {
- enabled = false;
- }
-
- return(enabled);
- },
-
- ieviewContext: function()
- {
- if (gContextMenu)
- {
- var i;
-
- for ( i=0; i < this.ieviewMenuItems.length; i++)
- {
- var menuitem = document.getElementById(this.ieviewMenuItems[i]);
-
- if (menuitem) // click-on-page item
- {
- menuitem.hidden = (gContextMenu.isTextSelected || gContextMenu.onLink || gContextMenu.onImage || gContextMenu.onTextInput ) ||
- ((! this.enableForceIE()) && (this.ieviewMenuItems[i].indexOf("force") >= 0));
- }
- }
-
- for ( i = 0; i < this.ieviewLinkMenuItems.length; ++i )
- {
- var menuitem = document.getElementById(this.ieviewLinkMenuItems[i]);
-
- if (menuitem) // click-on-link item
- {
- menuitem.hidden = (! gContextMenu.onLink) || // no link, no link item
- ((! this.enableForceIE()) && (this.ieviewMenuItems[i].indexOf("force") >= 0));
-
- // disable the link if it's javascript
- //
- var disable = ((! menuitem.hidden) && this.isJsLink(gContextMenu.linkURL()));
-
- if (disable)
- menuitem.setAttribute('disabled', 'true');
- else
- menuitem.setAttribute('disabled', 'false');
- }
- }
- }
- },
-
- ieView: function()
- {
- var href = gBrowser.currentURI.spec;
-
- this.ieViewLaunch("Internet Explorer.lnk", href);
- },
-
-
- ieViewLink: function()
- {
- if (gContextMenu)
- {
- var href = gContextMenu.linkURL();
-
- this.ieViewLaunch("Internet Explorer.lnk", href);
- }
- },
-
- saveIeLoc: function(path)
- {
- var prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
-
- var prefs = prefService.getBranch("");
-
- prefs.setCharPref("ieview.ieapp", path);
- },
-
- confirmAdd: function(href)
- {
- var alwaysMsg = "Will always use IE to open URLs beginning with";
-
- try
- {
- alwaysMessage = gIeViewBundle.getString("ieview.alwaysopenwith");
- }
- catch(e)
- {
- alert(e);
- return(false);
- }
-
- alwaysMessage += " " + href;
-
- return( confirm(alwaysMessage) );
- },
-
- addForce: function(href)
- {
- var root = href;
-
- var cls = Components.classes["@mozilla.org/network/io-service;1"];
- var srv = cls.getService(Components.interfaces.nsIIOService);
-
- var uri = false;
-
- try
- {
- uri = srv.newURI(href, null, null);
- }
- catch(e)
- {
- uri = false;
- }
-
- if (uri && uri.prePath && uri.username && uri.userPass)
- {
- uri.username = '';
- uri.userPass = '';
-
- root = uri.prePath;
- }
-
- if (! this.confirmAdd(root))
- {
- return(false);
- }
-
- var forces = this.getForceList();
- forces[forces.length] = root;
- var forceStr = forces.join(" ");
-
- var prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
-
- var prefs = prefService.getBranch("");
-
- prefs.setCharPref("ieview.forceielist", forceStr);
-
- return(true);
- },
-
-
- forceLink: function()
- {
- if (! gContextMenu)
- {
- return;
- }
-
- var href = gContextMenu.linkURL();
-
- this.addForce(href);
-
- this.ieViewLink();
- },
-
- forcePage: function()
- {
- var href = gBrowser.currentURI.spec;
-
- if (this.addForce(href))
- {
- this.ieView();
- }
- },
-
-
- // attempt to grab the real path of a predefined directory
- //
- tryDir: function(dsp, key)
- {
- try
- {
- var nif = dsp.get(key, Components.interfaces.nsIFile);
- return(nif.path);
- }
- catch (ar)
- {
- return("");
- }
- },
-
-
- searchPath: function(path, fname)
- {
- var result = null;
-
- try
- {
- var f = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
-
- f.initWithPath(path);
-
- if (f.exists() && f.isDirectory())
- {
- var entries = f.directoryEntries;
-
- while (entries.hasMoreElements())
- {
- var ent = entries.getNext().QueryInterface(Components.interfaces.nsIFile);
-
- if (ent.isDirectory())
- {
- result = this.searchPath(ent.path, fname);
-
- if (result)
- {
- break;
- }
- }
- else if (ent.isSymlink())
- {
- if (ent.leafName.toLowerCase() == fname.toLowerCase())
- {
- result = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
- result.followLinks = true;
- result.initWithPath(ent.path);
-
- if (result.target == "")
- {
- result = null;
- }
- else
- {
- break;
- }
- }
- }
- else if (ent.isFile())
- {
- if (ent.leafName.toLowerCase() == fname.toLowerCase())
- {
- result = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
- result.initWithPath(ent.path);
- break;
- }
- }
- }
- }
- }
- catch (ar)
- {
- return(null);
- }
-
- return( result );
- },
-
-
- ievtrim: function(st)
- {
- result = st.replace( /^\s+/g, "" );
- result = result.replace( /\s+$/g, "" );
- return(result);
- },
-
-
- deQuote: function(st)
- {
- var result = this.ievtrim(st);
-
- if ((result.length >= 2) &&
- (result.charAt(0) == '"') &&
- (result.charAt(result.length - 1) == '"')
- )
- {
- result = this.ievtrim(result.substr(1, result.length - 2));
- }
-
- return(result);
- },
-
- getBoolPref: function(prefName, defval)
- {
- var result = defval;
-
- var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
- var prefs = prefservice.getBranch("");
-
- if (prefs.getPrefType(prefName) == prefs.PREF_BOOL)
- {
- result = prefs.getBoolPref(prefName);
- }
-
- return(result);
- },
-
- getForceList: function()
- {
- var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
-
- var prefs = prefservice.getBranch("");
- var forces = new Array();
-
- if (prefs.getPrefType("ieview.forceielist") == prefs.PREF_STRING)
- {
- var forceList = this.deQuote(prefs.getCharPref("ieview.forceielist"));
- forces = forceList.split(" ");
-
- var msg = "force list:\r\n";
-
- var i;
-
- for (i = 0; i < forces.length; ++i )
- {
- msg += forces[i] + "\r\n";
- }
- }
-
- return(forces);
- },
-
-
- ieViewLaunch: function (path,argumentstext)
- {
- var cantMessage = "can't find";
-
- if (! this.isForceable(argumentstext))
- {
- return;
- }
-
- try
- {
- cantMessage = gIeViewBundle.getString("ieview.cantFindExplorer");
- }
- catch(e)
- {
- alert(e);
- }
-
- var prefservice = Components.classes["@mozilla.org/preferences-service;1"].
- getService(Components.interfaces.nsIPrefService);
-
- var prefs = prefservice.getBranch("");
-
- try
- {
- if (path=="") return false;
-
- var ieloc = null;
-
- if (prefs.getPrefType("ieview.ieapp") == prefs.PREF_STRING)
- {
- ieloc = this.deQuote(prefs.getCharPref("ieview.ieapp"));
-
- if (this.trim(ieloc) == "")
- {
- ieloc = null;
- }
- }
-
- var natTarget = null;
- var usePath = null;
-
- if (ieloc != null)
- {
- natTarget = ieloc;
- }
- else
- {
- var dsprops = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties);
-
- usePath = this.tryDir(dsprops, this.userPrograms); // try user-specific program menu first
-
- var file = null;
-
- if (usePath != "")
- {
- file = this.searchPath(usePath, path);
- }
-
- if (! file)
- {
- usePath = this.tryDir(dsprops, this.allUserPrograms); // no joy? try "all users" program menu
-
- if (usePath != "")
- {
- file = this.searchPath(usePath, path);
- }
- }
-
- if (! file)
- {
- usePath = this.tryDir(dsprops, this.applicationData); // last resort, check the "quick start" bar
-
- if (usePath != "")
- {
- var quickPath = "\\microsoft\\internet explorer\\quick launch";
-
- usePath = usePath + quickPath;
-
- file = this.searchPath(usePath, path);
-
- if (! file) // check alternate QuickLaunch bar title
- {
- var launchLink = "Launch Internet Explorer Browser.lnk";
-
- file = this.searchPath(usePath, launchLink);
- }
- }
- }
-
- // last ditch -- find the windows directory
- // assume that the main Program Files directory is on the same drive
- // look in there, under Program Files\Internet Explorer, for iexplore.exe
- //
- if (! file)
- {
- usePath = this.tryDir(dsprops, "WinD");
-
- if ((usePath != "") && (usePath.charAt(1) == ":"))
- {
- usePath = usePath.substr(0, 2) + "\\program files\\internet explorer";
-
- file = this.searchPath(usePath, "iexplore.exe");
- }
- }
-
- if ((! file) || (! file.exists()))
- {
- alert(cantMessage);
- return false;
- }
-
- natTarget = file.target;
- this.saveIeLoc(natTarget);
- }
-
- var targetFile = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
-
- try
- {
- targetFile.initWithPath(natTarget);
- }
- catch(e)
- {
- alert(cantMessage);
- return(false);
- }
-
- if (! targetFile.exists())
- {
- alert(cantMessage);
- return(false);
- }
-
- usePath = targetFile.target;
-
- var process = Components.classes['@mozilla.org/process/util;1'].getService(Components.interfaces.nsIProcess);
- process.init(targetFile);
- var arguments= [] ;
-
- arguments.push(argumentstext);
-
- process.run(false, arguments, arguments.length,{});
- return true;
-
- }
- catch (e)
- {
- alert(e);
- return false;
- }
-
- return false; // avoid JavaScript Error.
- },
-
-
- ieviewInit: function()
- {
- var i;
- var menu = document.getElementById("contentAreaContextMenu");
-
- if (menu)
- {
- menu.addEventListener("popupshowing", ieviewContextListener,false);
- }
-
- gIeViewBundle = document.getElementById("bundle_ieview");
-
- var docHref = this.getDocHref();
-
- if (docHref == "")
- {
- docHref = document.location.href;
- }
-
- if (docHref.substring(0, 7) != 'chrome:')
- {
- if (! gIeViewBundle)
- {
- alert("no bundle");
- }
- }
-
- var appcontent = document.getElementById("appcontent"); // browser
-
- if (appcontent)
- {
- appcontent.addEventListener("load", ieviewLoadListener, true);
- }
- },
-
- isForceable: function(href) {
- return( (typeof(href) != 'undefined') && (href.substr) &&
- ((href.substr(0, 4) == 'http') || (href.substr(0, 7) == 'file://'))
- );
- },
-
- hideMenu: function(aEvent)
- {
- var i;
- var doc = aEvent.originalTarget;
-
- var menuitem = document.getElementById('ieview-do-forcepage-menu');
-
- if (! menuitem)
- {
- menuitem = doc.getElementById('ieview-do-forcepage-menu');
- }
-
- if (! menuitem)
- {
- menuitem = document.getElementById('ieview-do-forcepage-menu-moz');
- }
-
- if (! menuitem)
- {
- menuitem = doc.getElementById('ieview-do-forcepage-menu-moz');
- }
-
- if (menuitem) // click-on-page item
- {
- var href = '';
-
- if (doc.location && doc.location.href)
- {
- href = doc.location.href;
- }
-
- var enabled = this.enableForceIE() && this.isForceable(href);
-
- menuitem.setAttribute('disabled', enabled ? 'false' : 'true');
-
- }
- },
-
- grabLinks: function(aEvent)
- {
- var i;
- var doc = aEvent.originalTarget;
-
- if ((! this.enableForceIE()) || ! doc.links || ! doc.links.length)
- {
- return;
- }
-
- for (i = 0; i < doc.links.length; ++i)
- {
- var ln = doc.links[i];
-
- if (! ln.onclick)
- {
- ln.onclick = this.ieviewClick;
- }
- }
- },
-
- // check whether the document behind a dom-loaded event is framed
- //
- isFramed: function(aEvent)
- {
- var framed = aEvent.originalTarget &&
- window._content && window._content.document &&
- (window._content.document != aEvent.originalTarget);
-
- return(framed);
- },
-
-
- checkForced: function(aEvent)
- {
- var i;
- var doc = aEvent.originalTarget;
-
- if (! this.enableForceIE())
- {
- return;
- }
-
- // only want to reload the topmost frame
- //
- if (this.isFramed(aEvent))
- {
- return;
- }
-
- if (doc.location && doc.location.href && this.forceIe(doc.location.href))
- {
- var ws = gBrowser.docShell.QueryInterface(Components.interfaces.nsIRefreshURI);
-
- if (ws)
- {
- ws.cancelRefreshURITimers();
- }
-
- doc.location.href = "chrome://ieview/content/reloaded.html";
-
- var head = null;
-
- var heads = doc.getElementsByTagName('head');
-
- if (heads && heads.length > 0)
- {
- head = heads[0];
- }
- else
- {
- heads = doc.getElementsByTagName('HEAD');
-
- if (heads && heads.length > 0)
- {
- head = heads[0];
- }
- }
-
- if (head)
- {
- head.innerHTML = "<title>IE View " + gIeViewBundle.getString("ieview.reloaded") + "</title>";
- }
-
- this.ieViewLaunch("Internet Explorer.lnk", doc.location.href);
- }
- },
-
- startsWith: function(st, pref)
- {
- return( (pref.length > 0) && (st.substring(0, pref.length) == pref) );
-
- },
-
- isMatch: function(url, pattern)
- {
- if ((pattern == null) || (pattern == ""))
- {
- return(false);
- }
-
- var repat = pattern;
-
- repat = repat.replace(/\\/, "\\\\");
- repat = repat.replace(/\./g, "\\.");
- repat = repat.replace(/\//g, "\\/");
- repat = repat.replace(/\*/g, ".*");
- repat = repat.replace(/\.\*\\\./g, ".*\\.?\\b");
- repat = "^" + repat;
-
- // document.write("\n " + pattern + " - " + repat + " <br />\n");
-
- var reg = new RegExp(repat);
-
- var matched = (reg.test(url));
-
- return(matched);
- },
-
- forceIe: function(href)
- {
- var skipList = this.getForceList();
-
- for (i = 0; i < skipList.length; ++i)
- {
- if (this.isMatch(href, skipList[i]))
- {
- ieviewAlert(href + " matches " + skipList[i]);
- return(true);
- }
- }
-
- return(false);
- },
-
- ieviewClick: function(aEvent)
- {
- var link = aEvent.originalTarget;
-
- if ((! link) || (! link.href))
- {
- link = aEvent.currentTarget;
- }
-
- if (IeView.forceIe(link.href))
- {
- IeView.ieViewLaunch("Internet Explorer.lnk", link.href);
- return(false);
- }
-
- return(true);
- },
-
- setIeviewOptions: function()
- {
- var prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
-
- var prefs = prefService.getBranch("");
-
- prefs.setCharPref("ieview.ieapp", document.getElementById('ieloc').value);
-
- prefs.setCharPref("ieview.forceielist", this.getPrefListString());
-
- window.close();
- },
-
- pickIe: function()
- {
- var picker = Components.classes["@mozilla.org/filepicker;1"].getService(Components.interfaces.nsIFilePicker);
-
- picker.init(window, "Choose Browser", 0);
- picker.appendFilters(64);
-
- if (picker.show() == 0)
- {
- document.getElementById('ieloc').value = picker.file.target;
- }
- },
-
-
- initPath: function()
- {
- var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
-
- var prefs = prefservice.getBranch("");
-
- var ieloc = null;
-
- if (prefs.getPrefType("ieview.ieapp") == prefs.PREF_STRING)
- {
- ieloc = prefs.getCharPref("ieview.ieapp");
- }
-
- if ((ieloc != null) && (ieloc.length > 0))
- {
- document.getElementById('ieloc').value = ieloc;
- }
-
- var skips = this.getForceList();
-
- var listbox = document.getElementById('patlist');
-
- if (! listbox)
- {
- alert('no listbox');
- }
- else
- {
- var i;
-
- for ( i = 0; i < skips.length; ++i )
- {
- if (skips[i] != '')
- {
- listbox.appendItem(skips[i]);
- }
- }
- }
-
- prefs = null;
-
- var np = document.getElementById('newpat');
-
- np.disabled = ! this.enableForceIE();
- listbox.disabled = ! this.enableForceIE();
- },
-
- updateDelButtonStatus: function()
- {
- var lb = document.getElementById('patlist');
- var bt = document.getElementById('delpat');
-
- bt.disabled = (lb.selectedItems.length < 1) || ! this.enableForceIE();
- },
-
- compareNum: function(a,b)
- {
- return(b-a);
- },
-
- deleteSelected: function()
- {
- var lb = document.getElementById('patlist');
- var selected = lb.selectedItems;
- var indexes = new Array();
-
- var i;
-
- for ( i = 0; i < selected.length; ++i )
- {
- var item = selected[i];
-
- var idx = lb.getIndexOfItem(item);
-
- indexes[i] = idx;
- }
-
- indexes.sort(this.compareNum);
-
- for ( i = 0; i < indexes.length; ++i )
- {
- lb.removeItemAt(indexes[i]);
- }
-
- this.updateDelButtonStatus();
- },
-
-
- updateButtonStatus: function()
- {
- var tb = document.getElementById('newpat');
- var bt = document.getElementById('addpat');
-
- bt.disabled = (this.trim(tb.value).length < 1);
- },
-
- addNewPat: function()
- {
- var tb = document.getElementById('newpat');
-
- var newstr = tb.value;
-
- if (newstr.indexOf("://") < 0)
- {
- newstr = "http://" + newstr;
- }
-
- var listbox = document.getElementById('patlist');
-
- listbox.appendItem(this.trim(newstr));
-
- tb.value = "";
- this.updateButtonStatus();
- },
-
- getPrefListString: function()
- {
- var result = "";
-
- var listbox = document.getElementById('patlist');
- var rows = listbox.getRowCount();
- var i;
-
- for ( i = 0; i < rows; ++i )
- {
- var li = listbox.getItemAtIndex(i);
-
- if (result != "")
- {
- result += " ";
- }
-
- result += li.label;
- }
-
- return(result);
- },
-
- trim: function(st)
- {
- var result = st;
-
- while ((result.length > 0) && (result.substring(0,1) == " "))
- {
- result = result.substring(1);
- }
-
- while ((result.length > 0) && (result.substring(st.length - 1, 1) == " "))
- {
- result = result.substring(0, result.length - 1);
- }
-
- return(result);
- },
-
- getDocHref: function()
- {
- if (typeof(XPCNativeWrapper) == "undefined")
- {
- return("");
- }
-
- var winWrapper = new XPCNativeWrapper(window._content, 'document');
- var docWrapper = new XPCNativeWrapper(winWrapper.document, 'location');
- var locWrapper = new XPCNativeWrapper(docWrapper.location, 'href');
-
- return(locWrapper.href);
- },
- };
-
-
- function ieviewLaunchOptions()
- {
- window.openDialog("chrome://ieview/content/ieviewsettings.xul", "ieviewsettings",
- "resizable,centerscreen,modal");
- }
-
- function ieviewContextListener(aEvent)
- {
- return(IeView.ieviewContext(aEvent));
- }
-
- function ieviewLoadListener(aEvent)
- {
- IeView.hideMenu(aEvent);
-
- return(IeView.grabLinks(aEvent));
- }
-
- function ieviewCheckForcedListener(aEvent)
- {
- IeView.checkForced(aEvent);
- }
-
- function ieviewInitListener(aEvent)
- {
- return(IeView.ieviewInit(aEvent));
- }
-
- // do the init on load
- window.addEventListener("load", ieviewInitListener, false);
- window.addEventListener("DOMContentLoaded", ieviewCheckForcedListener, false);
-
- if (typeof(BookmarksCommand) != "undefined")
- {
- if (BookmarksCommand.openOneBookmark && ! gOldOpenOneBookmark && IeView.enableForceIE())
- {
- gOldOpenOneBookmark = BookmarksCommand.openOneBookmark;
-
- BookmarksCommand.openOneBookmark = function(aURI, aTargetBrowser, aDS) {
-
- var namespaceVar = null;
-
- if (typeof(gNC_NS) != "undefined")
- {
- namespaceVar = gNC_NS;
- }
- else if (typeof(NC_NS) != "undefined")
- {
- namespaceVar = NC_NS;
- }
- else
- {
- alert('no namespace var');
- }
-
- var url = BookmarksUtils.getProperty(aURI, namespaceVar+"URL", aDS);
-
- if (IeView.forceIe(url))
- {
- IeView.ieViewLaunch("Internet Explorer.lnk", url);
- return;
- }
- else
- {
- gOldOpenOneBookmark(aURI, aTargetBrowser, aDS);
- }
- };
- }
- }
-
-