-
Notifications
You must be signed in to change notification settings - Fork 1
/
include.postload.js
674 lines (589 loc) · 19.8 KB
/
include.postload.js
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-2015 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/>.
*/
// Click-to-hide stuff
var clickHide_activated = false;
var clickHide_filters = null;
var currentElement = null;
var highlightedElementsSelector = null;
var highlightedElementsInterval = null;
var clickHideFiltersDialog = null;
var lastRightClickEvent = null;
var lastRightClickEventValid = false;
var lastMouseOverEvent = null;
function highlightElement(element, shadowColor, backgroundColor)
{
unhighlightElement(element);
var highlightWithOverlay = function()
{
var overlay = addElementOverlay(element);
// If the element isn't displayed no overlay will be added.
// Moreover, we don't need to highlight anything then.
if (!overlay)
return;
highlightElement(overlay, shadowColor, backgroundColor);
overlay.style.pointerEvents = "none";
element._unhighlight = function()
{
overlay.parentNode.removeChild(overlay);
};
};
var highlightWithStyleAttribute = function()
{
var originalBoxShadow = element.style.getPropertyValue("box-shadow");
var originalBoxShadowPriority = element.style.getPropertyPriority("box-shadow");
var originalBackgroundColor = element.style.getPropertyValue("background-color");
var originalBackgroundColorPriority = element.style.getPropertyPriority("background-color");
element.style.setProperty("box-shadow", "inset 0px 0px 5px " + shadowColor, "important");
element.style.setProperty("background-color", backgroundColor, "important");
element._unhighlight = function()
{
this.style.removeProperty("box-shadow");
this.style.setProperty(
"box-shadow",
originalBoxShadow,
originalBoxShadowPriority
);
this.style.removeProperty("background-color");
this.style.setProperty(
"background-color",
originalBackgroundColor,
originalBackgroundColorPriority
);
};
};
if ("prisoner" in element)
highlightWithStyleAttribute();
else
highlightWithOverlay();
}
function unhighlightElement(element)
{
if ("_unhighlight" in element)
{
element._unhighlight();
delete element._unhighlight;
}
}
// Highlight elements according to selector string. This would include
// all elements that would be affected by proposed filters.
function highlightElements(selectorString) {
unhighlightElements();
var elements = Array.prototype.slice.call(document.querySelectorAll(selectorString));
highlightedElementsSelector = selectorString;
// Highlight elements progressively. Otherwise the page freezes
// when a lot of elements get highlighted at the same time.
highlightedElementsInterval = setInterval(function()
{
if (elements.length > 0)
{
var element = elements.shift();
if (element != currentElement)
highlightElement(element, "#fd6738", "#f6e1e5");
}
else
{
clearInterval(highlightedElementsInterval);
highlightedElementsInterval = null;
}
}, 0);
}
// Unhighlight all elements, including those that would be affected by
// the proposed filters
function unhighlightElements() {
if (highlightedElementsInterval)
{
clearInterval(highlightedElementsInterval)
highlightedElementsInterval = null;
}
if (highlightedElementsSelector)
{
Array.prototype.forEach.call(
document.querySelectorAll(highlightedElementsSelector),
unhighlightElement
);
highlightedElementsSelector = null;
}
}
// Adds an overlay to an element, which is probably a Flash object
function addElementOverlay(elt) {
var position = "absolute";
var offsetX = window.scrollX;
var offsetY = window.scrollY;
for (var e = elt; e; e = e.parentElement)
{
var style = getComputedStyle(e);
// If the element isn't rendered (since its or one of its ancestor's
// "display" property is "none"), the overlay wouldn't match the element.
if (style.display == "none")
return null;
// If the element or one of its ancestors uses fixed postioning, the overlay
// has to use fixed postioning too. Otherwise it might not match the element.
if (style.position == "fixed")
{
position = "fixed";
offsetX = offsetY = 0;
}
}
var overlay = document.createElement('div');
overlay.prisoner = elt;
overlay.className = "__adblockplus__overlay";
overlay.setAttribute('style', 'opacity:0.4; display:inline-box; overflow:hidden; box-sizing:border-box;');
var rect = elt.getBoundingClientRect();
overlay.style.width = rect.width + "px";
overlay.style.height = rect.height + "px";
overlay.style.left = (rect.left + offsetX) + "px";
overlay.style.top = (rect.top + offsetY) + "px";
overlay.style.position = position;
overlay.style.zIndex = 0x7FFFFFFE;
// elt.parentNode.appendChild(overlay, elt);
document.documentElement.appendChild(overlay);
return overlay;
}
// Show dialog asking user whether she wants to add the proposed filters derived
// from selected page element
function clickHide_showDialog(filters)
{
clickHide_filters = filters;
clickHideFiltersDialog = document.createElement("iframe");
clickHideFiltersDialog.src = ext.getURL("block.html");
clickHideFiltersDialog.setAttribute("style", "position: fixed !important; visibility: hidden; display: block !important; border: 0px !important;");
clickHideFiltersDialog.style.WebkitBoxShadow = "5px 5px 20px rgba(0,0,0,0.5)";
clickHideFiltersDialog.style.zIndex = 0x7FFFFFFF;
// Position in upper-left all the time
clickHideFiltersDialog.style.left = "50px";
clickHideFiltersDialog.style.top = "50px";
// Make dialog partly transparent when mouse isn't over it so user has a better
// view of what's going to be blocked
clickHideFiltersDialog.onmouseout = function()
{
if (clickHideFiltersDialog)
clickHideFiltersDialog.style.setProperty("opacity", "0.7");
};
clickHideFiltersDialog.onmouseover = function()
{
if (clickHideFiltersDialog)
clickHideFiltersDialog.style.setProperty("opacity", "1.0");
};
document.documentElement.appendChild(clickHideFiltersDialog);
}
// Turn on the choose element to create filter thing
function clickHide_activate() {
if(document == null)
return;
// If we are already selecting, abort now
if (clickHide_activated || clickHideFiltersDialog)
clickHide_deactivate();
// Add overlays for blockable elements that don't emit mouse events,
// so that they can still be selected.
[].forEach.call(
document.querySelectorAll('object,embed,iframe,frame'),
function(element)
{
getFiltersForElement(element, function(filters)
{
if (filters.length > 0)
addElementOverlay(element);
});
}
);
clickHide_activated = true;
document.addEventListener("mousedown", clickHide_stopPropagation, true);
document.addEventListener("mouseup", clickHide_stopPropagation, true);
document.addEventListener("mouseenter", clickHide_stopPropagation, true);
document.addEventListener("mouseleave", clickHide_stopPropagation, true);
document.addEventListener("mouseover", clickHide_mouseOver, true);
document.addEventListener("mouseout", clickHide_mouseOut, true);
document.addEventListener("click", clickHide_mouseClick, true);
document.addEventListener("keydown", clickHide_keyDown, true);
ext.onExtensionUnloaded.addListener(clickHide_deactivate);
}
// Called when user has clicked on something and we are waiting for confirmation
// on whether the user actually wants these filters
function clickHide_rulesPending() {
clickHide_activated = false;
if (clickHideFiltersDialog)
{
document.documentElement.removeChild(clickHideFiltersDialog);
clickHideFiltersDialog = null;
}
document.removeEventListener("mousedown", clickHide_stopPropagation, true);
document.removeEventListener("mouseup", clickHide_stopPropagation, true);
document.removeEventListener("mouseenter", clickHide_stopPropagation, true);
document.removeEventListener("mouseleave", clickHide_stopPropagation, true);
document.removeEventListener("mouseover", clickHide_mouseOver, true);
document.removeEventListener("mouseout", clickHide_mouseOut, true);
document.removeEventListener("click", clickHide_mouseClick, true);
document.removeEventListener("keydown", clickHide_keyDown, true);
}
function clickHide_deactivate()
{
clickHide_rulesPending();
clickHide_filters = null;
lastRightClickEvent = null;
if (currentElement)
{
currentElement.removeEventListener("contextmenu", clickHide_elementClickHandler, true);
unhighlightElement(currentElement);
currentElement = null;
}
unhighlightElements();
var overlays = document.getElementsByClassName("__adblockplus__overlay");
while (overlays.length > 0)
overlays[0].parentNode.removeChild(overlays[0]);
ext.onExtensionUnloaded.removeListener(clickHide_deactivate);
}
function clickHide_stopPropagation(e)
{
e.stopPropagation();
}
function clickHide_elementClickHandler(e) {
e.preventDefault();
e.stopPropagation();
clickHide_mouseClick(e);
}
function getBlockableElementOrAncestor(element, callback)
{
// We assume that the user doesn't want to block the whole page.
// So we never consider the <html> or <body> element.
while (element && element != document.documentElement
&& element != document.body)
{
// We can't handle non-HTML (like SVG) elements, as well as
// <area> elements (see below). So fall back to the parent element.
if (!(element instanceof HTMLElement) || element.localName == "area")
element = element.parentElement;
// If image maps are used mouse events occur for the <area> element.
// But we have to block the image associated with the <map> element.
else if (element.localName == "map")
{
var images = document.querySelectorAll("img[usemap]");
var image = null;
for (var i = 0; i < images.length; i++)
{
var usemap = images[i].getAttribute("usemap");
var index = usemap.indexOf("#");
if (index != -1 && usemap.substr(index + 1) == element.name)
{
image = images[i];
break;
}
}
element = image;
}
// Finally, if none of the above is true, check whether we can generate
// any filters for this element. Otherwise fall back to its parent element.
else
{
getFiltersForElement(element, function(filters)
{
if (filters.length > 0)
callback(element);
else
getBlockableElementOrAncestor(element.parentElement, callback);
});
return;
}
}
// We reached the document root without finding a blockable element.
callback(null);
}
// Hovering over an element so highlight it
function clickHide_mouseOver(e)
{
lastMouseOverEvent = e;
getBlockableElementOrAncestor(e.target, function(element)
{
if (e == lastMouseOverEvent)
{
lastMouseOverEvent = null;
if (clickHide_activated)
{
if (currentElement)
unhighlightElement(currentElement);
if (element)
{
highlightElement(element, "#d6d84b", "#f8fa47");
element.addEventListener("contextmenu", clickHide_elementClickHandler, true);
}
currentElement = element;
}
}
});
e.stopPropagation();
}
// No longer hovering over this element so unhighlight it
function clickHide_mouseOut(e)
{
if (!clickHide_activated || currentElement != e.target)
return;
unhighlightElement(currentElement);
currentElement.removeEventListener("contextmenu", clickHide_elementClickHandler, true);
e.stopPropagation();
}
// Selects the currently hovered-over filter or cancels selection
function clickHide_keyDown(e)
{
if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 13 /*DOM_VK_RETURN*/)
clickHide_mouseClick(e);
else if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 27 /*DOM_VK_ESCAPE*/)
{
ext.backgroundPage.sendMessage(
{
type: "forward",
payload:
{
type: "clickhide-deactivate"
}
});
e.preventDefault();
e.stopPropagation();
}
}
function getFiltersForElement(element, callback)
{
ext.backgroundPage.sendMessage(
{
type: "compose-filters",
tagName: element.localName,
id: element.id,
src: element.getAttribute("src"),
style: element.getAttribute("style"),
classes: [].slice.call(element.classList),
urls: getURLsFromElement(element),
mediatype: typeMap[element.localName],
baseURL: document.location.href
},
function(response)
{
callback(response.filters, response.selectors);
}
);
}
// When the user clicks, the currentElement is the one we want.
// We should have ABP rules ready for when the
// popup asks for them.
function clickHide_mouseClick(e)
{
if (!currentElement || !clickHide_activated)
return;
var elt = currentElement;
if (currentElement.classList.contains("__adblockplus__overlay"))
elt = currentElement.prisoner;
getFiltersForElement(elt, function(filters, selectors)
{
ext.backgroundPage.sendMessage(
{
type: "forward",
payload:
{
type: "clickhide-show-dialog",
clickHideFilters: filters
}
});
if (selectors.length > 0)
highlightElements(selectors.join(","));
highlightElement(currentElement, "#fd1708", "#f6a1b5");
});
// Make sure the browser doesn't handle this click
e.preventDefault();
e.stopPropagation();
}
// This function Copyright (c) 2008 Jeni Tennison, from jquery.uri.js
// and licensed under the MIT license. See jquery-*.min.js for details.
function removeDotSegments(u) {
var r = '', m = [];
if (/\./.test(u)) {
while (u !== undefined && u !== '') {
if (u === '.' || u === '..') {
u = '';
} else if (/^\.\.\//.test(u)) { // starts with ../
u = u.substring(3);
} else if (/^\.\//.test(u)) { // starts with ./
u = u.substring(2);
} else if (/^\/\.(\/|$)/.test(u)) { // starts with /./ or consists of /.
u = '/' + u.substring(3);
} else if (/^\/\.\.(\/|$)/.test(u)) { // starts with /../ or consists of /..
u = '/' + u.substring(4);
r = r.replace(/\/?[^\/]+$/, '');
} else {
m = u.match(/^(\/?[^\/]*)(\/.*)?$/);
u = m[2];
r = r + m[1];
}
}
return r;
} else {
return u;
}
}
// In Chrome 37-40, the document_end content script (this one) runs properly, while the
// document_start content scripts (that defines ext) might not. Check whether variable ext
// exists before continuing to avoid "Uncaught ReferenceError: ext is not defined".
// See https://crbug.com/416907
if ("ext" in window && document instanceof HTMLDocument)
{
// Use a contextmenu handler to save the last element the user right-clicked on.
// To make things easier, we actually save the DOM event.
// We have to do this because the contextMenu API only provides a URL, not the actual
// DOM element.
document.addEventListener('contextmenu', function(e)
{
lastRightClickEvent = e;
// We also need to ensure any old lastRightClickEvent variables in other
// frames are cleared.
lastRightClickEventValid = true;
ext.backgroundPage.sendMessage(
{
type: "forward",
payload:
{
type: "clickhide-clear-last-right-click-event"
}
});
}, true);
document.addEventListener("click", function(event)
{
// Ignore right-clicks
if (event.button == 2)
return;
// Search the link associated with the click
var link = event.target;
while (!(link instanceof HTMLAnchorElement))
{
link = link.parentNode;
if (!link)
return;
}
if (link.protocol == "http:" || link.protocol == "https:")
{
if (link.host != "subscribe.adblockplus.org" || link.pathname != "/")
return;
}
else if (!/^abp:\/*subscribe\/*\?/i.test(link.href))
return;
// This is our link - make sure the browser doesn't handle it
event.preventDefault();
event.stopPropagation();
// Decode URL parameters
var params = link.search.substr(1).split("&");
var title = null;
var url = null;
for (var i = 0; i < params.length; i++)
{
var parts = params[i].split("=", 2);
if (parts.length != 2 || !/\S/.test(parts[1]))
continue;
switch (parts[0])
{
case "title":
title = decodeURIComponent(parts[1]);
break;
case "location":
url = decodeURIComponent(parts[1]);
break;
}
}
if (!url)
return;
// Default title to the URL
if (!title)
title = url;
// Trim spaces in title and URL
title = title.trim();
url = url.trim();
if (!/^(https?|ftp):/.test(url))
return;
ext.backgroundPage.sendMessage({
type: "add-subscription",
title: title,
url: url
});
}, true);
ext.onMessage.addListener(function(msg, sender, sendResponse)
{
switch (msg.type)
{
case "get-clickhide-state":
sendResponse({active: clickHide_activated});
break;
case "clickhide-activate":
clickHide_activate();
break;
case "clickhide-deactivate":
clickHide_deactivate();
break;
case "clickhide-new-filter":
if(lastRightClickEvent)
{
var event = lastRightClickEvent;
getBlockableElementOrAncestor(event.target, function(element)
{
clickHide_activate();
currentElement = element;
clickHide_mouseClick(event);
});
}
break;
case "clickhide-init":
if (clickHideFiltersDialog)
{
sendResponse({filters: clickHide_filters});
clickHideFiltersDialog.style.width = msg.width + "px";
clickHideFiltersDialog.style.height = msg.height + "px";
clickHideFiltersDialog.style.visibility = "visible";
}
break;
case "clickhide-move":
if (clickHideFiltersDialog)
{
var rect = clickHideFiltersDialog.getBoundingClientRect();
var x = Math.max(0, Math.min(rect.left + msg.x, window.innerWidth - rect.width));
var y = Math.max(0, Math.min(rect.top + msg.y, window.innerHeight - rect.height));
clickHideFiltersDialog.style.left = x + "px";
clickHideFiltersDialog.style.top = y + "px";
}
break;
case "clickhide-close":
if (currentElement && msg.remove)
{
// Hide the selected element itself if an added blocking
// filter is causing it to collapse. Note that this
// behavior is incomplete, but the best we can do here,
// e.g. if an added blocking filter matches other elements,
// the effect won't be visible until the page is is reloaded.
checkCollapse(currentElement.prisoner || currentElement);
// Apply added element hiding filters.
updateStylesheet();
}
clickHide_deactivate();
break;
case "clickhide-show-dialog":
clickHide_rulesPending();
if (window.self == window.top)
clickHide_showDialog(msg.clickHideFilters);
break;
case "clickhide-clear-last-right-click-event":
if (lastRightClickEventValid)
lastRightClickEventValid = false;
else
lastRightClickEvent = null;
break;
}
});
if (window == window.top)
ext.backgroundPage.sendMessage({type: "report-html-page"});
}