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 function reinjectStyleSheetWhenRemoved(document, style) | |
348 { | |
349 if (!MutationObserver) | |
350 return null; | |
351 | |
352 var rules = style.sheet.rules; | |
353 var parentNode = style.parentNode; | |
354 var observer = new MutationObserver(function() | |
355 { | |
356 if (style.parentNode != parentNode) | |
357 { | |
358 parentNode.appendChild(style); | |
359 | |
360 if (style.sheet.rules.length == 0) | |
361 { | |
362 for (var i = 0; i < rules.length; i++) | |
363 style.sheet.addRule(rules[i].selectorText, "display: none !important;"
); | |
364 | |
365 style.id = id; | |
366 runInPage( | |
367 function(id) | |
368 { | |
369 var style = document.getElementById(id) || | |
370 document.documentElement.shadowRoot.getElementById(id); | |
371 style.removeAttribute("id"); | |
372 Object.defineProperty(style.sheet, "disabled", | |
373 {value: false, enumerable: true}); | |
374 }, id | |
375 ); | |
376 } | |
377 } | |
378 }); | |
379 | |
380 observer.observe(parentNode, {childList: true}); | |
381 return observer; | |
382 } | |
383 | |
384 function protectStyleSheet(document, style) | |
385 { | |
386 style.id = id; | |
387 | |
388 runInPage(function(id) | |
389 { | |
390 var style = document.getElementById(id) || | |
391 document.documentElement.shadowRoot.getElementById(id); | |
392 style.removeAttribute("id"); | |
393 | |
394 var disableables = [style, style.sheet]; | |
395 for (var i = 0; i < disableables.length; i++) | |
396 Object.defineProperty(disableables[i], "disabled", | |
397 {value: false, enumerable: true}); | |
398 | |
399 ["deleteRule", "removeRule"].forEach(function(method) | |
400 { | |
401 var original = CSSStyleSheet.prototype[method]; | |
402 CSSStyleSheet.prototype[method] = function(index) | |
403 { | |
404 if (this != style.sheet) | |
405 original.call(this, index); | |
406 }; | |
407 }); | |
408 }, id); | |
409 } | |
410 | |
411 // 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 |
412 // 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 |
413 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket | 348 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket |
414 // connections from being opened. | 349 // connections from being opened. |
415 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | 350 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 |
416 function wrapWebSocket() | 351 function wrapWebSocket(document) |
417 { | 352 { |
418 if (typeof WebSocket == "undefined") | 353 if (typeof WebSocket == "undefined") |
419 return; | 354 return; |
420 | 355 |
421 var eventName = "abpws-" + id; | 356 var eventName = "abpws-" + Math.random().toString(36).substr(2); |
422 | 357 |
423 document.addEventListener(eventName, function(event) | 358 document.addEventListener(eventName, function(event) |
424 { | 359 { |
425 ext.backgroundPage.sendMessage({ | 360 ext.backgroundPage.sendMessage({ |
426 type: "websocket-request", | 361 type: "websocket-request", |
427 url: event.detail.url | 362 url: event.detail.url |
428 }, function (block) | 363 }, function (block) |
429 { | 364 { |
430 document.dispatchEvent( | 365 document.dispatchEvent( |
431 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) | 366 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) |
432 ); | 367 ); |
433 }); | 368 }); |
434 }); | 369 }); |
435 | 370 |
436 runInPage(function(eventName) | 371 runInDocument(document, function(eventName) |
437 { | 372 { |
438 // 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 |
439 // sabotaged by the website later in order to circumvent us. | 374 // sabotaged by the website later in order to circumvent us. |
440 var RealWebSocket = WebSocket; | 375 var RealWebSocket = WebSocket; |
441 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl
ose); | 376 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl
ose); |
442 var addEventListener = document.addEventListener.bind(document); | 377 var addEventListener = document.addEventListener.bind(document); |
443 var removeEventListener = document.removeEventListener.bind(document); | 378 var removeEventListener = document.removeEventListener.bind(document); |
444 var dispatchEvent = document.dispatchEvent.bind(document); | 379 var dispatchEvent = document.dispatchEvent.bind(document); |
445 var CustomEvent = window.CustomEvent; | 380 var CustomEvent = window.CustomEvent; |
446 | 381 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 }, eventName); | 423 }, eventName); |
489 } | 424 } |
490 | 425 |
491 function init(document) | 426 function init(document) |
492 { | 427 { |
493 var shadow = null; | 428 var shadow = null; |
494 var style = null; | 429 var style = null; |
495 var observer = null; | 430 var observer = null; |
496 var tracer = null; | 431 var tracer = null; |
497 | 432 |
498 wrapWebSocket(); | 433 wrapWebSocket(document); |
499 | 434 |
500 function getPropertyFilters(callback) | 435 function getPropertyFilters(callback) |
501 { | 436 { |
502 ext.backgroundPage.sendMessage({ | 437 ext.backgroundPage.sendMessage({ |
503 type: "filters.get", | 438 type: "filters.get", |
504 what: "cssproperties" | 439 what: "cssproperties" |
505 }, callback); | 440 }, callback); |
506 } | 441 } |
507 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, | 442 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, |
508 addElemHideSelectors); | 443 addElemHideSelectors); |
509 | 444 |
510 // 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 |
511 // the order of their own <style> tags (#309). | 446 // the order of their own <style> tags (#309). |
512 // | 447 // |
513 // However, creating a shadow root breaks running CSS transitions. So we | 448 // However, creating a shadow root breaks running CSS transitions. So we |
514 // have to create the shadow root before transistions might start (#452). | 449 // have to create the shadow root before transistions might start (#452). |
515 // | 450 // |
516 // Also, using shadow DOM causes issues on some Google websites, | 451 // Also, using shadow DOM causes issues on some Google websites, |
517 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). | 452 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). |
518 if ("createShadowRoot" in document.documentElement && | 453 if ("createShadowRoot" in document.documentElement && |
519 !/\.(?:google|blogger)\.com$/.test(document.domain)) | 454 !/\.(?:google|blogger)\.com$/.test(document.domain)) |
520 { | 455 { |
521 shadow = document.documentElement.createShadowRoot(); | 456 shadow = document.documentElement.createShadowRoot(); |
522 shadow.appendChild(document.createElement("shadow")); | 457 shadow.appendChild(document.createElement("shadow")); |
| 458 |
| 459 // Stop the website from messing with our shadowRoot |
| 460 runInDocument(document, function() |
| 461 { |
| 462 var ourShadowRoot = document.documentElement.shadowRoot; |
| 463 var desc = Object.getOwnPropertyDescriptor(Element.prototype, "shadowRoot"
); |
| 464 var shadowRoot = Function.prototype.call.bind(desc.get); |
| 465 |
| 466 Object.defineProperty(Element.prototype, "shadowRoot", { |
| 467 conifgurable: true, enumerable: true, get: function() |
| 468 { |
| 469 var shadow = shadowRoot(this); |
| 470 return shadow == ourShadowRoot ? null : shadow; |
| 471 } |
| 472 }); |
| 473 }, null); |
523 } | 474 } |
524 | 475 |
525 function addElemHideSelectors(selectors) | 476 function addElemHideSelectors(selectors) |
526 { | 477 { |
527 if (selectors.length == 0) | 478 if (selectors.length == 0) |
528 return; | 479 return; |
529 | 480 |
530 if (!style) | 481 if (!style) |
531 { | 482 { |
532 // 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 |
533 // the shadow DOM if possible. Otherwise fallback to the <head> or | 484 // the shadow DOM if possible. Otherwise fallback to the <head> or |
534 // <html> element. If we have injected a style element before that | 485 // <html> element. If we have injected a style element before that |
535 // has been removed (the sheet property is null), create a new one. | 486 // has been removed (the sheet property is null), create a new one. |
536 style = document.createElement("style"); | 487 style = document.createElement("style"); |
537 (shadow || document.head || document.documentElement).appendChild(style); | 488 (shadow || document.head || document.documentElement).appendChild(style); |
538 | 489 |
539 // It can happen that the frame already navigated to a different | 490 // It can happen that the frame already navigated to a different |
540 // document while we were waiting for the background page to respond. | 491 // document while we were waiting for the background page to respond. |
541 // In that case the sheet property will stay null, after addind the | 492 // In that case the sheet property will stay null, after addind the |
542 // <style> element to the shadow DOM. | 493 // <style> element to the shadow DOM. |
543 if (!style.sheet) | 494 if (!style.sheet) |
544 return; | 495 return; |
545 | |
546 observer = reinjectStyleSheetWhenRemoved(document, style); | |
547 protectStyleSheet(document, style); | |
548 } | 496 } |
549 | 497 |
550 // If using shadow DOM, we have to add the ::content pseudo-element | 498 // If using shadow DOM, we have to add the ::content pseudo-element |
551 // before each selector, in order to match elements within the | 499 // before each selector, in order to match elements within the |
552 // insertion point. | 500 // insertion point. |
553 if (shadow) | 501 if (shadow) |
554 { | 502 { |
555 var preparedSelectors = []; | 503 var preparedSelectors = []; |
556 for (var i = 0; i < selectors.length; i++) | 504 for (var i = 0; i < selectors.length; i++) |
557 { | 505 { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 }, true); | 608 }, true); |
661 | 609 |
662 return updateStylesheet; | 610 return updateStylesheet; |
663 } | 611 } |
664 | 612 |
665 if (document instanceof HTMLDocument) | 613 if (document instanceof HTMLDocument) |
666 { | 614 { |
667 checkSitekey(); | 615 checkSitekey(); |
668 window.updateStylesheet = init(document); | 616 window.updateStylesheet = init(document); |
669 } | 617 } |
LEFT | RIGHT |