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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/*
* This file is part of Adblock Plus <http://adblockplus.org/>,
* Copyright (C) 2006-2014 Eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @fileOverview Code responsible for showing and hiding object tabs.
*/
/**
* Class responsible for showing and hiding object tabs.
* @class
*/
var objTabs =
{
/**
* Number of milliseconds to wait until hiding tab after the mouse moves away.
* @type Integer
*/
HIDE_DELAY: 1000,
/**
* Flag used to trigger object tabs initialization first time object tabs are
* used.
* @type Boolean
*/
initialized: false,
/**
* Will be set to true while initialization is in progress.
* @type Boolean
*/
initializing: false,
/**
* Parameters for _showTab, to be called once initialization is complete.
*/
delayedShowParams: null,
/**
* Randomly generated class to be used for visible object tabs on top of object.
* @type String
*/
objTabClassVisibleTop: null,
/**
* Randomly generated class to be used for visible object tabs at the bottom of the object.
* @type String
*/
objTabClassVisibleBottom: null,
/**
* Randomly generated class to be used for invisible object tabs.
* @type String
*/
objTabClassHidden: null,
/**
* Document element the object tab is currently being displayed for.
* @type Element
*/
currentElement: null,
/**
* Windows that the window event handler is currently registered for.
* @type Array of Window
*/
windowListeners: null,
/**
* Panel element currently used as object tab.
* @type Element
*/
objtabElement: null,
/**
* Time of previous position update.
* @type Integer
*/
prevPositionUpdate: 0,
/**
* Timer used to update position of the object tab.
* @type nsITimer
*/
positionTimer: null,
/**
* Timer used to delay hiding of the object tab.
* @type nsITimer
*/
hideTimer: null,
/**
* Used when hideTimer is running, time when the tab should be hidden.
* @type Integer
*/
hideTargetTime: 0,
/**
* Initializes object tabs (generates random classes and registers stylesheet).
*/
_initCSS: function()
{
function processCSSData(request)
{
if (onShutdown.done)
return;
let data = request.responseText;
let rnd = [];
let offset = "a".charCodeAt(0);
for (let i = 0; i < 60; i++)
rnd.push(offset + Math.random() * 26);
this.objTabClassVisibleTop = String.fromCharCode.apply(String, rnd.slice(0, 20));
this.objTabClassVisibleBottom = String.fromCharCode.apply(String, rnd.slice(20, 40));
this.objTabClassHidden = String.fromCharCode.apply(String, rnd.slice(40, 60));
let {Utils} = require("utils");
let url = Utils.makeURI("data:text/css," + encodeURIComponent(data.replace(/%%CLASSVISIBLETOP%%/g, this.objTabClassVisibleTop)
.replace(/%%CLASSVISIBLEBOTTOM%%/g, this.objTabClassVisibleBottom)
.replace(/%%CLASSHIDDEN%%/g, this.objTabClassHidden)));
Utils.styleService.loadAndRegisterSheet(url, Ci.nsIStyleSheetService.USER_SHEET);
onShutdown.add(function()
{
Utils.styleService.unregisterSheet(url, Ci.nsIStyleSheetService.USER_SHEET);
});
this.initializing = false;
this.initialized = true;
if (this.delayedShowParams)
this._showTab.apply(this, this.delayedShowParams);
}
this.delayedShowParams = arguments;
if (!this.initializing)
{
this.initializing = true;
// Load CSS asynchronously
try {
let request = new XMLHttpRequest();
request.mozBackgroundRequest = true;
request.open("GET", "chrome://adblockplus/content/objtabs.css");
request.overrideMimeType("text/plain");
request.addEventListener("load", processCSSData.bind(this, request), false);
request.send(null);
}
catch (e)
{
Cu.reportError(e);
this.initializing = false;
}
}
},
/**
* Called to show object tab for an element.
*/
showTabFor: function(/**Element*/ element)
{
// Object tabs aren't usable in Fennec
let {application} = require("info");
if (application == "fennec" || application == "fennec2")
return;
let {Prefs} = require("prefs");
if (!Prefs.frameobjects)
return;
if (this.hideTimer)
{
this.hideTimer.cancel();
this.hideTimer = null;
}
if (this.objtabElement)
this.objtabElement.style.setProperty("opacity", "1", "important");
if (this.currentElement != element)
{
this._hideTab();
let {Policy} = require("contentPolicy");
let {RequestNotifier} = require("requestNotifier");
let data = RequestNotifier.getDataForNode(element, true, Policy.type.OBJECT);
if (data)
{
if (this.initialized)
this._showTab(element, data[1]);
else
this._initCSS(element, data[1]);
}
}
},
/**
* Called to hide object tab for an element (actual hiding happens delayed).
*/
hideTabFor: function(/**Element*/ element)
{
if (element != this.currentElement || this.hideTimer)
return;
this.hideTargetTime = Date.now() + this.HIDE_DELAY;
this.hideTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
this.hideTimer.init(this, 40, Ci.nsITimer.TYPE_REPEATING_SLACK);
},
/**
* Makes the tab element visible.
*/
_showTab: function(/**Element*/ element, /**RequestEntry*/ data)
{
let {UI} = require("ui");
if (!UI.overlay)
return;
let doc = element.ownerDocument.defaultView.top.document;
this.objtabElement = doc.createElementNS("http://www.w3.org/1999/xhtml", "a");
this.objtabElement.textContent = UI.overlay.attributes.objtabtext;
this.objtabElement.setAttribute("title", UI.overlay.attributes.objtabtooltip);
this.objtabElement.setAttribute("href", data.location);
this.objtabElement.setAttribute("class", this.objTabClassHidden);
this.objtabElement.style.setProperty("opacity", "1", "important");
this.objtabElement.nodeData = data;
this.currentElement = element;
// Register paint listeners for the relevant windows
this.windowListeners = [];
let wnd = element.ownerDocument.defaultView;
while (wnd)
{
wnd.addEventListener("MozAfterPaint", objectWindowEventHandler, false);
this.windowListeners.push(wnd);
wnd = (wnd.parent != wnd ? wnd.parent : null);
}
// Register mouse listeners on the object tab
this.objtabElement.addEventListener("mouseover", objectTabEventHander, false);
this.objtabElement.addEventListener("mouseout", objectTabEventHander, false);
this.objtabElement.addEventListener("click", objectTabEventHander, true);
// Insert the tab into the document and adjust its position
doc.documentElement.appendChild(this.objtabElement);
if (!this.positionTimer)
{
this.positionTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
this.positionTimer.init(this, 200, Ci.nsITimer.TYPE_REPEATING_SLACK);
}
this._positionTab();
},
/**
* Hides the tab element.
*/
_hideTab: function()
{
this.delayedShowParams = null;
if (this.objtabElement)
{
// Prevent recursive calls via popuphidden handler
let objtab = this.objtabElement;
this.objtabElement = null;
this.currentElement = null;
if (this.hideTimer)
{
this.hideTimer.cancel();
this.hideTimer = null;
}
if (this.positionTimer)
{
this.positionTimer.cancel();
this.positionTimer = null;
}
try {
objtab.parentNode.removeChild(objtab);
} catch (e) {}
objtab.removeEventListener("mouseover", objectTabEventHander, false);
objtab.removeEventListener("mouseout", objectTabEventHander, false);
objtab.nodeData = null;
for (let wnd of this.windowListeners)
wnd.removeEventListener("MozAfterPaint", objectWindowEventHandler, false);
this.windowListeners = null;
}
},
/**
* Updates position of the tab element.
*/
_positionTab: function()
{
// Test whether element is still in document
let elementDoc = null;
try
{
elementDoc = this.currentElement.ownerDocument;
} catch (e) {} // Ignore "can't access dead object" error
if (!elementDoc || !this.currentElement.offsetWidth || !this.currentElement.offsetHeight ||
!elementDoc.defaultView || !elementDoc.documentElement)
{
this._hideTab();
return;
}
let objRect = this._getElementPosition(this.currentElement);
let className = this.objTabClassVisibleTop;
let left = objRect.right - this.objtabElement.offsetWidth;
let top = objRect.top - this.objtabElement.offsetHeight;
if (top < 0)
{
top = objRect.bottom;
className = this.objTabClassVisibleBottom;
}
if (this.objtabElement.style.left != left + "px")
this.objtabElement.style.setProperty("left", left + "px", "important");
if (this.objtabElement.style.top != top + "px")
this.objtabElement.style.setProperty("top", top + "px", "important");
if (this.objtabElement.getAttribute("class") != className)
this.objtabElement.setAttribute("class", className);
this.prevPositionUpdate = Date.now();
},
/**
* Calculates element's position relative to the top frame and considering
* clipping due to scrolling.
* @return {left: Number, top: Number, right: Number, bottom: Number}
*/
_getElementPosition: function(/**Element*/ element)
{
// Restrict rectangle coordinates by the boundaries of a window's client area
function intersectRect(rect, wnd)
{
// Cannot use wnd.innerWidth/Height because they won't account for scrollbars
let doc = wnd.document;
let wndWidth = doc.documentElement.clientWidth;
let wndHeight = doc.documentElement.clientHeight;
if (doc.compatMode == "BackCompat") // clientHeight will be bogus in quirks mode
wndHeight = Math.max(doc.documentElement.offsetHeight, doc.body.offsetHeight) - wnd.scrollMaxY - 1;
rect.left = Math.max(rect.left, 0);
rect.top = Math.max(rect.top, 0);
rect.right = Math.min(rect.right, wndWidth);
rect.bottom = Math.min(rect.bottom, wndHeight);
}
let rect = element.getBoundingClientRect();
let wnd = element.ownerDocument.defaultView;
let style = wnd.getComputedStyle(element, null);
let offsets = [
parseFloat(style.borderLeftWidth) + parseFloat(style.paddingLeft),
parseFloat(style.borderTopWidth) + parseFloat(style.paddingTop),
parseFloat(style.borderRightWidth) + parseFloat(style.paddingRight),
parseFloat(style.borderBottomWidth) + parseFloat(style.paddingBottom)
];
rect = {left: rect.left + offsets[0], top: rect.top + offsets[1],
right: rect.right - offsets[2], bottom: rect.bottom - offsets[3]};
while (true)
{
intersectRect(rect, wnd);
if (!wnd.frameElement)
break;
// Recalculate coordinates to be relative to frame's parent window
let frameElement = wnd.frameElement;
wnd = frameElement.ownerDocument.defaultView;
let frameRect = frameElement.getBoundingClientRect();
let frameStyle = wnd.getComputedStyle(frameElement, null);
let relLeft = frameRect.left + parseFloat(frameStyle.borderLeftWidth) + parseFloat(frameStyle.paddingLeft);
let relTop = frameRect.top + parseFloat(frameStyle.borderTopWidth) + parseFloat(frameStyle.paddingTop);
rect.left += relLeft;
rect.right += relLeft;
rect.top += relTop;
rect.bottom += relTop;
}
return rect;
},
doBlock: function()
{
let {UI} = require("ui");
let {Utils} = require("utils");
let chromeWindow = Utils.getChromeWindow(this.currentElement.ownerDocument.defaultView);
UI.blockItem(chromeWindow, this.currentElement, this.objtabElement.nodeData);
},
/**
* Called whenever a timer fires.
*/
observe: function(/**nsISupport*/ subject, /**String*/ topic, /**String*/ data)
{
if (subject == this.positionTimer)
{
// Don't update position if it was already updated recently (via MozAfterPaint)
if (Date.now() - this.prevPositionUpdate > 100)
this._positionTab();
}
else if (subject == this.hideTimer)
{
let now = Date.now();
if (now >= this.hideTargetTime)
this._hideTab();
else if (this.hideTargetTime - now < this.HIDE_DELAY / 2)
this.objtabElement.style.setProperty("opacity", (this.hideTargetTime - now) * 2 / this.HIDE_DELAY, "important");
}
}
};
onShutdown.add(objTabs._hideTab.bind(objTabs));
/**
* Function called whenever the mouse enters or leaves an object.
*/
function objectMouseEventHander(/**Event*/ event)
{
if (!event.isTrusted)
return;
if (event.type == "mouseover")
objTabs.showTabFor(event.target);
else if (event.type == "mouseout")
objTabs.hideTabFor(event.target);
}
/**
* Function called for paint events of the object tab window.
*/
function objectWindowEventHandler(/**Event*/ event)
{
if (!event.isTrusted)
return;
// Don't trigger update too often, avoid overusing CPU on frequent page updates
if (event.type == "MozAfterPaint" && Date.now() - objTabs.prevPositionUpdate > 20)
objTabs._positionTab();
}
/**
* Function called whenever the mouse enters or leaves an object tab.
*/
function objectTabEventHander(/**Event*/ event)
{
if (onShutdown.done || !event.isTrusted)
return;
if (event.type == "click" && event.button == 0)
{
event.preventDefault();
event.stopPropagation();
objTabs.doBlock();
}
else if (event.type == "mouseover")
objTabs.showTabFor(objTabs.currentElement);
else if (event.type == "mouseout")
objTabs.hideTabFor(objTabs.currentElement);
}
exports.objectMouseEventHander = objectMouseEventHander;
|