1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var LinkToolbarUI = function()
{
}
LinkToolbarUI.prototype.linkAdded =
function(event)
{
var element = event.originalTarget;
if (element.ownerDocument != getBrowser().contentDocument ||
!linkToolbarUI.isLinkToolbarEnabled() ||
!(ChromeUtils.getClassName(element) === "HTMLLinkElement") ||
!element.href || (!element.rel && !element.rev))
return;
linkToolbarHandler.handle(element);
}
LinkToolbarUI.prototype.isLinkToolbarEnabled =
function()
{
if (document.getElementById("linktoolbar").getAttribute("hidden") == "true")
return false;
else
return true;
}
LinkToolbarUI.prototype.clear =
function(event)
{
if (event.originalTarget != getBrowser().contentDocument ||
!linkToolbarUI.isLinkToolbarEnabled() ||
!linkToolbarHandler.hasItems)
return;
linkToolbarHandler.clearAllItems();
}
LinkToolbarUI.prototype.tabSelected =
function(event)
{
if (event.originalTarget.localName != "tabs" ||
!linkToolbarUI.isLinkToolbarEnabled())
return;
linkToolbarHandler.clearAllItems();
linkToolbarUI.deactivate();
linkToolbarUI.fullSlowRefresh();
}
LinkToolbarUI.prototype.fullSlowRefresh =
function()
{
var currentNode = getBrowser().contentDocument.documentElement;
if (!ChromeUtils.getClassName(currentNode) === "HTMLHtmlElement")
return;
currentNode = currentNode.firstChild;
while(currentNode)
{
if (ChromeUtils.getClassName(currentNode) === "HTMLHeadElement") {
currentNode = currentNode.firstChild;
while(currentNode)
{
if (ChromeUtils.getClassName(currentNode) === "HTMLLinkElement")
linkToolbarUI.linkAdded({originalTarget: currentNode});
currentNode = currentNode.nextSibling;
}
}
else if (currentNode.nodeType == currentNode.ELEMENT_NODE) {
// head is supposed to be the first element inside html.
// Got something else instead. returning
return;
}
else
{
// Got a comment node or something like that. Moving on.
currentNode = currentNode.nextSibling;
}
}
}
LinkToolbarUI.prototype.toolbarActive = false;
LinkToolbarUI.prototype.activate =
function()
{
if (!linkToolbarUI.toolbarActive) {
linkToolbarUI.toolbarActive = true;
document.getElementById("linktoolbar").setAttribute("hasitems", "true");
var contentArea = document.getElementById("appcontent");
contentArea.addEventListener("pagehide", linkToolbarUI.clear, true);
contentArea.addEventListener("pageshow", linkToolbarUI.deactivate, true);
contentArea.addEventListener("DOMHeadLoaded", linkToolbarUI.deactivate,
true);
}
}
LinkToolbarUI.prototype.deactivate =
function()
{
// This function can never be called unless the toolbar is active, because
// it's a handler that's only activated in that situation, so there's no need
// to check toolbarActive. On the other hand, by the time this method is
// called the toolbar might have been populated again already, in which case
// we don't want to deactivate.
if (!linkToolbarHandler.hasItems) {
linkToolbarUI.toolbarActive = false;
document.getElementById("linktoolbar").setAttribute("hasitems", "false");
var contentArea = document.getElementById("appcontent");
contentArea.removeEventListener("pagehide", linkToolbarUI.clear, true);
contentArea.removeEventListener("pageshow", linkToolbarUI.deactivate, true);
contentArea.removeEventListener("DOMHeadLoaded", linkToolbarUI.deactivate,
true);
}
}
/* called whenever something on the toolbar gets an oncommand event */
LinkToolbarUI.prototype.commanded =
function(event)
{
// Return if this is one of the menubuttons.
if (event.target.getAttribute("type") == "menu") return;
if (!event.target.getAttribute("href")) return;
var destURL = event.target.getAttribute("href");
// We have to do a security check here, because we are loading URIs given
// to us by a web page from chrome, which is privileged.
try {
urlSecurityCheck(destURL, content.document.nodePrincipal,
Ci.nsIScriptSecurityManager.STANDARD);
loadURI(destURL, content.document.documentURIObject);
} catch (e) {
dump("Error: it is not permitted to load this URI from a <link> element: " + e);
}
}
// functions for twiddling XUL elements in the toolbar
LinkToolbarUI.prototype.toggleLinkToolbar =
function(checkedItem)
{
this.goToggleTristateToolbar("linktoolbar", checkedItem);
this.initHandlers();
if (this.isLinkToolbarEnabled())
this.fullSlowRefresh();
else
linkToolbarHandler.clearAllItems();
}
LinkToolbarUI.prototype.initLinkbarVisibilityMenu =
function()
{
var state = document.getElementById("linktoolbar").getAttribute("hidden");
if (!state)
state = "maybe";
var checkedItem = document.getElementById("cmd_viewlinktoolbar_" + state);
checkedItem.setAttribute("checked", true);
checkedItem.checked = true;
}
LinkToolbarUI.prototype.goToggleTristateToolbar =
function(id, checkedItem)
{
var toolbar = document.getElementById(id);
if (toolbar)
{
toolbar.setAttribute("hidden", checkedItem.value);
document.persist(id, "hidden");
}
}
LinkToolbarUI.prototype.addHandlerActive = false;
LinkToolbarUI.prototype.initialized = false;
LinkToolbarUI.prototype.initHandlers =
function()
{
var contentArea = document.getElementById("appcontent");
if (linkToolbarUI.isLinkToolbarEnabled())
{
if (!linkToolbarUI.addHandlerActive) {
contentArea.addEventListener("select", linkToolbarUI.tabSelected);
contentArea.addEventListener("DOMLinkAdded", linkToolbarUI.linkAdded,
true);
linkToolbarUI.addHandlerActive = true;
}
} else
{
if (linkToolbarUI.addHandlerActive) {
contentArea.removeEventListener("select", linkToolbarUI.tabSelected);
contentArea.removeEventListener("DOMLinkAdded", linkToolbarUI.linkAdded,
true);
linkToolbarUI.addHandlerActive = false;
}
}
if (!linkToolbarUI.initialized)
{
linkToolbarUI.initialized = true;
document.removeEventListener("pageshow", linkToolbarUI.initHandlers, true);
}
}
var linkToolbarUI = new LinkToolbarUI;
|