Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; | 18 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; |
19 var SELECTOR_GROUP_SIZE = 200; | 19 var SELECTOR_GROUP_SIZE = 200; |
20 var id = Math.random().toString(36).substr(2); | |
21 | 20 |
22 var typeMap = { | 21 var typeMap = { |
23 "img": "IMAGE", | 22 "img": "IMAGE", |
24 "input": "IMAGE", | 23 "input": "IMAGE", |
25 "picture": "IMAGE", | 24 "picture": "IMAGE", |
26 "audio": "MEDIA", | 25 "audio": "MEDIA", |
27 "video": "MEDIA", | 26 "video": "MEDIA", |
28 "frame": "SUBDOCUMENT", | 27 "frame": "SUBDOCUMENT", |
29 "iframe": "SUBDOCUMENT", | 28 "iframe": "SUBDOCUMENT", |
30 "object": "OBJECT", | 29 "object": "OBJECT", |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 }, | 326 }, |
328 | 327 |
329 disconnect: function() | 328 disconnect: function() |
330 { | 329 { |
331 this.document.removeEventListener("DOMContentLoaded", this.trace); | 330 this.document.removeEventListener("DOMContentLoaded", this.trace); |
332 this.observer.disconnect(); | 331 this.observer.disconnect(); |
333 clearTimeout(this.timeout); | 332 clearTimeout(this.timeout); |
334 } | 333 } |
335 }; | 334 }; |
336 | 335 |
337 function runInPage(fn, arg) | 336 function runInDocument(document, fn, arg) |
338 { | 337 { |
339 var script = document.createElement("script"); | 338 var script = document.createElement("script"); |
340 script.type = "application/javascript"; | 339 script.type = "application/javascript"; |
341 script.async = false; | 340 script.async = false; |
342 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; | 341 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; |
343 document.documentElement.appendChild(script); | 342 document.documentElement.appendChild(script); |
344 document.documentElement.removeChild(script); | 343 document.documentElement.removeChild(script); |
345 } | 344 } |
346 | 345 |
347 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore | 346 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore |
348 // some ad networks are misusing them as a way to serve adverts and circumvent | 347 // some ad networks are misusing them as a way to serve adverts and circumvent |
349 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket | 348 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket |
350 // connections from being opened. | 349 // connections from being opened. |
351 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | 350 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 |
352 function wrapWebSocket() | 351 function wrapWebSocket(document) |
353 { | 352 { |
354 if (typeof WebSocket == "undefined") | 353 if (typeof WebSocket == "undefined") |
355 return; | 354 return; |
356 | 355 |
357 var eventName = "abpws-" + id; | 356 var eventName = "abpws-" + Math.random().toString(36).substr(2); |
358 | 357 |
359 document.addEventListener(eventName, function(event) | 358 document.addEventListener(eventName, function(event) |
360 { | 359 { |
361 ext.backgroundPage.sendMessage({ | 360 ext.backgroundPage.sendMessage({ |
362 type: "websocket-request", | 361 type: "websocket-request", |
363 url: event.detail.url | 362 url: event.detail.url |
364 }, function (block) | 363 }, function (block) |
365 { | 364 { |
366 document.dispatchEvent( | 365 document.dispatchEvent( |
367 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) | 366 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) |
368 ); | 367 ); |
369 }); | 368 }); |
370 }); | 369 }); |
371 | 370 |
372 runInPage(function(eventName) | 371 runInDocument(document, function(eventName) |
373 { | 372 { |
374 // As far as possible we must track everything we use that could be | 373 // As far as possible we must track everything we use that could be |
375 // sabotaged by the website later in order to circumvent us. | 374 // sabotaged by the website later in order to circumvent us. |
376 var RealWebSocket = WebSocket; | 375 var RealWebSocket = WebSocket; |
377 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl ose); | 376 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl ose); |
378 var addEventListener = document.addEventListener.bind(document); | 377 var addEventListener = document.addEventListener.bind(document); |
379 var removeEventListener = document.removeEventListener.bind(document); | 378 var removeEventListener = document.removeEventListener.bind(document); |
380 var dispatchEvent = document.dispatchEvent.bind(document); | 379 var dispatchEvent = document.dispatchEvent.bind(document); |
381 var CustomEvent = window.CustomEvent; | 380 var CustomEvent = window.CustomEvent; |
382 | 381 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
424 }, eventName); | 423 }, eventName); |
425 } | 424 } |
426 | 425 |
427 function init(document) | 426 function init(document) |
428 { | 427 { |
429 var shadow = null; | 428 var shadow = null; |
430 var style = null; | 429 var style = null; |
431 var observer = null; | 430 var observer = null; |
432 var tracer = null; | 431 var tracer = null; |
433 | 432 |
434 wrapWebSocket(); | 433 wrapWebSocket(document); |
435 | 434 |
436 function getPropertyFilters(callback) | 435 function getPropertyFilters(callback) |
437 { | 436 { |
438 ext.backgroundPage.sendMessage({ | 437 ext.backgroundPage.sendMessage({ |
439 type: "filters.get", | 438 type: "filters.get", |
440 what: "cssproperties" | 439 what: "cssproperties" |
441 }, callback); | 440 }, callback); |
442 } | 441 } |
443 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, | 442 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, |
444 addElemHideSelectors); | 443 addElemHideSelectors); |
445 | 444 |
446 // Use Shadow DOM if available to don't mess with web pages that rely on | 445 // Use Shadow DOM if available to don't mess with web pages that rely on |
447 // the order of their own <style> tags (#309). | 446 // the order of their own <style> tags (#309). |
448 // | 447 // |
449 // However, creating a shadow root breaks running CSS transitions. So we | 448 // However, creating a shadow root breaks running CSS transitions. So we |
450 // have to create the shadow root before transistions might start (#452). | 449 // have to create the shadow root before transistions might start (#452). |
451 // | 450 // |
452 // Also, using shadow DOM causes issues on some Google websites, | 451 // Also, using shadow DOM causes issues on some Google websites, |
453 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). | 452 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). |
454 if ("createShadowRoot" in document.documentElement && | 453 if ("createShadowRoot" in document.documentElement && |
455 !/\.(?:google|blogger)\.com$/.test(document.domain)) | 454 !/\.(?:google|blogger)\.com$/.test(document.domain)) |
456 { | 455 { |
457 shadow = document.documentElement.createShadowRoot(); | 456 shadow = document.documentElement.createShadowRoot(); |
458 shadow.appendChild(document.createElement("shadow")); | 457 shadow.appendChild(document.createElement("shadow")); |
459 | 458 |
460 // Stop the website from messing with our shadowRoot | 459 // Stop the website from messing with our shadowRoot |
461 runInPage(function() | 460 runInDocument(document, function() |
Sebastian Noack
2016/08/11 12:48:11
You have to pass the document to runInPage(), to m
kzar
2016/08/11 13:04:46
Done.
| |
462 { | 461 { |
463 var ourShadowRoot = document.documentElement.shadowRoot; | 462 var ourShadowRoot = document.documentElement.shadowRoot; |
464 var desc = Object.getOwnPropertyDescriptor(Element.prototype, "shadowRoot" ); | 463 var desc = Object.getOwnPropertyDescriptor(Element.prototype, "shadowRoot" ); |
465 var shadowRoot = Function.prototype.call.bind(desc.get); | 464 var shadowRoot = Function.prototype.call.bind(desc.get); |
466 | 465 |
467 Object.defineProperty(Element.prototype, "shadowRoot", { | 466 Object.defineProperty(Element.prototype, "shadowRoot", { |
468 conifgurable: true, enumerable: true, get: function() | 467 conifgurable: true, enumerable: true, get: function() |
469 { | 468 { |
470 var shadow = shadowRoot(this); | 469 var shadow = shadowRoot(this); |
471 return shadow === ourShadowRoot ? null : shadow; | 470 return shadow == ourShadowRoot ? null : shadow; |
Sebastian Noack
2016/08/11 12:38:57
Nit: As per Mozilla's coding practices, we prefer
Sebastian Noack
2016/08/11 12:42:37
(The other way around, I meant, == is preferred ov
kzar
2016/08/11 12:48:18
Done.
| |
472 } | 471 } |
473 }); | 472 }); |
474 }, ""); | 473 }, null); |
Sebastian Noack
2016/08/11 12:38:57
Nit: I'd rather pass null, than creating an empty
kzar
2016/08/11 12:48:18
Done.
| |
475 } | 474 } |
476 | 475 |
477 function addElemHideSelectors(selectors) | 476 function addElemHideSelectors(selectors) |
478 { | 477 { |
479 if (selectors.length == 0) | 478 if (selectors.length == 0) |
480 return; | 479 return; |
481 | 480 |
482 if (!style) | 481 if (!style) |
483 { | 482 { |
484 // Create <style> element lazily, only if we add styles. Add it to | 483 // Create <style> element lazily, only if we add styles. Add it to |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
609 }, true); | 608 }, true); |
610 | 609 |
611 return updateStylesheet; | 610 return updateStylesheet; |
612 } | 611 } |
613 | 612 |
614 if (document instanceof HTMLDocument) | 613 if (document instanceof HTMLDocument) |
615 { | 614 { |
616 checkSitekey(); | 615 checkSitekey(); |
617 window.updateStylesheet = init(document); | 616 window.updateStylesheet = init(document); |
618 } | 617 } |
LEFT | RIGHT |