| 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()", | 371 { | 
| 359 "{", | 372 var style = document.getElementById(id) || | 
| 360 ' var style = document.getElementById("' + id + '") ||', | 373 document.documentElement.shadowRoot.getElementById(id); | 
| 361 ' document.documentElement.shadowRoot.getElementById("' + id + '");', | 374 style.removeAttribute("id"); | 
| 362 ' style.removeAttribute("id");' | |
| 363 ]; | |
| 364 | 375 | 
| 365 var disableables = ["style", "style.sheet"]; | 376 var i; | 
| 366 for (var i = 0; i < disableables.length; 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, preventing blocked WebSocket | |
| 406 // connections from being opened. | |
| 407 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | |
| 408 function wrapWebSocket() | |
| 409 { | |
| 410 if (typeof WebSocket == "undefined") | |
| 411 return; | |
| 412 | |
| 413 var eventName = "abpws-" + id; | |
| 414 | |
| 415 document.addEventListener(eventName, function(event) | |
| 367 { | 416 { | 
| 368 code.push(" Object.defineProperty(" + disableables[i] + ', "disabled", ' | 417 ext.backgroundPage.sendMessage({ | 
| 369 + "{value: false, enumerable: true});") ; | 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 // As far as possible we must track everything we use that could be | |
| 431 // sabotaged by the website later in order to circumvent us. | |
| 432 var RealWebSocket = WebSocket; | |
| 433 var closeWebSocket = RealWebSocket.prototype.close; | |
| 
 
Sebastian Noack
2016/08/08 16:42:55
Since the real WebSocket isn't reachable anymore f
 
kzar
2016/08/08 18:19:07
Well our WebSocket wrapper gets assigned the real
 
 | |
| 434 var document = window.document; | |
| 435 var addEventListener = document.addEventListener; | |
| 436 var removeEventListener = document.removeEventListener; | |
| 
 
Sebastian Noack
2016/08/08 16:42:56
Any reason why you don't simply bind those functio
 
kzar
2016/08/08 18:19:06
Done, but unfortunately I do still need boundCall
 
 | |
| 437 var dispatchEvent = document.dispatchEvent; | |
| 438 var CustomEvent = window.CustomEvent; | |
| 439 var boundCall = Function.prototype.call.bind(Function.prototype.call); | |
| 440 var stringToString = String.prototype.toString; | |
| 441 // (These two functions are usually the same, but since Safari 9 considers | |
| 442 // WebSocket to be an object rather than a function we must track both.) | |
| 443 var functionToString = Function.prototype.toString; | |
| 444 var websocketToString = RealWebSocket.toString; | |
| 445 | |
| 446 function checkRequest(url, callback) | |
| 447 { | |
| 448 var incomingEventName = eventName + "-" + url; | |
| 449 function listener(event) | |
| 450 { | |
| 451 callback(event.detail); | |
| 452 boundCall(removeEventListener, document, incomingEventName, listener); | |
| 453 } | |
| 454 boundCall(addEventListener, document, incomingEventName, listener); | |
| 455 | |
| 456 boundCall(dispatchEvent, document, new CustomEvent(eventName, { | |
| 457 detail: {url: url} | |
| 458 })); | |
| 459 } | |
| 460 | |
| 461 function wrappedToString() | |
| 462 { | |
| 463 if (this === WebSocket) | |
| 464 return boundCall(websocketToString, RealWebSocket); | |
| 465 if (this === wrappedToString) | |
| 466 return boundCall(functionToString, functionToString); | |
| 467 return boundCall(functionToString, this); | |
| 468 }; | |
| 469 Function.prototype.toString = wrappedToString; | |
| 470 | |
| 471 WebSocket = function(url, protocols) | |
| 472 { | |
| 473 // First ensure url isn't a URL object, then make sure it's a real String. | |
| 474 // This is necessary to prevent circumvention, without breaking anything. | |
| 475 url = boundCall(stringToString, url.toString()); | |
| 
 
Sebastian Noack
2016/08/08 16:42:56
This generates a different error when url is null
 
kzar
2016/08/08 18:19:07
Ah, well spotted. Fixed.
As for _why_ I do this,
 
 | |
| 476 | |
| 477 var websocket = new RealWebSocket(url, protocols); | |
| 478 | |
| 479 checkRequest(url, function(blocked) | |
| 480 { | |
| 481 if (blocked) | |
| 482 boundCall(closeWebSocket, websocket); | |
| 483 }); | |
| 484 | |
| 485 return websocket; | |
| 486 }; | |
| 487 Object.defineProperties(WebSocket, { | |
| 488 CONNECTING: {value: 0, enumerable: true}, | |
| 489 OPEN: {value: 1, enumerable: true}, | |
| 490 CLOSING: {value: 2, enumerable: true}, | |
| 491 CLOSED: {value: 3, enumerable: true} | |
| 492 }); | |
| 493 WebSocket.prototype = RealWebSocket.prototype; | |
| 494 RealWebSocket.prototype.constructor = WebSocket; | |
| 370 } | 495 } | 
| 371 | 496 | 
| 372 var methods = ["deleteRule", "removeRule"]; | 497 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 } | 498 } | 
| 396 | 499 | 
| 397 function init(document) | 500 function init(document) | 
| 398 { | 501 { | 
| 399 var shadow = null; | 502 var shadow = null; | 
| 400 var style = null; | 503 var style = null; | 
| 401 var observer = null; | 504 var observer = null; | 
| 402 var tracer = null; | 505 var tracer = null; | 
| 403 | 506 | 
| 507 wrapWebSocket(); | |
| 508 | |
| 404 function getPropertyFilters(callback) | 509 function getPropertyFilters(callback) | 
| 405 { | 510 { | 
| 406 ext.backgroundPage.sendMessage({ | 511 ext.backgroundPage.sendMessage({ | 
| 407 type: "filters.get", | 512 type: "filters.get", | 
| 408 what: "cssproperties" | 513 what: "cssproperties" | 
| 409 }, callback); | 514 }, callback); | 
| 410 } | 515 } | 
| 411 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, | 516 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, | 
| 412 addElemHideSelectors); | 517 addElemHideSelectors); | 
| 413 | 518 | 
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 }, true); | 669 }, true); | 
| 565 | 670 | 
| 566 return updateStylesheet; | 671 return updateStylesheet; | 
| 567 } | 672 } | 
| 568 | 673 | 
| 569 if (document instanceof HTMLDocument) | 674 if (document instanceof HTMLDocument) | 
| 570 { | 675 { | 
| 571 checkSitekey(); | 676 checkSitekey(); | 
| 572 window.updateStylesheet = init(document); | 677 window.updateStylesheet = init(document); | 
| 573 } | 678 } | 
| OLD | NEW |