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 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 var observer = new MutationObserver(function() | 343 var observer = new MutationObserver(function() |
344 { | 344 { |
345 if (style.parentNode != parentNode) | 345 if (style.parentNode != parentNode) |
346 parentNode.appendChild(style); | 346 parentNode.appendChild(style); |
347 }); | 347 }); |
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 injectJS(f) | 353 function runInPage(fn, arg) |
354 { | 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"); | 355 var script = document.createElement("script"); |
356 script.type = "application/javascript"; | |
360 script.async = false; | 357 script.async = false; |
361 script.textContent = codeString; | 358 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; |
362 document.documentElement.appendChild(script); | 359 document.documentElement.appendChild(script); |
363 document.documentElement.removeChild(script); | 360 document.documentElement.removeChild(script); |
364 } | 361 } |
365 | 362 |
366 function protectStyleSheet(document, style) | 363 function protectStyleSheet(document, style) |
367 { | 364 { |
368 style.id = id; | 365 style.id = id; |
369 | 366 |
370 var protector = function(id) | 367 runInPage(function(id) |
371 { | 368 { |
372 var style = document.getElementById(id) || | 369 var style = document.getElementById(id) || |
373 document.documentElement.shadowRoot.getElementById(id); | 370 document.documentElement.shadowRoot.getElementById(id); |
374 style.removeAttribute("id"); | 371 style.removeAttribute("id"); |
375 | 372 |
376 var i; | |
377 var disableables = [style, style.sheet]; | 373 var disableables = [style, style.sheet]; |
378 for (i = 0; i < disableables.length; i += 1) | 374 for (var i = 0; i < disableables.length; i++) |
379 Object.defineProperty(disableables[i], "disabled", | 375 Object.defineProperty(disableables[i], "disabled", |
380 {value: false, enumerable: true}); | 376 {value: false, enumerable: true}); |
381 | 377 |
382 var methods = ["deleteRule", "removeRule"]; | 378 ["deleteRule", "removeRule"].forEach(function(method) |
383 for (i = 0; i < methods.length; i += 1) | 379 { |
384 { | 380 var original = CSSStyleSheet.prototype[method]; |
385 if (methods[i] in CSSStyleSheet.prototype) | 381 CSSStyleSheet.prototype[method] = function(index) |
386 { | 382 { |
387 (function(method) | 383 if (this != style.sheet) |
388 { | 384 original.call(this, index); |
389 var original = CSSStyleSheet.prototype[method]; | 385 }; |
390 CSSStyleSheet.prototype[method] = function(index) | 386 }); |
391 { | 387 }, id); |
392 if (this != style.sheet) | |
393 original.call(this, index); | |
394 }; | |
395 }(methods[i])); | |
396 } | |
397 } | |
398 }; | |
399 | |
400 injectJS(protector, id); | |
401 } | 388 } |
402 | 389 |
403 // 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 |
404 // 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 |
405 // us. As a workaround we wrap WebSocket, closing connections that would have | 392 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket |
406 // otherwise been blocked. | 393 // connections from being opened. |
407 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | 394 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 |
408 function wrapWebSocket() | 395 function wrapWebSocket() |
409 { | 396 { |
410 if (typeof WebSocket == "undefined" || typeof WeakMap == "undefined") | 397 if (typeof WebSocket == "undefined") |
411 return; | 398 return; |
412 | 399 |
413 var eventName = "abpws-" + id; | 400 var eventName = "abpws-" + id; |
414 | 401 |
415 document.addEventListener(eventName, function(event) | 402 document.addEventListener(eventName, function(event) |
416 { | 403 { |
417 ext.backgroundPage.sendMessage({ | 404 ext.backgroundPage.sendMessage({ |
418 type: "websocket-request", | 405 type: "websocket-request", |
419 url: event.detail.url | 406 url: event.detail.url |
420 }, function (block) | 407 }, function (block) |
421 { | 408 { |
422 document.dispatchEvent( | 409 document.dispatchEvent( |
423 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) | 410 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) |
424 ); | 411 ); |
425 }); | 412 }); |
426 }); | 413 }); |
427 | 414 |
428 function wrapper(eventName) | 415 runInPage(function(eventName) |
429 { | 416 { |
417 // As far as possible we must track everything we use that could be | |
418 // sabotaged by the website later in order to circumvent us. | |
430 var RealWebSocket = WebSocket; | 419 var RealWebSocket = WebSocket; |
431 | 420 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl ose); |
432 // To avoid detection we make our WebSocket wrapper as similar as possible. | 421 var addEventListener = document.addEventListener.bind(document); |
433 // Most WebSocket properties and methods actually belong to | 422 var removeEventListener = document.removeEventListener.bind(document); |
434 // WebSocket.prototype, but they require access to the instance state. To | 423 var dispatchEvent = document.dispatchEvent.bind(document); |
435 // acheive this without adding extra properties we need to manage our own | 424 var CustomEvent = window.CustomEvent; |
436 // storage here. To avoid leaking memory in the case that all other | 425 |
437 // references to a WebSocket have been deleted we must use a WeakMap. | 426 function checkRequest(url, callback) |
438 var instanceStorage = new WeakMap(); | 427 { |
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; | 428 var incomingEventName = eventName + "-" + url; |
483 function listener(event) | 429 function listener(event) |
484 { | 430 { |
485 storage.blocked = event.detail; | 431 callback(event.detail); |
486 if (!storage.blocked) | 432 removeEventListener(incomingEventName, listener); |
487 { | 433 } |
488 storage.websocket = new RealWebSocket(url, protocol); | 434 addEventListener(incomingEventName, listener); |
489 processQueue(storage.queue, storage.websocket); | 435 |
490 } | 436 dispatchEvent(new CustomEvent(eventName, { |
491 delete storage.queue; // FIXME onerror!? | 437 detail: {url: url} |
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 })); | 438 })); |
500 }; | 439 } |
501 | 440 |
502 function proxyProperties(original) | 441 WebSocket = function WrappedWebSocket(url, protocols) |
503 { | 442 { |
504 var properties = Object.create(null); | 443 // Throw correct exceptions if the constructor is used improperly. |
505 | 444 if (!(this instanceof WrappedWebSocket)) return RealWebSocket(); |
506 Object.keys(original).map(function(key) | 445 if (arguments.length < 1) return new RealWebSocket(); |
507 { | 446 |
508 if (key == "prototype") | 447 var websocket = new RealWebSocket(url, protocols); |
kzar
2016/07/06 16:39:20
(For Safari)
| |
509 return; | 448 |
510 | 449 checkRequest(websocket.url, function(blocked) |
511 var descriptor = Object.getOwnPropertyDescriptor(original, key); | 450 { |
512 | 451 if (blocked) |
513 if (typeof descriptor.value == "function") | 452 closeWebSocket(websocket); |
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 }); | 453 }); |
557 return properties; | 454 |
558 } | 455 return websocket; |
559 | 456 }.bind(); |
560 Object.defineProperties(WebSocket, proxyProperties(RealWebSocket)); | 457 |
561 WebSocket.prototype = Object.create( | 458 Object.defineProperties(WebSocket, { |
562 RealWebSocket.prototype.__proto__, | 459 CONNECTING: {value: RealWebSocket.CONNECTING, enumerable: true}, |
563 proxyProperties(RealWebSocket.prototype) | 460 OPEN: {value: RealWebSocket.OPEN, enumerable: true}, |
564 ); | 461 CLOSING: {value: RealWebSocket.CLOSING, enumerable: true}, |
565 } | 462 CLOSED: {value: RealWebSocket.CLOSED, enumerable: true}, |
566 | 463 prototype: {value: RealWebSocket.prototype} |
567 injectJS(wrapper, eventName); | 464 }); |
465 | |
466 RealWebSocket.prototype.constructor = WebSocket; | |
467 }, eventName); | |
568 } | 468 } |
569 | 469 |
570 function init(document) | 470 function init(document) |
571 { | 471 { |
572 var shadow = null; | 472 var shadow = null; |
573 var style = null; | 473 var style = null; |
574 var observer = null; | 474 var observer = null; |
575 var tracer = null; | 475 var tracer = null; |
576 | 476 |
577 wrapWebSocket(); | 477 wrapWebSocket(); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
739 }, true); | 639 }, true); |
740 | 640 |
741 return updateStylesheet; | 641 return updateStylesheet; |
742 } | 642 } |
743 | 643 |
744 if (document instanceof HTMLDocument) | 644 if (document instanceof HTMLDocument) |
745 { | 645 { |
746 checkSitekey(); | 646 checkSitekey(); |
747 window.updateStylesheet = init(document); | 647 window.updateStylesheet = init(document); |
748 } | 648 } |
LEFT | RIGHT |