1
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
23
* Alec Flett <alecf@netscape.com>
24
* Ben Goodger <ben@netscape.com>
25
* Mike Pinkerton <pinkerton@netscape.com>
26
* Blake Ross <blakeross@telocity.com>
28
* Alternatively, the contents of this file may be used under the terms of
29
* either of the GNU General Public License Version 2 or later (the "GPL"),
30
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31
* in which case the provisions of the GPL or the LGPL are applicable instead
32
* of those above. If you wish to allow use of your version of this file only
33
* under the terms of either the GPL or the LGPL, and not to allow others to
34
* use your version of this file under the terms of the MPL, indicate your
35
* decision by deleting the provisions above and replace them with the notice
36
* and other provisions required by the GPL or the LGPL. If you do not delete
37
* the provisions above, a recipient may use your version of this file under
38
* the terms of any one of the MPL, the GPL or the LGPL.
40
* ***** END LICENSE BLOCK ***** */
43
* - [ Dependencies ] ---------------------------------------------------------
50
pref = Components.classes["@mozilla.org/preferences-service;1"]
51
.getService(Components.interfaces.nsIPrefBranch);
53
// Prefill a single text field
54
function prefillTextBox(target) {
55
// obtain values to be used for prefilling
56
var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
57
var value = walletService.WALLET_PrefillOneElement(window.content, target);
60
// result is a linear sequence of values, each preceded by a separator character
61
// convert linear sequence of values into an array of values
62
var separator = value[0];
63
var valueList = value.substring(1, value.length).split(separator);
65
target.value = valueList[0];
67
* Following code is a replacement for above line. In the case of multiple values, it
68
* presents the user with a dialog containing a list from which he can select the value
69
* he wants. However it is being commented out for now because of several problems, namely
71
* 1. There is no easy way to put localizable strings for the title and message of
72
* the dialog without introducing a .properties file which currently doesn't exist
73
* 2. Using blank title and messages as shown below have a problem because a zero-length
74
* title is being displayed as some garbage characters (which is why the code below
75
* has a title of " " instead of ""). This is a separate bug which will have to be
76
* investigated further.
77
* 3. The current wallet tables present alternate values for items such as shipping
78
* address -- namely billing address and home address. Up until now, these alternate
79
* values have never been a problem because the preferred value is always first and is
80
* all the user sees when doing a prefill. However now he will be presented with a
81
* list showing all these values and asking him to pick one, even though the wallet
82
* code was clearly able to determine that he meant shipping address and not billing
84
* 4. There is a relatively long delay before the dialog come up whereas values are
85
* filled in quickly when no dialog is involved.
87
* Once this feature is checked in, a separate bug will be opened asking that the above
88
* problems be examined and this dialog turned on
90
if (valueList.length == 1) {
91
// only one value, use it for prefilling
92
target.value = valueList[0];
95
// more than one value, have user select the one he wants
96
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
97
promptService = promptService.QueryInterface(Components.interfaces.nsIPromptService);
103
(window, title, message, valueList.length, valueList, position)
105
target.value = valueList[position.value];
109
* End of commented out code
114
function hrefAndLinkNodeForClickEvent(event)
116
var target = event.target;
120
var isKeyPress = (event.type == "keypress");
122
if ( target instanceof HTMLAnchorElement ||
123
target instanceof HTMLAreaElement ||
124
target instanceof HTMLLinkElement ) {
125
if (target.hasAttribute("href"))
128
else if ( target instanceof HTMLInputElement
129
&& (event.target.type == "text") // text field
130
&& !isKeyPress // not a key event
131
&& event.detail == 2 // double click
132
&& event.button == 0 // left mouse button
133
&& event.target.value.length == 0 // no text has been entered
134
&& "@mozilla.org/wallet/wallet-service;1" in Components.classes // wallet is available
136
prefillTextBox(target); // prefill the empty text field if possible
139
linkNode = event.originalTarget;
140
while (linkNode && !(linkNode instanceof HTMLAnchorElement))
141
linkNode = linkNode.parentNode;
142
// <a> cannot be nested. So if we find an anchor without an
143
// href, there is no useful <a> around the target
144
if (linkNode && !linkNode.hasAttribute("href"))
149
href = linkNode.href;
154
if (linkNode.nodeType == Node.ELEMENT_NODE) {
155
href = linkNode.getAttributeNS("http://www.w3.org/1999/xlink", "href");
158
linkNode = linkNode.parentNode;
161
href = makeURLAbsolute(linkNode.baseURI, href);
165
return href ? {href: href, linkNode: linkNode} : null;
168
// Called whenever the user clicks in the content area,
169
// except when left-clicking on links (special case)
170
// should always return true for click to go through
171
function contentAreaClick(event)
173
if (!event.isTrusted || event.getPreventDefault()) {
177
var isKeyPress = (event.type == "keypress");
178
var ceParams = hrefAndLinkNodeForClickEvent(event);
180
var href = ceParams.href;
182
openNewTabWith(href, true, event.shiftKey);
183
event.stopPropagation();
186
handleLinkClick(event, href, ceParams.linkNode);
187
// if in mailnews block the link left click if we determine
188
// that this URL is phishy (i.e. a potential email scam)
189
if ("gMessengerBundle" in this && !event.button)
190
return !isPhishingURL(ceParams.linkNode, false, href);
195
if (pref && !isKeyPress && event.button == 1 &&
196
pref.getBoolPref("middlemouse.contentLoadURL")) {
197
if (middleMousePaste(event)) {
198
event.stopPropagation();
204
function contentAreaMouseDown(event)
206
if (event.button == 1 && (event.target != event.currentTarget)
207
&& !hrefAndLinkNodeForClickEvent(event)
208
&& !isAutoscrollBlocker(event.originalTarget)) {
209
startScrolling(event);
215
function isAutoscrollBlocker(node)
220
if (pref.getBoolPref("middlemouse.contentLoadURL"))
223
if (!pref.getBoolPref("middlemouse.paste"))
226
if (node.ownerDocument.designMode == "on")
230
if (node instanceof HTMLTextAreaElement ||
231
(node instanceof HTMLInputElement &&
232
(node.type == "text" || node.type == "password")))
235
node = node.parentNode;
240
function openNewTabOrWindow(event, href, sendReferrer)
242
// should we open it in a new tab?
243
if (pref && pref.getBoolPref("browser.tabs.opentabfor.middleclick")) {
244
openNewTabWith(href, sendReferrer, event.shiftKey);
245
event.stopPropagation();
249
// should we open it in a new window?
250
if (pref && pref.getBoolPref("middlemouse.openNewWindow")) {
251
openNewWindowWith(href, sendReferrer);
252
event.stopPropagation();
256
// let someone else deal with it
260
function handleLinkClick(event, href, linkNode)
262
// Make sure we are allowed to open this URL
263
urlSecurityCheck(href, document);
265
switch (event.button) {
266
case 0: // if left button clicked
267
if (event.metaKey || event.ctrlKey) { // and meta or ctrl are down
268
if (openNewTabOrWindow(event, href, true))
271
var saveModifier = true;
274
saveModifier = pref.getBoolPref("ui.key.saveLink.shift");
279
saveModifier = saveModifier ? event.shiftKey : event.altKey;
281
if (saveModifier) { // if saveModifier is down
282
saveURL(href, linkNode ? gatherTextUnder(linkNode) : "",
283
"SaveLinkTitle", false, getReferrer(document));
286
if (event.altKey) // if alt is down
287
return true; // do nothing
289
case 1: // if middle button clicked
290
if (openNewTabOrWindow(event, href, true))
297
function middleMousePaste( event )
299
var url = readFromClipboard();
302
addToUrlbarHistory(url);
303
url = getShortcutOrURI(url);
305
// On ctrl-middleclick, open in new window or tab. Do not send referrer.
307
// fix up our pasted URI in case it is malformed.
308
const nsIURIFixup = Components.interfaces.nsIURIFixup;
310
gURIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
311
.getService(nsIURIFixup);
313
url = gURIFixup.createFixupURI(url, nsIURIFixup.FIXUP_FLAGS_MAKE_ALTERNATE_URI).spec;
315
return openNewTabOrWindow(event, url, false);
318
// If ctrl wasn't down, then just load the url in the targeted win/tab.
319
var browser = getBrowser();
320
var tab = event.originalTarget;
321
if (tab.localName == "tab" &&
322
tab.parentNode == browser.mTabContainer) {
323
tab.linkedBrowser.userTypedValue = url;
324
if (tab == browser.mCurrentTab && url != "about:blank") {
327
tab.linkedBrowser.loadURI(url);
328
if (event.shiftKey != (pref && pref.getBoolPref("browser.tabs.loadInBackground")))
329
browser.selectedTab = tab;
331
else if (event.target == browser) {
332
tab = browser.addTab(url);
333
if (event.shiftKey != (pref && pref.getBoolPref("browser.tabs.loadInBackground")))
334
browser.selectedTab = tab;
337
if (url != "about:blank") {
343
event.stopPropagation();
347
function addToUrlbarHistory(aUrlToAdd)
349
// Remove leading and trailing spaces first
350
aUrlToAdd = aUrlToAdd.replace(/^\s+/, '').replace(/\s+$/, '');
354
if (aUrlToAdd.search(/[\x00-\x1F]/) != -1) // don't store bad URLs
358
gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
359
.getService(Components.interfaces.nsIRDFService);
362
gGlobalHistory = Components.classes["@mozilla.org/browser/global-history;2"]
363
.getService(Components.interfaces.nsIBrowserHistory);
366
gURIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
367
.getService(Components.interfaces.nsIURIFixup);
369
gLocalStore = gRDF.GetDataSource("rdf:local-store");
372
gRDFC = Components.classes["@mozilla.org/rdf/container-utils;1"]
373
.getService(Components.interfaces.nsIRDFContainerUtils);
375
var entries = gRDFC.MakeSeq(gLocalStore, gRDF.GetResource("nc:urlbar-history"));
378
var elements = entries.GetElements();
383
var urlToCompare = aUrlToAdd.toUpperCase();
384
while(elements.hasMoreElements()) {
385
var entry = elements.getNext();
386
if (!entry) continue;
390
entry = entry.QueryInterface(Components.interfaces.nsIRDFLiteral);
392
// XXXbar not an nsIRDFLiteral for some reason. see 90337.
396
if (urlToCompare == entry.Value.toUpperCase()) {
397
// URL already present in the database
398
// Remove it from its current position.
399
// It is inserted to the top after the while loop.
400
entries.RemoveElementAt(index, true);
405
// Otherwise, we've got a new URL in town. Add it!
408
var url = getShortcutOrURI(aUrlToAdd);
409
var fixedUpURI = gURIFixup.createFixupURI(url, 0);
410
if (!fixedUpURI.schemeIs("data"))
411
gGlobalHistory.markPageAsTyped(fixedUpURI);
416
// Put the value as it was typed by the user in to RDF
417
// Insert it to the beginning of the list.
418
var entryToAdd = gRDF.GetLiteral(aUrlToAdd);
419
entries.InsertElementAt(entryToAdd, 1, true);
421
// Remove any expired history items so that we don't let
422
// this grow without bound.
423
for (index = entries.GetCount(); index > MAX_URLBAR_HISTORY_ITEMS; --index) {
424
entries.RemoveElementAt(index, true);
428
function makeURLAbsolute(base, url)
431
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
432
.getService(Components.interfaces.nsIIOService);
433
var baseURI = ioService.newURI(base, null, null);
435
return ioService.newURI(baseURI.resolve(url), null, null).spec;