home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 February / WN_129_CD.iso / Windows / Extensions Firefox / Text Plain / textplain.xpi / chrome / textplain.jar / content / textplain / TextPlain.js < prev    next >
Encoding:
JavaScript  |  2004-07-26  |  7.6 KB  |  193 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is text/plain.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Gilles Durys.
  18.  * Portions created by the Initial Developer are Copyright (C) 2003
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *        Gilles Durys <mozbug@durys.net>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37. var TextPlain = function() {
  38.     this.userSelection = null;
  39. }
  40.  
  41. TextPlain.urlItems = new Array("openInCurrent", "openInNewWindow", "openInNewTab", "textplainSepUrl", "bookmarkSelectionTarget", "saveSelectionTarget", "textplainSepSave");
  42. TextPlain.mailItems = new Array("composeMail", "addEmailToAddressBook", "textplainSepMail");
  43. TextPlain.menuItems = new Array(TextPlain.urlItems, TextPlain.mailItems);
  44. TextPlain.notFirefoxItems = new Array("addEmailToAddressBook");
  45. try {
  46.     TextPlain.hasIntegratedMailClient = ("@mozilla.org/messengercompose/composeparams;1" in Components.classes);
  47. } catch (e){
  48.     TextPlain.hasIntegratedMailClient = false;
  49. }
  50.  
  51. TextPlain.prototype.initUserSelection = function() {
  52.     var node = document.popupNode;
  53.     var selection = "";
  54.     var nodeLocalName = node.localName.toUpperCase();
  55.     if ((nodeLocalName == "TEXTAREA") ||
  56.         (nodeLocalName == "INPUT" && (node.type == "text" || node.type == "password"))) {
  57.         selection = node.value.substring(node.selectionStart, node.selectionEnd);
  58.     } else if (nodeLocalName == "OPTION") {
  59.         var parentSelect = node.parentNode;
  60.         if (parentSelect.localName.toUpperCase() == "SELECT") {
  61.             if (parentSelect.multiple) {
  62.                 var anOption;
  63.                 for (var i = 0; i < parentSelect.options.length; i++) {
  64.                     anOption = parentSelect.options[i];
  65.                     if (anOption.selected) {
  66.                         selection += " " + anOption.value;
  67.                     }
  68.                 }
  69.             } else {
  70.                 selection = node.value;
  71.             }
  72.         }
  73.     } else if (nodeLocalName == "SELECT") {
  74.         selection = node.options[node.selected].value;
  75.     } else {
  76.         var focusedWindow = document.commandDispatcher.focusedWindow;
  77.         selection = focusedWindow.__proto__.getSelection.call(focusedWindow).toString();
  78.     }
  79.     selection = selection.toString();
  80.     selection = selection.replace(/(^\s+)|(\s+$)/gm, "");
  81.     selection = selection.replace(/(\n|\r|\t)+/g, "");
  82.     this.userSelection = selection;
  83. }
  84.  
  85. TextPlain.prototype.isSelectionURL = function() {
  86.     if (this.userSelection.indexOf("mailto:") == 0)
  87.         return false;
  88.     var RE = /^((((https?|ttps?|tps?|ftp|hxxps?):\/\/)?([\w\-\.]+:.+@)?([\w\-]+(\.[\w\-]+)+)(:\d{1,5})?(\/[\S]*)?)|((https?|ttps?|tps?|ftp|hxxps?):\/\/([\w\-\.]+(:.+)?@)?[\w\-]+?(:\d{1,5})?[\S]*))$/i;
  89.     return RE.test(this.userSelection);
  90. }
  91.  
  92. TextPlain.prototype.isSelectionMail = function() {
  93.     var RE = /^(mailto:)?(([\w\.\-]+@[\w\.\-]*)|(\s*[\w\.\-]+?\s*at\s*[\w\-]+?(\s*dot\s*[\w\-]+)+))$/i;
  94.     return RE.test(this.userSelection);
  95. }
  96.  
  97. TextPlain.prototype.getSelectionAsURL = function() {
  98.     if (this.isSelectionURL()) {
  99.         return TextPlain.addDefaultProtocol(this.userSelection);
  100.     } else {
  101.         return null;
  102.     }
  103. }
  104.  
  105. TextPlain.prototype.getSelectionAsMail = function() {
  106.     if (this.isSelectionMail()) {
  107.         var selectionMail = this.userSelection;
  108.         if (this.userSelection.indexOf("mailto:") == 0) {
  109.             selectionMail = selectionMail.substring(7);
  110.         }
  111.         if (selectionMail.indexOf("@") == -1) {
  112.             // user at host dot com
  113.               selectionMail = selectionMail.replace(/^\s*(.*)/, "$1");
  114.               selectionMail = selectionMail.replace(/\s*\ at\ \s*/gi, "@");
  115.               selectionMail = selectionMail.replace(/\s*\ dot\ \s*/gi, ".");
  116.         }
  117.         return selectionMail;
  118.     } else {
  119.         return null;
  120.     }
  121. }
  122.  
  123. TextPlain.prototype.openSelectionInCurrent = function() {
  124.     TextPlain.getLatestBrowser().loadURI(this.getSelectionAsURL());
  125. }
  126.  
  127. TextPlain.prototype.openSelectionInNewWindow = function() {
  128.     window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no", this.getSelectionAsURL());
  129. }
  130.  
  131. TextPlain.prototype.openSelectionInNewTab = function(event) {
  132.     var reverse = false;
  133.     if (event) {
  134.         reverse = event.shiftKey;
  135.     }
  136.  
  137.     var theBrowser = TextPlain.getLatestBrowser();
  138.     var tab = theBrowser.addTab(this.getSelectionAsURL(), null);
  139.  
  140.     var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  141.     if (pref) {
  142.         var loadInBackground = pref.getBoolPref("browser.tabs.loadInBackground");
  143.         if (reverse) {
  144.             loadInBackground = !loadInBackground;
  145.         }
  146.         if (!loadInBackground) {
  147.             theBrowser.selectedTab = tab;
  148.         }
  149.     }
  150. }
  151.  
  152. TextPlain.prototype.bookmarkSelectionTarget = function() {
  153.     var selectionAsURL = this.getSelectionAsURL();
  154.     BookmarksUtils.addBookmark(selectionAsURL, selectionAsURL, undefined, true);
  155. }
  156.  
  157. TextPlain.prototype.saveSelectionTarget = function() {
  158.     saveURL(this.getSelectionAsURL(), null, null, true);
  159. }
  160.  
  161. TextPlain.prototype.composeMail = function() {
  162.     TextPlain.getLatestBrowser().loadURI("mailto:" + this.getSelectionAsMail());
  163. }
  164.  
  165. TextPlain.prototype.addEmailToAddressBook = function() {
  166.     window.openDialog("chrome://messenger/content/addressbook/abNewCardDialog.xul",
  167.                       "",
  168.                       "chrome,titlebar,resizable=no",
  169.                      {primaryEmail:this.getSelectionAsMail()});
  170. }
  171.  
  172. TextPlain.addDefaultProtocol = function(str) {
  173.     // ftp urls
  174.     if (str.indexOf("ftp://") == 0) {
  175.         return str;
  176.     }
  177.     // http and alike
  178.     var RE = /^(?:(?:http|ttp|tp|hxxp)(s?):\/\/)?(.*)$/i;
  179.     return str.replace(RE, "http$1://$2");
  180. }
  181.  
  182. TextPlain.getLatestBrowser = function() {
  183.     var latestBrowser = null;
  184.     var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  185.                                    .getService(Components.interfaces.nsIWindowMediator);
  186.     var browserWindow = windowMediator.getMostRecentWindow("navigator:browser");
  187.  
  188.     if (browserWindow) {
  189.         latestBrowser = browserWindow.getBrowser();
  190.     }
  191.     return latestBrowser;
  192. }
  193.