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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
486 { | 486 { |
487 event.preventDefault(); | 487 event.preventDefault(); |
488 } | 488 } |
489 }, | 489 }, |
490 true); | 490 true); |
491 } | 491 } |
492 | 492 |
493 exports["prevent-inline-scripts"] = preventInlineScripts; | 493 exports["prevent-inline-scripts"] = preventInlineScripts; |
494 | 494 |
495 /** | 495 /** |
496 * Generates a random alphanumeric ID consisting of 6 chars base-36 digits | 496 * Generates a random alphanumeric ID consisting of 6 base-36 digits |
Manish Jethani
2018/10/25 16:12:16
s/chars//
hub
2018/10/25 20:26:10
Done.
| |
497 * from the range 100000..zzzzzz (both inclusive). | 497 * from the range 100000..zzzzzz (both inclusive). |
498 * | 498 * |
499 * @returns {string} The random ID. | 499 * @returns {string} The random ID. |
500 */ | 500 */ |
501 function randomId() | 501 function randomId() |
502 { | 502 { |
503 // 2176782336 is 36^6 which mean 6 chars [a-z0-9] | 503 // 2176782336 is 36^6 which mean 6 chars [a-z0-9] |
504 // 60466176 is 36^5 | 504 // 60466176 is 36^5 |
505 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 | 505 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 |
506 // chars even if Math.random() returns its minimum value 0.0 | 506 // chars even if Math.random() returns its minimum value 0.0 |
507 // | |
507 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); | 508 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); |
508 } | 509 } |
509 | 510 |
510 function wrapPropertyAccess(object, property, descriptor) | 511 function wrapPropertyAccess(object, property, descriptor) |
511 { | 512 { |
512 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); | 513 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); |
513 if (!currentDescriptor) | 514 if (currentDescriptor && !currentDescriptor.configurable) |
514 currentDescriptor = {}; | 515 return false; |
515 if (typeof descriptor.get != "undefined") | 516 |
516 currentDescriptor.get = descriptor.get; | 517 Object.defineProperty(object, property, descriptor); |
517 if (typeof descriptor.set != "undefined") | 518 return true; |
518 currentDescriptor.set = descriptor.set; | 519 } |
519 Object.defineProperty(object, property, currentDescriptor); | 520 |
520 } | 521 /** |
521 | 522 * Patches a property on the window object to abort execution when the |
522 /** | 523 * property is read. |
523 * Will patch a property on the window object to abort when read. | 524 * |
524 * It will intercept the onerror callback and block it if tagged. | 525 * No error is be printed to the console. |
526 * | |
525 * The idea originates from | 527 * The idea originates from |
526 * https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237bfc268ec fc9d9d2b/filters/resources.txt#L1703 | 528 * {@link https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237b fc268ecfc9d9d2b/filters/resources.txt#L1703 uBlock Origin}. |
527 * | 529 * |
528 * @todo handle properties of properties. | 530 * @param {string} property The name of the property. |
529 * | |
530 * @param {string} property the name of the property. | |
531 */ | 531 */ |
532 function abortOnPropertyRead(property) | 532 function abortOnPropertyRead(property) |
533 { | 533 { |
534 if (!property) | 534 if (!property) |
535 return; | 535 return; |
536 | 536 |
537 let rid = randomId(); | 537 let rid = randomId(); |
538 | 538 |
539 function abort() | 539 function abort() |
540 { | 540 { |
541 throw new ReferenceError(rid); | 541 throw new ReferenceError(rid); |
542 } | 542 } |
543 | 543 |
544 let {onerror} = window; | 544 let {onerror} = window; |
545 window.onerror = (message, ...rest) => | 545 if (wrapPropertyAccess(window, property, {get: abort, set() {}})) |
546 { | 546 { |
547 if (typeof message == "string" && message.includes(rid)) | 547 window.onerror = (message, ...rest) => |
548 return true; | 548 { |
549 if (typeof onerror == "function") | 549 if (typeof message == "string" && message.includes(rid)) |
550 return onerror(message, ...rest); | 550 return true; |
551 }; | 551 if (typeof onerror == "function") |
552 | 552 return (() => {}).call.call(onerror, this, message, ...rest); |
553 wrapPropertyAccess(window, property, {get: abort, set() {}}); | 553 }; |
554 } | |
554 } | 555 } |
555 | 556 |
556 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, | 557 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, |
557 wrapPropertyAccess, | 558 wrapPropertyAccess, |
558 randomId); | 559 randomId); |
LEFT | RIGHT |