home *** CD-ROM | disk | FTP | other *** search
Wrap
/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is text/plain. * * The Initial Developer of the Original Code is * Gilles Durys. * Portions created by the Initial Developer are Copyright (C) 2003 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Gilles Durys <mozbug@durys.net> * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ var TextPlain = function() { this.userSelection = null; } TextPlain.urlItems = new Array("openInCurrent", "openInNewWindow", "openInNewTab", "textplainSepUrl", "bookmarkSelectionTarget", "saveSelectionTarget", "textplainSepSave"); TextPlain.mailItems = new Array("composeMail", "addEmailToAddressBook", "textplainSepMail"); TextPlain.menuItems = new Array(TextPlain.urlItems, TextPlain.mailItems); TextPlain.notFirefoxItems = new Array("addEmailToAddressBook"); try { TextPlain.hasIntegratedMailClient = ("@mozilla.org/messengercompose/composeparams;1" in Components.classes); } catch (e){ TextPlain.hasIntegratedMailClient = false; } TextPlain.prototype.initUserSelection = function() { var node = document.popupNode; var selection = ""; var nodeLocalName = node.localName.toUpperCase(); if ((nodeLocalName == "TEXTAREA") || (nodeLocalName == "INPUT" && (node.type == "text" || node.type == "password"))) { selection = node.value.substring(node.selectionStart, node.selectionEnd); } else if (nodeLocalName == "OPTION") { var parentSelect = node.parentNode; if (parentSelect.localName.toUpperCase() == "SELECT") { if (parentSelect.multiple) { var anOption; for (var i = 0; i < parentSelect.options.length; i++) { anOption = parentSelect.options[i]; if (anOption.selected) { selection += " " + anOption.value; } } } else { selection = node.value; } } } else if (nodeLocalName == "SELECT") { selection = node.options[node.selected].value; } else { var focusedWindow = document.commandDispatcher.focusedWindow; selection = focusedWindow.__proto__.getSelection.call(focusedWindow).toString(); } selection = selection.toString(); selection = selection.replace(/(^\s+)|(\s+$)/gm, ""); selection = selection.replace(/(\n|\r|\t)+/g, ""); this.userSelection = selection; } TextPlain.prototype.isSelectionURL = function() { if (this.userSelection.indexOf("mailto:") == 0) return false; 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; return RE.test(this.userSelection); } TextPlain.prototype.isSelectionMail = function() { var RE = /^(mailto:)?(([\w\.\-]+@[\w\.\-]*)|(\s*[\w\.\-]+?\s*at\s*[\w\-]+?(\s*dot\s*[\w\-]+)+))$/i; return RE.test(this.userSelection); } TextPlain.prototype.getSelectionAsURL = function() { if (this.isSelectionURL()) { return TextPlain.addDefaultProtocol(this.userSelection); } else { return null; } } TextPlain.prototype.getSelectionAsMail = function() { if (this.isSelectionMail()) { var selectionMail = this.userSelection; if (this.userSelection.indexOf("mailto:") == 0) { selectionMail = selectionMail.substring(7); } if (selectionMail.indexOf("@") == -1) { // user at host dot com selectionMail = selectionMail.replace(/^\s*(.*)/, "$1"); selectionMail = selectionMail.replace(/\s*\ at\ \s*/gi, "@"); selectionMail = selectionMail.replace(/\s*\ dot\ \s*/gi, "."); } return selectionMail; } else { return null; } } TextPlain.prototype.openSelectionInCurrent = function() { TextPlain.getLatestBrowser().loadURI(this.getSelectionAsURL()); } TextPlain.prototype.openSelectionInNewWindow = function() { window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no", this.getSelectionAsURL()); } TextPlain.prototype.openSelectionInNewTab = function(event) { var reverse = false; if (event) { reverse = event.shiftKey; } var theBrowser = TextPlain.getLatestBrowser(); var tab = theBrowser.addTab(this.getSelectionAsURL(), null); var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); if (pref) { var loadInBackground = pref.getBoolPref("browser.tabs.loadInBackground"); if (reverse) { loadInBackground = !loadInBackground; } if (!loadInBackground) { theBrowser.selectedTab = tab; } } } TextPlain.prototype.bookmarkSelectionTarget = function() { var selectionAsURL = this.getSelectionAsURL(); BookmarksUtils.addBookmark(selectionAsURL, selectionAsURL, undefined, true); } TextPlain.prototype.saveSelectionTarget = function() { saveURL(this.getSelectionAsURL(), null, null, true); } TextPlain.prototype.composeMail = function() { TextPlain.getLatestBrowser().loadURI("mailto:" + this.getSelectionAsMail()); } TextPlain.prototype.addEmailToAddressBook = function() { window.openDialog("chrome://messenger/content/addressbook/abNewCardDialog.xul", "", "chrome,titlebar,resizable=no", {primaryEmail:this.getSelectionAsMail()}); } TextPlain.addDefaultProtocol = function(str) { // ftp urls if (str.indexOf("ftp://") == 0) { return str; } // http and alike var RE = /^(?:(?:http|ttp|tp|hxxp)(s?):\/\/)?(.*)$/i; return str.replace(RE, "http$1://$2"); } TextPlain.getLatestBrowser = function() { var latestBrowser = null; var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"] .getService(Components.interfaces.nsIWindowMediator); var browserWindow = windowMediator.getMostRecentWindow("navigator:browser"); if (browserWindow) { latestBrowser = browserWindow.getBrowser(); } return latestBrowser; }