| 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 ); | 170 ); |
| 172 } | 171 } |
| 173 } | 172 } |
| 174 ); | 173 ); |
| 175 } | 174 } |
| 176 | 175 |
| 177 function checkSitekey() | 176 function checkSitekey() |
| 178 { | 177 { |
| 179 var attr = document.documentElement.getAttribute("data-adblockkey"); | 178 var attr = document.documentElement.getAttribute("data-adblockkey"); |
| 180 if (attr) | 179 if (attr) |
| 181 ext.backgroundPage.sendMessage({type: "filter.addKey", token: attr}); | 180 ext.backgroundPage.sendMessage({type: "filters.addKey", token: attr}); |
| 182 } | 181 } |
| 183 | 182 |
| 184 function getContentDocument(element) | 183 function getContentDocument(element) |
| 185 { | 184 { |
| 186 try | 185 try |
| 187 { | 186 { |
| 188 return element.contentDocument; | 187 return element.contentDocument; |
| 189 } | 188 } |
| 190 catch (e) | 189 catch (e) |
| 191 { | 190 { |
| (...skipping 135 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 reinjectStyleSheetWhenRemoved(document, style) | 336 function runInDocument(document, fn, arg) |
| 338 { | 337 { |
| 339 if (!MutationObserver) | |
| 340 return null; | |
| 341 | |
| 342 var rules = style.sheet.rules; | |
| 343 var parentNode = style.parentNode; | |
| 344 var observer = new MutationObserver(function() | |
| 345 { | |
| 346 if (style.parentNode != parentNode) | |
| 347 { | |
| 348 parentNode.appendChild(style); | |
| 349 | |
| 350 if (style.sheet.rules.length == 0) | |
| 351 { | |
| 352 for (var i = 0; i < rules.length; i += 1) | |
| 353 style.sheet.addRule(rules[i].selectorText, "display: none !important;"
); | |
| 354 | |
| 355 style.id = id; | |
| 356 injectJS( | |
| 357 function(id) | |
| 358 { | |
| 359 var style = document.getElementById(id) || | |
| 360 document.documentElement.shadowRoot.getElementById(id); | |
| 361 style.removeAttribute("id"); | |
| 362 Object.defineProperty(style.sheet, "disabled", | |
| 363 {value: false, enumerable: true}); | |
| 364 }, id | |
| 365 ); | |
| 366 } | |
| 367 } | |
| 368 }); | |
| 369 | |
| 370 observer.observe(parentNode, {childList: true}); | |
| 371 return observer; | |
| 372 } | |
| 373 | |
| 374 function injectJS(f) | |
| 375 { | |
| 376 var args = JSON.stringify(Array.prototype.slice.call(arguments, 1)); | |
| 377 args = args.substring(1, args.length - 1); | |
| 378 var codeString = "(" + f.toString() + ")(" + args + ");"; | |
| 379 | |
| 380 var script = document.createElement("script"); | 338 var script = document.createElement("script"); |
| 339 script.type = "application/javascript"; |
| 381 script.async = false; | 340 script.async = false; |
| 382 script.textContent = codeString; | 341 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; |
| 383 document.documentElement.appendChild(script); | 342 document.documentElement.appendChild(script); |
| 384 document.documentElement.removeChild(script); | 343 document.documentElement.removeChild(script); |
| 385 } | |
| 386 | |
| 387 function protectStyleSheet(document, style) | |
| 388 { | |
| 389 style.id = id; | |
| 390 | |
| 391 var protector = function(id) | |
| 392 { | |
| 393 var style = document.getElementById(id) || | |
| 394 document.documentElement.shadowRoot.getElementById(id); | |
| 395 style.removeAttribute("id"); | |
| 396 | |
| 397 var i; | |
| 398 var disableables = [style, style.sheet]; | |
| 399 for (i = 0; i < disableables.length; i += 1) | |
| 400 Object.defineProperty(disableables[i], "disabled", | |
| 401 {value: false, enumerable: true}); | |
| 402 | |
| 403 var methods = ["deleteRule", "removeRule"]; | |
| 404 for (i = 0; i < methods.length; i += 1) | |
| 405 { | |
| 406 if (methods[i] in CSSStyleSheet.prototype) | |
| 407 { | |
| 408 (function(method) | |
| 409 { | |
| 410 var original = CSSStyleSheet.prototype[method]; | |
| 411 CSSStyleSheet.prototype[method] = function(index) | |
| 412 { | |
| 413 if (this != style.sheet) | |
| 414 original.call(this, index); | |
| 415 }; | |
| 416 }(methods[i])); | |
| 417 } | |
| 418 } | |
| 419 }; | |
| 420 | |
| 421 injectJS(protector, id); | |
| 422 } | 344 } |
| 423 | 345 |
| 424 // 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 |
| 425 // 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 |
| 426 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket | 348 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket |
| 427 // connections from being opened. | 349 // connections from being opened. |
| 428 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | 350 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 |
| 429 function wrapWebSocket() | 351 function wrapWebSocket(document) |
| 430 { | 352 { |
| 431 if (typeof WebSocket == "undefined") | 353 if (typeof WebSocket == "undefined") |
| 432 return; | 354 return; |
| 433 | 355 |
| 434 var eventName = "abpws-" + id; | 356 var eventName = "abpws-" + Math.random().toString(36).substr(2); |
| 435 | 357 |
| 436 document.addEventListener(eventName, function(event) | 358 document.addEventListener(eventName, function(event) |
| 437 { | 359 { |
| 438 ext.backgroundPage.sendMessage({ | 360 ext.backgroundPage.sendMessage({ |
| 439 type: "websocket-request", | 361 type: "websocket-request", |
| 440 url: event.detail.url | 362 url: event.detail.url |
| 441 }, function (block) | 363 }, function (block) |
| 442 { | 364 { |
| 443 document.dispatchEvent( | 365 document.dispatchEvent( |
| 444 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) | 366 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) |
| 445 ); | 367 ); |
| 446 }); | 368 }); |
| 447 }); | 369 }); |
| 448 | 370 |
| 449 function wrapper(eventName) | 371 runInDocument(document, function(eventName) |
| 450 { | 372 { |
| 451 // 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 |
| 452 // sabotaged by the website later in order to circumvent us. | 374 // sabotaged by the website later in order to circumvent us. |
| 453 var RealWebSocket = WebSocket; | 375 var RealWebSocket = WebSocket; |
| 454 var closeWebSocket = RealWebSocket.prototype.close; | 376 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl
ose); |
| 455 var document = window.document; | 377 var addEventListener = document.addEventListener.bind(document); |
| 456 var addEventListener = document.addEventListener; | 378 var removeEventListener = document.removeEventListener.bind(document); |
| 457 var removeEventListener = document.removeEventListener; | 379 var dispatchEvent = document.dispatchEvent.bind(document); |
| 458 var dispatchEvent = document.dispatchEvent; | |
| 459 var CustomEvent = window.CustomEvent; | 380 var CustomEvent = window.CustomEvent; |
| 460 var boundCall = Function.prototype.call.bind(Function.prototype.call); | 381 |
| 461 // (These two functions are usually the same, but since Safari 9 considers | 382 function checkRequest(url, callback) |
| 462 // WebSocket to be an object rather than a function we must track both.) | |
| 463 var toString = Function.prototype.toString; | |
| 464 var wsToString = RealWebSocket.toString; | |
| 465 | |
| 466 function checkRequest(url, protocols, callback) | |
| 467 { | 383 { |
| 468 var incomingEventName = eventName + "-" + url; | 384 var incomingEventName = eventName + "-" + url; |
| 469 function listener(event) | 385 function listener(event) |
| 470 { | 386 { |
| 471 callback(event.detail); | 387 callback(event.detail); |
| 472 boundCall(removeEventListener, document, incomingEventName, listener); | 388 removeEventListener(incomingEventName, listener); |
| 473 } | 389 } |
| 474 boundCall(addEventListener, document, incomingEventName, listener); | 390 addEventListener(incomingEventName, listener); |
| 475 | 391 |
| 476 boundCall(dispatchEvent, document, new CustomEvent(eventName, { | 392 dispatchEvent(new CustomEvent(eventName, { |
| 477 detail: {url: url, protocols: protocols} | 393 detail: {url: url} |
| 478 })); | 394 })); |
| 479 } | 395 } |
| 480 | 396 |
| 481 function wrappedToString() | 397 WebSocket = function WrappedWebSocket(url, protocols) |
| 482 { | 398 { |
| 483 if (this === WebSocket) | 399 // Throw correct exceptions if the constructor is used improperly. |
| 484 return boundCall(wsToString, RealWebSocket); | 400 if (!(this instanceof WrappedWebSocket)) return RealWebSocket(); |
| 485 if (this === wrappedToString) | 401 if (arguments.length < 1) return new RealWebSocket(); |
| 486 return boundCall(toString, toString); | 402 |
| 487 return boundCall(toString, this); | |
| 488 }; | |
| 489 Function.prototype.toString = wrappedToString; | |
| 490 | |
| 491 WebSocket = function(url, protocols) | |
| 492 { | |
| 493 var websocket = new RealWebSocket(url, protocols); | 403 var websocket = new RealWebSocket(url, protocols); |
| 494 | 404 |
| 495 checkRequest(url, protocols, function(blocked) | 405 checkRequest(websocket.url, function(blocked) |
| 496 { | 406 { |
| 497 if (blocked) | 407 if (blocked) |
| 498 boundCall(closeWebSocket, websocket); | 408 closeWebSocket(websocket); |
| 499 }); | 409 }); |
| 500 | 410 |
| 501 return websocket; | 411 return websocket; |
| 502 }; | 412 }.bind(); |
| 413 |
| 503 Object.defineProperties(WebSocket, { | 414 Object.defineProperties(WebSocket, { |
| 504 CONNECTING: {value: 0, enumerable: true}, | 415 CONNECTING: {value: RealWebSocket.CONNECTING, enumerable: true}, |
| 505 OPEN: {value: 1, enumerable: true}, | 416 OPEN: {value: RealWebSocket.OPEN, enumerable: true}, |
| 506 CLOSING: {value: 2, enumerable: true}, | 417 CLOSING: {value: RealWebSocket.CLOSING, enumerable: true}, |
| 507 CLOSED: {value: 3, enumerable: true} | 418 CLOSED: {value: RealWebSocket.CLOSED, enumerable: true}, |
| 419 prototype: {value: RealWebSocket.prototype} |
| 508 }); | 420 }); |
| 509 WebSocket.prototype = RealWebSocket.prototype; | 421 |
| 510 RealWebSocket.prototype.constructor = WebSocket; | 422 RealWebSocket.prototype.constructor = WebSocket; |
| 511 } | 423 }, eventName); |
| 512 | |
| 513 injectJS(wrapper, eventName); | |
| 514 } | 424 } |
| 515 | 425 |
| 516 function init(document) | 426 function init(document) |
| 517 { | 427 { |
| 518 var shadow = null; | 428 var shadow = null; |
| 519 var style = null; | 429 var style = null; |
| 520 var observer = null; | 430 var observer = null; |
| 521 var tracer = null; | 431 var tracer = null; |
| 522 | 432 |
| 523 wrapWebSocket(); | 433 wrapWebSocket(document); |
| 524 | 434 |
| 525 function getPropertyFilters(callback) | 435 function getPropertyFilters(callback) |
| 526 { | 436 { |
| 527 ext.backgroundPage.sendMessage({ | 437 ext.backgroundPage.sendMessage({ |
| 528 type: "filters.get", | 438 type: "filters.get", |
| 529 what: "cssproperties" | 439 what: "cssproperties" |
| 530 }, callback); | 440 }, callback); |
| 531 } | 441 } |
| 532 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, | 442 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, |
| 533 addElemHideSelectors); | 443 addElemHideSelectors); |
| 534 | 444 |
| 535 // 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 |
| 536 // the order of their own <style> tags (#309). | 446 // the order of their own <style> tags (#309). |
| 537 // | 447 // |
| 538 // However, creating a shadow root breaks running CSS transitions. So we | 448 // However, creating a shadow root breaks running CSS transitions. So we |
| 539 // have to create the shadow root before transistions might start (#452). | 449 // have to create the shadow root before transistions might start (#452). |
| 540 // | 450 // |
| 541 // Also, using shadow DOM causes issues on some Google websites, | 451 // Also, using shadow DOM causes issues on some Google websites, |
| 542 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). | 452 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). |
| 543 if ("createShadowRoot" in document.documentElement && | 453 if ("createShadowRoot" in document.documentElement && |
| 544 !/\.(?:google|blogger)\.com$/.test(document.domain)) | 454 !/\.(?:google|blogger)\.com$/.test(document.domain)) |
| 545 { | 455 { |
| 546 shadow = document.documentElement.createShadowRoot(); | 456 shadow = document.documentElement.createShadowRoot(); |
| 547 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); |
| 548 } | 474 } |
| 549 | 475 |
| 550 function addElemHideSelectors(selectors) | 476 function addElemHideSelectors(selectors) |
| 551 { | 477 { |
| 552 if (selectors.length == 0) | 478 if (selectors.length == 0) |
| 553 return; | 479 return; |
| 554 | 480 |
| 555 if (!style) | 481 if (!style) |
| 556 { | 482 { |
| 557 // 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 |
| 558 // the shadow DOM if possible. Otherwise fallback to the <head> or | 484 // the shadow DOM if possible. Otherwise fallback to the <head> or |
| 559 // <html> element. If we have injected a style element before that | 485 // <html> element. If we have injected a style element before that |
| 560 // 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. |
| 561 style = document.createElement("style"); | 487 style = document.createElement("style"); |
| 562 (shadow || document.head || document.documentElement).appendChild(style); | 488 (shadow || document.head || document.documentElement).appendChild(style); |
| 563 | 489 |
| 564 // It can happen that the frame already navigated to a different | 490 // It can happen that the frame already navigated to a different |
| 565 // document while we were waiting for the background page to respond. | 491 // document while we were waiting for the background page to respond. |
| 566 // 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 |
| 567 // <style> element to the shadow DOM. | 493 // <style> element to the shadow DOM. |
| 568 if (!style.sheet) | 494 if (!style.sheet) |
| 569 return; | 495 return; |
| 570 | |
| 571 observer = reinjectStyleSheetWhenRemoved(document, style); | |
| 572 protectStyleSheet(document, style); | |
| 573 } | 496 } |
| 574 | 497 |
| 575 // 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 |
| 576 // before each selector, in order to match elements within the | 499 // before each selector, in order to match elements within the |
| 577 // insertion point. | 500 // insertion point. |
| 578 if (shadow) | 501 if (shadow) |
| 579 { | 502 { |
| 580 var preparedSelectors = []; | 503 var preparedSelectors = []; |
| 581 for (var i = 0; i < selectors.length; i++) | 504 for (var i = 0; i < selectors.length; i++) |
| 582 { | 505 { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 }, true); | 608 }, true); |
| 686 | 609 |
| 687 return updateStylesheet; | 610 return updateStylesheet; |
| 688 } | 611 } |
| 689 | 612 |
| 690 if (document instanceof HTMLDocument) | 613 if (document instanceof HTMLDocument) |
| 691 { | 614 { |
| 692 checkSitekey(); | 615 checkSitekey(); |
| 693 window.updateStylesheet = init(document); | 616 window.updateStylesheet = init(document); |
| 694 } | 617 } |
| LEFT | RIGHT |