| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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); | |
| 20 | 21 |
| 21 var typeMap = { | 22 var typeMap = { |
| 22 "img": "IMAGE", | 23 "img": "IMAGE", |
| 23 "input": "IMAGE", | 24 "input": "IMAGE", |
| 24 "picture": "IMAGE", | 25 "picture": "IMAGE", |
| 25 "audio": "MEDIA", | 26 "audio": "MEDIA", |
| 26 "video": "MEDIA", | 27 "video": "MEDIA", |
| 27 "frame": "SUBDOCUMENT", | 28 "frame": "SUBDOCUMENT", |
| 28 "iframe": "SUBDOCUMENT", | 29 "iframe": "SUBDOCUMENT", |
| 29 "object": "OBJECT", | 30 "object": "OBJECT", |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 var observer = new MutationObserver(function() | 343 var observer = new MutationObserver(function() |
| 343 { | 344 { |
| 344 if (style.parentNode != parentNode) | 345 if (style.parentNode != parentNode) |
| 345 parentNode.appendChild(style); | 346 parentNode.appendChild(style); |
| 346 }); | 347 }); |
| 347 | 348 |
| 348 observer.observe(parentNode, {childList: true}); | 349 observer.observe(parentNode, {childList: true}); |
| 349 return observer; | 350 return observer; |
| 350 } | 351 } |
| 351 | 352 |
| 353 function injectJS(f) | |
| 354 { | |
| 355 var args = JSON.stringify(Array.prototype.slice.call(arguments, 1)); | |
| 356 args = args.substring(1, args.length - 1); | |
| 357 var codeString = "(" + f.toString() + ")(" + args + ");"; | |
| 358 | |
| 359 var script = document.createElement("script"); | |
| 360 script.async = false; | |
| 361 script.textContent = codeString; | |
| 362 document.documentElement.appendChild(script); | |
| 363 document.documentElement.removeChild(script); | |
| 364 } | |
| 365 | |
| 352 function protectStyleSheet(document, style) | 366 function protectStyleSheet(document, style) |
| 353 { | 367 { |
| 354 var id = Math.random().toString(36).substr(2) | |
| 355 style.id = id; | 368 style.id = id; |
| 356 | 369 |
| 357 var code = [ | 370 var protector = function(id) |
| 358 "(function()", | |
| 359 "{", | |
| 360 ' var style = document.getElementById("' + id + '") ||', | |
| 361 ' document.documentElement.shadowRoot.getElementById("' + id + '");', | |
| 362 ' style.removeAttribute("id");' | |
| 363 ]; | |
| 364 | |
| 365 var disableables = ["style", "style.sheet"]; | |
| 366 for (var i = 0; i < disableables.length; i++) | |
| 367 { | 371 { |
| 368 code.push(" Object.defineProperty(" + disableables[i] + ', "disabled", ' | 372 var style = document.getElementById(id) || |
| 369 + "{value: false, enumerable: true});") ; | 373 document.documentElement.shadowRoot.getElementById(id); |
| 374 style.removeAttribute("id"); | |
| 375 | |
| 376 var i; | |
| 377 var disableables = [style, style.sheet]; | |
| 378 for (i = 0; i < disableables.length; i += 1) | |
| 379 Object.defineProperty(disableables[i], "disabled", | |
| 380 {value: false, enumerable: true}); | |
| 381 | |
| 382 var methods = ["deleteRule", "removeRule"]; | |
| 383 for (i = 0; i < methods.length; i += 1) | |
| 384 { | |
| 385 if (methods[i] in CSSStyleSheet.prototype) | |
| 386 { | |
| 387 (function(method) | |
| 388 { | |
| 389 var original = CSSStyleSheet.prototype[method]; | |
| 390 CSSStyleSheet.prototype[method] = function(index) | |
| 391 { | |
| 392 if (this != style.sheet) | |
| 393 original.call(this, index); | |
| 394 }; | |
| 395 }(methods[i])); | |
| 396 } | |
| 397 } | |
| 398 }; | |
| 399 | |
| 400 injectJS(protector, id); | |
| 401 } | |
| 402 | |
| 403 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore | |
| 404 // some ad networks are misusing them as a way to serve adverts and circumvent | |
| 405 // us. As a workaround we wrap WebSocket, closing connections that would have | |
| 406 // otherwise been blocked. | |
| 407 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | |
| 408 function wrapWebSocket() | |
| 409 { | |
| 410 if (typeof WebSocket == "undefined" || typeof WeakMap == "undefined") | |
| 411 return; | |
| 412 | |
| 413 var eventName = "abpws-" + id; | |
| 414 | |
| 415 document.addEventListener(eventName, function(event) | |
| 416 { | |
| 417 ext.backgroundPage.sendMessage({ | |
| 418 type: "websocket-request", | |
| 419 url: event.detail.url | |
| 420 }, function (block) | |
| 421 { | |
| 422 document.dispatchEvent( | |
| 423 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) | |
| 424 ); | |
| 425 }); | |
| 426 }); | |
| 427 | |
| 428 function wrapper(eventName) | |
| 429 { | |
| 430 var RealWebSocket = WebSocket; | |
| 431 | |
| 432 // To avoid detection we make our WebSocket wrapper as similar as possible. | |
| 433 // Most WebSocket properties and methods actually belong to | |
| 434 // WebSocket.prototype, but they require access to the instance state. To | |
| 435 // acheive this without adding extra properties we need to manage our own | |
| 436 // storage here. To avoid leaking memory in the case that all other | |
| 437 // references to a WebSocket have been deleted we must use a WeakMap. | |
| 438 var instanceStorage = new WeakMap(); | |
| 439 | |
| 440 // We can't create a WebSocket until we know it shouldn't be blocked, but we | |
| 441 // can only check this asynchronously. We queue up any actions that are | |
| 442 // performed on a WebSocket wrapper instance before its real WebSocket | |
| 443 // exists. If not blocked we perform queued actions on the WebSocket after | |
| 444 // it has been created. | |
| 445 function processQueue(queue, websocket) | |
| 446 { | |
| 447 for (var i = 0; i < queue.length; i += 1) | |
| 448 { | |
| 449 var action = queue[i][0]; | |
| 450 var key = queue[i][1]; | |
| 451 var value = queue[i][2]; | |
| 452 switch (action) | |
| 453 { | |
| 454 case "set": | |
| 455 websocket[key] = value; | |
| 456 break; | |
| 457 case "call": | |
| 458 websocket[key].apply(websocket, value); | |
| 459 break; | |
| 460 } | |
| 461 } | |
| 462 } | |
| 463 | |
| 464 var defaults = { | |
| 465 readyState: RealWebSocket.CONNECTING, | |
| 466 bufferedAmount: 0, | |
| 467 extensions: "", | |
| 468 binaryType: "blob" | |
| 469 }; | |
| 470 | |
| 471 WebSocket = function(url, protocol) | |
| 472 { | |
| 473 var storage = { | |
| 474 url: url, | |
| 475 protocol: protocol, | |
| 476 queue: [], | |
| 477 blocked: false, | |
| 478 websocket: null | |
| 479 }; | |
| 480 instanceStorage.set(this, storage); | |
| 481 | |
| 482 var incomingEventName = eventName + "-" + url; | |
| 483 function listener(event) | |
| 484 { | |
| 485 storage.blocked = event.detail; | |
| 486 if (!storage.blocked) | |
| 487 { | |
| 488 storage.websocket = new RealWebSocket(url, protocol); | |
| 489 processQueue(storage.queue, storage.websocket); | |
| 490 } | |
| 491 delete storage.queue; // FIXME onerror!? | |
|
kzar
2016/07/06 16:39:20
So far I don't fire the error / close event when a
kzar
2016/07/07 07:51:31
Note: My earlier implementation which always creat
| |
| 492 | |
| 493 document.removeEventListener(incomingEventName, listener); | |
| 494 } | |
| 495 document.addEventListener(incomingEventName, listener); | |
| 496 | |
| 497 document.dispatchEvent(new CustomEvent(eventName, { | |
| 498 detail: {url: url, protocol: protocol} | |
| 499 })); | |
| 500 }; | |
| 501 | |
| 502 function proxyProperties(original) | |
| 503 { | |
| 504 var properties = Object.create(null); | |
| 505 | |
| 506 Object.keys(original).map(function(key) | |
| 507 { | |
| 508 if (key == "prototype") | |
|
kzar
2016/07/06 16:39:20
(For Safari)
| |
| 509 return; | |
| 510 | |
| 511 var descriptor = Object.getOwnPropertyDescriptor(original, key); | |
| 512 | |
| 513 if (typeof descriptor.value == "function") | |
| 514 { | |
| 515 descriptor.value = function() | |
| 516 { | |
| 517 var storage = instanceStorage.get(this); | |
| 518 if (!storage) | |
|
kzar
2016/07/06 16:39:20
So that the "Uncaught TypeError: Illegal invocatio
| |
| 519 original[key].apply(original, arguments); | |
| 520 if (storage.websocket) | |
| 521 storage.websocket[key].apply(storage.websocket, arguments); | |
| 522 else if (!storage.blocked) | |
| 523 storage.queue.push(["call", key, arguments]); | |
| 524 }; | |
| 525 } | |
| 526 else if (typeof descriptor.value == "undefined") | |
| 527 { | |
| 528 descriptor.get = function() | |
| 529 { | |
| 530 var storage = instanceStorage.get(this); | |
| 531 if (!storage) | |
| 532 return original[key]; | |
| 533 if (storage.websocket) | |
| 534 return storage.websocket[key]; | |
| 535 if (storage.blocked && key == "readyState") | |
| 536 return RealWebSocket.CLOSED; | |
| 537 if (key == "url" || key == "protocol") | |
| 538 return storage[key]; | |
| 539 if (key in defaults) | |
| 540 return defaults[key]; | |
| 541 return null; | |
| 542 }; | |
| 543 descriptor.set = function(value) | |
| 544 { | |
| 545 var storage = instanceStorage.get(this); | |
| 546 if (!storage) | |
| 547 original[key] = value; | |
| 548 else if (storage.websocket) | |
| 549 storage.websocket[key] = value; | |
| 550 else if (!storage.blocked) | |
| 551 storage.queue.push(["set", key, value]); | |
| 552 return value; | |
| 553 }; | |
| 554 } | |
| 555 properties[key] = descriptor; | |
| 556 }); | |
| 557 return properties; | |
| 558 } | |
| 559 | |
| 560 Object.defineProperties(WebSocket, proxyProperties(RealWebSocket)); | |
| 561 WebSocket.prototype = Object.create( | |
| 562 RealWebSocket.prototype.__proto__, | |
| 563 proxyProperties(RealWebSocket.prototype) | |
| 564 ); | |
| 370 } | 565 } |
| 371 | 566 |
| 372 var methods = ["deleteRule", "removeRule"]; | 567 injectJS(wrapper, eventName); |
| 373 for (var j = 0; j < methods.length; j++) | |
| 374 { | |
| 375 var method = methods[j]; | |
| 376 if (method in CSSStyleSheet.prototype) | |
| 377 { | |
| 378 var origin = "CSSStyleSheet.prototype." + method; | |
| 379 code.push(" var " + method + " = " + origin + ";", | |
| 380 " " + origin + " = function(index)", | |
| 381 " {", | |
| 382 " if (this != style.sheet)", | |
| 383 " " + method + ".call(this, index);", | |
| 384 " }"); | |
| 385 } | |
| 386 } | |
| 387 | |
| 388 code.push("})();"); | |
| 389 | |
| 390 var script = document.createElement("script"); | |
| 391 script.async = false; | |
| 392 script.textContent = code.join("\n"); | |
| 393 document.documentElement.appendChild(script); | |
| 394 document.documentElement.removeChild(script); | |
| 395 } | 568 } |
| 396 | 569 |
| 397 function init(document) | 570 function init(document) |
| 398 { | 571 { |
| 399 var shadow = null; | 572 var shadow = null; |
| 400 var style = null; | 573 var style = null; |
| 401 var observer = null; | 574 var observer = null; |
| 402 var tracer = null; | 575 var tracer = null; |
| 403 | 576 |
| 577 wrapWebSocket(); | |
| 578 | |
| 404 function getPropertyFilters(callback) | 579 function getPropertyFilters(callback) |
| 405 { | 580 { |
| 406 ext.backgroundPage.sendMessage({ | 581 ext.backgroundPage.sendMessage({ |
| 407 type: "filters.get", | 582 type: "filters.get", |
| 408 what: "cssproperties" | 583 what: "cssproperties" |
| 409 }, callback); | 584 }, callback); |
| 410 } | 585 } |
| 411 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, | 586 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, |
| 412 addElemHideSelectors); | 587 addElemHideSelectors); |
| 413 | 588 |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 }, true); | 739 }, true); |
| 565 | 740 |
| 566 return updateStylesheet; | 741 return updateStylesheet; |
| 567 } | 742 } |
| 568 | 743 |
| 569 if (document instanceof HTMLDocument) | 744 if (document instanceof HTMLDocument) |
| 570 { | 745 { |
| 571 checkSitekey(); | 746 checkSitekey(); |
| 572 window.updateStylesheet = init(document); | 747 window.updateStylesheet = init(document); |
| 573 } | 748 } |
| OLD | NEW |