| 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 | 
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 | 348 | 
| 349 observer.observe(parentNode, {childList: true}); | 349 observer.observe(parentNode, {childList: true}); | 
| 350 return observer; | 350 return observer; | 
| 351 } | 351 } | 
| 352 | 352 | 
| 353 function runInPage(fn, arg) | 353 function runInPage(fn, arg) | 
| 354 { | 354 { | 
| 355 var script = document.createElement("script"); | 355 var script = document.createElement("script"); | 
| 356 script.type = "application/javascript"; | 356 script.type = "application/javascript"; | 
| 357 script.async = false; | 357 script.async = false; | 
| 358 | 358 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; | 
| 359 // include.youtube.js passes this function is passed a RegExp which | |
| 
 
Sebastian Noack
2016/08/09 15:09:37
This reads weird. Typo?
  In include.youtube.js,
 
kzar
2016/08/09 16:11:40
Done.
 
 | |
| 360 // JSON.stringify would convert to "{}". | |
| 361 if (!(arg instanceof RegExp)) | |
| 
 
Sebastian Noack
2016/08/09 14:53:30
It seems this wasn't necessary before. So why is i
 
Sebastian Noack
2016/08/09 15:09:37
Never mind, I got it, (misinterpreted the conditio
 
 | |
| 362 arg = JSON.stringify(arg); | |
| 363 | |
| 364 script.textContent = "(" + fn + ")(" + arg + ");"; | |
| 365 document.documentElement.appendChild(script); | 359 document.documentElement.appendChild(script); | 
| 366 document.documentElement.removeChild(script); | 360 document.documentElement.removeChild(script); | 
| 367 } | 361 } | 
| 368 | 362 | 
| 369 function protectStyleSheet(document, style) | 363 function protectStyleSheet(document, style) | 
| 370 { | 364 { | 
| 371 style.id = id; | 365 style.id = id; | 
| 372 | 366 | 
| 373 var protector = function(id) | 367 runInPage(function(id) | 
| 374 { | 368 { | 
| 375 var style = document.getElementById(id) || | 369 var style = document.getElementById(id) || | 
| 376 document.documentElement.shadowRoot.getElementById(id); | 370 document.documentElement.shadowRoot.getElementById(id); | 
| 377 style.removeAttribute("id"); | 371 style.removeAttribute("id"); | 
| 378 | 372 | 
| 379 var disableables = [style, style.sheet]; | 373 var disableables = [style, style.sheet]; | 
| 380 for (var i = 0; i < disableables.length; i++) | 374 for (var i = 0; i < disableables.length; i++) | 
| 381 Object.defineProperty(disableables[i], "disabled", | 375 Object.defineProperty(disableables[i], "disabled", | 
| 382 {value: false, enumerable: true}); | 376 {value: false, enumerable: true}); | 
| 383 | 377 | 
| 384 ["deleteRule", "removeRule"].forEach(function(method) | 378 ["deleteRule", "removeRule"].forEach(function(method) | 
| 385 { | 379 { | 
| 386 var original = CSSStyleSheet.prototype[method]; | 380 var original = CSSStyleSheet.prototype[method]; | 
| 387 CSSStyleSheet.prototype[method] = function(index) | 381 CSSStyleSheet.prototype[method] = function(index) | 
| 388 { | 382 { | 
| 389 if (this != style.sheet) | 383 if (this != style.sheet) | 
| 390 original.call(this, index); | 384 original.call(this, index); | 
| 391 }; | 385 }; | 
| 
 
Sebastian Noack
2016/08/09 15:41:15
Bind those functions as well? Otherwise we can sti
 
kzar
2016/08/09 16:11:40
Done.
 
 | |
| 392 }); | 386 }); | 
| 393 }; | 387 }, id); | 
| 394 | |
| 395 runInPage(protector, id); | |
| 396 } | 388 } | 
| 397 | 389 | 
| 398 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore | 390 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore | 
| 399 // some ad networks are misusing them as a way to serve adverts and circumvent | 391 // some ad networks are misusing them as a way to serve adverts and circumvent | 
| 400 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket | 392 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket | 
| 401 // connections from being opened. | 393 // connections from being opened. | 
| 402 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | 394 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | 
| 403 function wrapWebSocket() | 395 function wrapWebSocket() | 
| 404 { | 396 { | 
| 405 if (typeof WebSocket == "undefined") | 397 if (typeof WebSocket == "undefined") | 
| 406 return; | 398 return; | 
| 407 | 399 | 
| 408 var eventName = "abpws-" + id; | 400 var eventName = "abpws-" + id; | 
| 409 | 401 | 
| 410 document.addEventListener(eventName, function(event) | 402 document.addEventListener(eventName, function(event) | 
| 411 { | 403 { | 
| 412 ext.backgroundPage.sendMessage({ | 404 ext.backgroundPage.sendMessage({ | 
| 413 type: "websocket-request", | 405 type: "websocket-request", | 
| 414 url: event.detail.url | 406 url: event.detail.url | 
| 415 }, function (block) | 407 }, function (block) | 
| 416 { | 408 { | 
| 417 document.dispatchEvent( | 409 document.dispatchEvent( | 
| 418 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) | 410 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) | 
| 419 ); | 411 ); | 
| 420 }); | 412 }); | 
| 421 }); | 413 }); | 
| 422 | 414 | 
| 423 function wrapper(eventName) | 415 runInPage(function(eventName) | 
| 
 
Sebastian Noack
2016/08/09 14:53:30
I just noticed that the argument name is the same
 
Sebastian Noack
2016/08/09 14:53:30
I just noticed a minor inconsistency, while you us
 
kzar
2016/08/09 16:11:39
Done.
 
kzar
2016/08/09 16:11:40
I'd rather leave this being called eventName, I th
 
 | |
| 424 { | 416 { | 
| 425 // As far as possible we must track everything we use that could be | 417 // As far as possible we must track everything we use that could be | 
| 426 // sabotaged by the website later in order to circumvent us. | 418 // sabotaged by the website later in order to circumvent us. | 
| 427 var RealWebSocket = WebSocket; | 419 var RealWebSocket = WebSocket; | 
| 428 var closeWebSocket = RealWebSocket.prototype.close; | 420 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl ose); | 
| 429 var addEventListener = document.addEventListener.bind(document); | 421 var addEventListener = document.addEventListener.bind(document); | 
| 430 var removeEventListener = document.removeEventListener.bind(document); | 422 var removeEventListener = document.removeEventListener.bind(document); | 
| 431 var dispatchEvent = document.dispatchEvent.bind(document); | 423 var dispatchEvent = document.dispatchEvent.bind(document); | 
| 432 var CustomEvent = window.CustomEvent; | 424 var CustomEvent = window.CustomEvent; | 
| 433 var boundCall = Function.prototype.call.bind(Function.prototype.call); | |
| 434 var functionToString = Function.prototype.toString; | |
| 435 // (Safari 9 considers WebSocket to be an object rather than a function.) | |
| 436 var webSocketString = RealWebSocket.toString(); | |
| 437 | 425 | 
| 438 function checkRequest(url, callback) | 426 function checkRequest(url, callback) | 
| 439 { | 427 { | 
| 440 var incomingEventName = eventName + "-" + url; | 428 var incomingEventName = eventName + "-" + url; | 
| 441 function listener(event) | 429 function listener(event) | 
| 442 { | 430 { | 
| 443 callback(event.detail); | 431 callback(event.detail); | 
| 444 removeEventListener(incomingEventName, listener); | 432 removeEventListener(incomingEventName, listener); | 
| 445 } | 433 } | 
| 446 addEventListener(incomingEventName, listener); | 434 addEventListener(incomingEventName, listener); | 
| 447 | 435 | 
| 448 dispatchEvent(new CustomEvent(eventName, { | 436 dispatchEvent(new CustomEvent(eventName, { | 
| 449 detail: {url: url} | 437 detail: {url: url} | 
| 450 })); | 438 })); | 
| 451 } | 439 } | 
| 452 | 440 | 
| 453 function wrappedToString() | 441 WebSocket = function WrappedWebSocket(url, protocols) | 
| 454 { | 442 { | 
| 455 if (this === WebSocket) | 443 // Throw correct exceptions if the constructor is used improperly. | 
| 456 return webSocketString; | 444 if (!(this instanceof WrappedWebSocket)) return RealWebSocket(); | 
| 457 if (this === wrappedToString) | 445 if (arguments.length < 1) return new RealWebSocket(); | 
| 458 return boundCall(functionToString, functionToString); | 446 | 
| 459 return boundCall(functionToString, this); | |
| 460 }; | |
| 461 Function.prototype.toString = wrappedToString; | |
| 462 | |
| 463 WebSocket = function(url, protocols) | |
| 464 { | |
| 465 var websocket = new RealWebSocket(url, protocols); | 447 var websocket = new RealWebSocket(url, protocols); | 
| 466 | 448 | 
| 467 checkRequest(websocket.url, function(blocked) | 449 checkRequest(websocket.url, function(blocked) | 
| 468 { | 450 { | 
| 469 if (blocked) | 451 if (blocked) | 
| 470 boundCall(closeWebSocket, websocket); | 452 closeWebSocket(websocket); | 
| 471 }); | 453 }); | 
| 472 | 454 | 
| 473 return websocket; | 455 return websocket; | 
| 474 }; | 456 }.bind(); | 
| 475 | 457 | 
| 476 var properties = Object.getOwnPropertyNames(RealWebSocket); | 458 Object.defineProperties(WebSocket, { | 
| 477 for (var i = 0; i < properties.length; i++) | 459 CONNECTING: {value: RealWebSocket.CONNECTING, enumerable: true}, | 
| 478 { | 460 OPEN: {value: RealWebSocket.OPEN, enumerable: true}, | 
| 479 var name = properties[i]; | 461 CLOSING: {value: RealWebSocket.CLOSING, enumerable: true}, | 
| 480 var desc = Object.getOwnPropertyDescriptor(RealWebSocket, name); | 462 CLOSED: {value: RealWebSocket.CLOSED, enumerable: true}, | 
| 481 Object.defineProperty(WebSocket, name, desc); | 463 prototype: {value: RealWebSocket.prototype} | 
| 482 } | 464 }); | 
| 483 | 465 | 
| 484 RealWebSocket.prototype.constructor = WebSocket; | 466 RealWebSocket.prototype.constructor = WebSocket; | 
| 485 } | 467 }, eventName); | 
| 486 | |
| 487 runInPage(wrapper, eventName); | |
| 488 } | 468 } | 
| 489 | 469 | 
| 490 function init(document) | 470 function init(document) | 
| 491 { | 471 { | 
| 492 var shadow = null; | 472 var shadow = null; | 
| 493 var style = null; | 473 var style = null; | 
| 494 var observer = null; | 474 var observer = null; | 
| 495 var tracer = null; | 475 var tracer = null; | 
| 496 | 476 | 
| 497 wrapWebSocket(); | 477 wrapWebSocket(); | 
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 659 }, true); | 639 }, true); | 
| 660 | 640 | 
| 661 return updateStylesheet; | 641 return updateStylesheet; | 
| 662 } | 642 } | 
| 663 | 643 | 
| 664 if (document instanceof HTMLDocument) | 644 if (document instanceof HTMLDocument) | 
| 665 { | 645 { | 
| 666 checkSitekey(); | 646 checkSitekey(); | 
| 667 window.updateStylesheet = init(document); | 647 window.updateStylesheet = init(document); | 
| 668 } | 648 } | 
| LEFT | RIGHT |