2006-07-29 05:38:04 +00:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2006-07-29 05:43:04 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2006-07-29 05:33:12 +00:00
|
|
|
*
|
2006-07-29 05:43:04 +00:00
|
|
|
* 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/
|
2006-07-29 05:33:12 +00:00
|
|
|
*
|
2006-07-29 05:38:04 +00:00
|
|
|
* 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.
|
2006-07-29 05:33:12 +00:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2006-07-29 05:43:04 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2006-07-29 05:38:04 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2006-07-29 05:33:12 +00:00
|
|
|
*
|
2006-07-29 05:38:04 +00:00
|
|
|
* Contributor(s):
|
2006-07-29 05:33:12 +00:00
|
|
|
* Alec Flett <alecf@netscape.com>
|
|
|
|
* Ben Goodger <ben@netscape.com>
|
|
|
|
* Mike Pinkerton <pinkerton@netscape.com>
|
|
|
|
* Blake Ross <blakeross@telocity.com>
|
2006-07-29 05:38:04 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2006-07-29 05:43:04 +00:00
|
|
|
* either of 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"),
|
2006-07-29 05:38:04 +00:00
|
|
|
* 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
|
2006-07-29 05:43:04 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2006-07-29 05:38:04 +00:00
|
|
|
* 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
|
2006-07-29 05:43:04 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2006-07-29 05:38:04 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2006-07-29 05:33:12 +00:00
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
/*
|
|
|
|
* - [ Dependencies ] ---------------------------------------------------------
|
|
|
|
* utilityOverlay.js:
|
|
|
|
* - gatherTextUnder
|
|
|
|
*/
|
|
|
|
|
2006-07-29 05:33:12 +00:00
|
|
|
var pref = null;
|
2006-07-29 05:38:51 +00:00
|
|
|
pref = Components.classes["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIPrefBranch);
|
2006-07-29 05:33:19 +00:00
|
|
|
|
|
|
|
// Prefill a single text field
|
2006-07-29 05:36:20 +00:00
|
|
|
function prefillTextBox(target) {
|
2006-07-29 05:33:19 +00:00
|
|
|
// obtain values to be used for prefilling
|
|
|
|
var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
|
2006-07-29 05:43:14 +00:00
|
|
|
var value = walletService.WALLET_PrefillOneElement(window.content, target);
|
2006-07-29 05:33:19 +00:00
|
|
|
if (value) {
|
|
|
|
|
|
|
|
// result is a linear sequence of values, each preceded by a separator character
|
|
|
|
// convert linear sequence of values into an array of values
|
|
|
|
var separator = value[0];
|
|
|
|
var valueList = value.substring(1, value.length).split(separator);
|
|
|
|
|
|
|
|
target.value = valueList[0];
|
|
|
|
/*
|
|
|
|
* Following code is a replacement for above line. In the case of multiple values, it
|
|
|
|
* presents the user with a dialog containing a list from which he can select the value
|
|
|
|
* he wants. However it is being commented out for now because of several problems, namely
|
|
|
|
*
|
|
|
|
* 1. There is no easy way to put localizable strings for the title and message of
|
|
|
|
* the dialog without introducing a .properties file which currently doesn't exist
|
|
|
|
* 2. Using blank title and messages as shown below have a problem because a zero-length
|
|
|
|
* title is being displayed as some garbage characters (which is why the code below
|
|
|
|
* has a title of " " instead of ""). This is a separate bug which will have to be
|
|
|
|
* investigated further.
|
|
|
|
* 3. The current wallet tables present alternate values for items such as shipping
|
|
|
|
* address -- namely billing address and home address. Up until now, these alternate
|
|
|
|
* values have never been a problem because the preferred value is always first and is
|
|
|
|
* all the user sees when doing a prefill. However now he will be presented with a
|
|
|
|
* list showing all these values and asking him to pick one, even though the wallet
|
|
|
|
* code was clearly able to determine that he meant shipping address and not billing
|
|
|
|
* address.
|
|
|
|
* 4. There is a relatively long delay before the dialog come up whereas values are
|
|
|
|
* filled in quickly when no dialog is involved.
|
|
|
|
*
|
|
|
|
* Once this feature is checked in, a separate bug will be opened asking that the above
|
|
|
|
* problems be examined and this dialog turned on
|
|
|
|
|
|
|
|
if (valueList.length == 1) {
|
|
|
|
// only one value, use it for prefilling
|
|
|
|
target.value = valueList[0];
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// more than one value, have user select the one he wants
|
2006-07-29 05:36:21 +00:00
|
|
|
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
|
|
|
|
promptService = promptService.QueryInterface(Components.interfaces.nsIPromptService);
|
2006-07-29 05:33:19 +00:00
|
|
|
var position = {};
|
|
|
|
var title = " ";
|
|
|
|
var message = "";
|
|
|
|
var ok =
|
2006-07-29 05:36:22 +00:00
|
|
|
promptService.select
|
2006-07-29 05:33:19 +00:00
|
|
|
(window, title, message, valueList.length, valueList, position)
|
|
|
|
if (ok) {
|
|
|
|
target.value = valueList[position.value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
* End of commented out code
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
2006-07-29 05:33:12 +00:00
|
|
|
|
2006-07-29 05:41:38 +00:00
|
|
|
function hrefForClickEvent(event)
|
2006-07-29 05:33:12 +00:00
|
|
|
{
|
2006-07-29 05:33:19 +00:00
|
|
|
var target = event.target;
|
2006-07-29 05:33:12 +00:00
|
|
|
var linkNode;
|
2006-07-29 05:38:12 +00:00
|
|
|
|
|
|
|
var local_name = target.localName;
|
|
|
|
|
|
|
|
if (local_name) {
|
2006-07-29 05:38:55 +00:00
|
|
|
local_name = local_name.toLowerCase();
|
2006-07-29 05:38:12 +00:00
|
|
|
}
|
2006-07-29 05:41:37 +00:00
|
|
|
|
|
|
|
var isKeyPress = (event.type == "keypress");
|
2006-07-29 05:38:12 +00:00
|
|
|
|
|
|
|
switch (local_name) {
|
2006-07-29 05:33:12 +00:00
|
|
|
case "a":
|
|
|
|
case "area":
|
2006-07-29 05:38:56 +00:00
|
|
|
case "link":
|
2006-07-29 05:39:20 +00:00
|
|
|
if (target.hasAttribute("href"))
|
2006-07-29 05:38:56 +00:00
|
|
|
linkNode = target;
|
|
|
|
break;
|
2006-07-29 05:33:19 +00:00
|
|
|
case "input":
|
2006-07-29 05:41:37 +00:00
|
|
|
if ((event.target.type == "text") // text field
|
|
|
|
&& !isKeyPress // not a key event
|
2006-07-29 05:35:46 +00:00
|
|
|
&& event.detail == 2 // double click
|
|
|
|
&& event.button == 0 // left mouse button
|
2006-07-29 05:43:20 +00:00
|
|
|
&& event.target.value.length == 0 // no text has been entered
|
|
|
|
&& "@mozilla.org/wallet/wallet-service;1" in Components.classes // wallet is available
|
|
|
|
) {
|
2006-07-29 05:36:20 +00:00
|
|
|
prefillTextBox(target); // prefill the empty text field if possible
|
2006-07-29 05:33:19 +00:00
|
|
|
}
|
2006-07-29 05:33:12 +00:00
|
|
|
break;
|
|
|
|
default:
|
2006-07-29 05:33:19 +00:00
|
|
|
linkNode = findParentNode(event.originalTarget, "a");
|
2006-07-29 05:39:20 +00:00
|
|
|
// <a> cannot be nested. So if we find an anchor without an
|
|
|
|
// href, there is no useful <a> around the target
|
|
|
|
if (linkNode && !linkNode.hasAttribute("href"))
|
|
|
|
linkNode = null;
|
2006-07-29 05:33:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-07-29 05:41:37 +00:00
|
|
|
var href;
|
2006-07-29 05:33:12 +00:00
|
|
|
if (linkNode) {
|
2006-07-29 05:43:18 +00:00
|
|
|
href = new XPCNativeWrapper(linkNode, "href").href;
|
2006-07-29 05:38:56 +00:00
|
|
|
} else {
|
|
|
|
// Try simple XLink
|
|
|
|
linkNode = target;
|
|
|
|
while (linkNode) {
|
|
|
|
if (linkNode.nodeType == Node.ELEMENT_NODE) {
|
2006-07-29 05:43:18 +00:00
|
|
|
var wrapper = new XPCNativeWrapper(linkNode, "getAttributeNS()");
|
|
|
|
href = wrapper.getAttributeNS("http://www.w3.org/1999/xlink", "href");
|
2006-07-29 05:38:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
linkNode = linkNode.parentNode;
|
|
|
|
}
|
|
|
|
if (href && href != "") {
|
2006-07-29 05:43:18 +00:00
|
|
|
var baseURI = new XPCNativeWrapper(linkNode, "baseURI").baseURI;
|
|
|
|
href = makeURLAbsolute(baseURI, href);
|
2006-07-29 05:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-29 05:41:38 +00:00
|
|
|
return href;
|
|
|
|
}
|
2006-07-29 05:41:37 +00:00
|
|
|
|
2006-07-29 05:41:38 +00:00
|
|
|
// Called whenever the user clicks in the content area,
|
|
|
|
// except when left-clicking on links (special case)
|
|
|
|
// should always return true for click to go through
|
|
|
|
function contentAreaClick(event)
|
|
|
|
{
|
2006-07-29 05:43:19 +00:00
|
|
|
if (!event.isTrusted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:41:38 +00:00
|
|
|
var isKeyPress = (event.type == "keypress");
|
|
|
|
var href = hrefForClickEvent(event);
|
2006-07-29 05:41:37 +00:00
|
|
|
if (href) {
|
|
|
|
if (isKeyPress) {
|
|
|
|
openNewTabWith(href, true, event.shiftKey);
|
|
|
|
event.preventBubble();
|
|
|
|
}
|
|
|
|
else {
|
2006-07-29 05:39:56 +00:00
|
|
|
handleLinkClick(event, href, null);
|
2006-07-29 05:38:56 +00:00
|
|
|
}
|
2006-07-29 05:41:37 +00:00
|
|
|
return true;
|
2006-07-29 05:33:12 +00:00
|
|
|
}
|
2006-07-29 05:41:37 +00:00
|
|
|
|
|
|
|
if (pref && !isKeyPress && event.button == 1 &&
|
2006-07-29 05:43:17 +00:00
|
|
|
!event.getPreventDefault() &&
|
2006-07-29 05:33:19 +00:00
|
|
|
!findParentNode(event.originalTarget, "scrollbar") &&
|
2006-07-29 05:38:09 +00:00
|
|
|
pref.getBoolPref("middlemouse.contentLoadURL")) {
|
2006-07-29 05:39:59 +00:00
|
|
|
if (middleMousePaste(event)) {
|
2006-07-29 05:33:19 +00:00
|
|
|
event.preventBubble();
|
|
|
|
}
|
2006-07-29 05:33:12 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:41:33 +00:00
|
|
|
function openNewTabOrWindow(event, href, sendReferrer)
|
2006-07-29 05:39:58 +00:00
|
|
|
{
|
|
|
|
// should we open it in a new tab?
|
2006-07-29 05:41:35 +00:00
|
|
|
if (pref && pref.getBoolPref("browser.tabs.opentabfor.middleclick")) {
|
2006-07-29 05:41:33 +00:00
|
|
|
openNewTabWith(href, sendReferrer, event.shiftKey);
|
2006-07-29 05:39:58 +00:00
|
|
|
event.preventBubble();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// should we open it in a new window?
|
|
|
|
if (pref && pref.getBoolPref("middlemouse.openNewWindow")) {
|
2006-07-29 05:41:33 +00:00
|
|
|
openNewWindowWith(href, sendReferrer);
|
2006-07-29 05:39:58 +00:00
|
|
|
event.preventBubble();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// let someone else deal with it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:56 +00:00
|
|
|
function handleLinkClick(event, href, linkNode)
|
2006-07-29 05:33:12 +00:00
|
|
|
{
|
2006-07-29 05:41:24 +00:00
|
|
|
// Make sure we are allowed to open this URL
|
|
|
|
urlSecurityCheck(href, document);
|
|
|
|
|
2006-07-29 05:33:12 +00:00
|
|
|
switch (event.button) {
|
2006-07-29 05:35:46 +00:00
|
|
|
case 0: // if left button clicked
|
2006-07-29 05:33:12 +00:00
|
|
|
if (event.metaKey || event.ctrlKey) { // and meta or ctrl are down
|
2006-07-29 05:41:33 +00:00
|
|
|
if (openNewTabOrWindow(event, href, true))
|
2006-07-29 05:38:08 +00:00
|
|
|
return true;
|
2006-07-29 05:33:12 +00:00
|
|
|
}
|
2006-07-29 05:35:48 +00:00
|
|
|
var saveModifier = true;
|
|
|
|
if (pref) {
|
|
|
|
try {
|
2006-07-29 05:38:09 +00:00
|
|
|
saveModifier = pref.getBoolPref("ui.key.saveLink.shift");
|
2006-07-29 05:35:48 +00:00
|
|
|
}
|
2006-07-29 05:35:49 +00:00
|
|
|
catch(ex) {
|
2006-07-29 05:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-29 05:39:23 +00:00
|
|
|
saveModifier = saveModifier ? event.shiftKey : event.altKey;
|
2006-07-29 05:35:48 +00:00
|
|
|
|
|
|
|
if (saveModifier) { // if saveModifier is down
|
2006-07-29 05:39:56 +00:00
|
|
|
saveURL(href, linkNode ? gatherTextUnder(linkNode) : "");
|
2006-07-29 05:33:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (event.altKey) // if alt is down
|
|
|
|
return true; // do nothing
|
|
|
|
return false;
|
2006-07-29 05:35:46 +00:00
|
|
|
case 1: // if middle button clicked
|
2006-07-29 05:41:33 +00:00
|
|
|
if (openNewTabOrWindow(event, href, true))
|
2006-07-29 05:33:12 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:59 +00:00
|
|
|
function middleMousePaste( event )
|
2006-07-29 05:33:12 +00:00
|
|
|
{
|
2006-07-29 05:34:50 +00:00
|
|
|
var url = readFromClipboard();
|
2006-07-29 05:39:59 +00:00
|
|
|
if (!url)
|
|
|
|
return false;
|
|
|
|
url = getShortcutOrURI(url);
|
|
|
|
|
2006-07-29 05:41:33 +00:00
|
|
|
// On ctrl-middleclick, open in new window or tab. Do not send referrer.
|
2006-07-29 05:41:39 +00:00
|
|
|
if (event.ctrlKey) {
|
|
|
|
// fix up our pasted URI in case it is malformed.
|
|
|
|
const nsIURIFixup = Components.interfaces.nsIURIFixup;
|
|
|
|
if (!gURIFixup)
|
|
|
|
gURIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
|
|
|
|
.getService(nsIURIFixup);
|
|
|
|
|
|
|
|
url = gURIFixup.createFixupURI(url, nsIURIFixup.FIXUP_FLAGS_MAKE_ALTERNATE_URI).spec;
|
|
|
|
|
2006-07-29 05:41:33 +00:00
|
|
|
return openNewTabOrWindow(event, url, false);
|
2006-07-29 05:41:39 +00:00
|
|
|
}
|
2006-07-29 05:39:59 +00:00
|
|
|
|
|
|
|
// If ctrl wasn't down, then just load the url in the current win/tab.
|
2006-07-29 05:41:33 +00:00
|
|
|
if (url != "about:blank") {
|
|
|
|
gURLBar.value = url;
|
|
|
|
}
|
2006-07-29 05:39:59 +00:00
|
|
|
loadURI(url);
|
|
|
|
event.preventBubble();
|
|
|
|
return true;
|
2006-07-29 05:33:12 +00:00
|
|
|
}
|
2006-07-29 05:38:56 +00:00
|
|
|
|
|
|
|
function makeURLAbsolute( base, url )
|
|
|
|
{
|
|
|
|
// Construct nsIURL.
|
|
|
|
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIIOService);
|
2006-07-29 05:39:31 +00:00
|
|
|
var baseURI = ioService.newURI(base, null, null);
|
2006-07-29 05:38:56 +00:00
|
|
|
|
2006-07-29 05:39:31 +00:00
|
|
|
return ioService.newURI(baseURI.resolve(url), null, null).spec;
|
2006-07-29 05:38:56 +00:00
|
|
|
}
|