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 an alphanumeric ID consisting of 5 randomly 6 base-36 digits | 496 * Generates a random alphanumeric ID consisting of 6 base-36 digits |
497 * from the range 100000..zzzzzz (both inclusive) concatenated. | 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() |
Manish Jethani
2018/10/25 22:22:52
Do you like this version of the function?
I don't
Manish Jethani
2018/10/25 22:59:57
Just to clarify, I'm sorry about leading you down
hub
2018/10/26 15:19:05
Done.
| |
502 { | 502 { |
503 let id = ""; | |
504 // | |
505 // 2176782336 is 36^6 which mean 6 chars [a-z0-9] | 503 // 2176782336 is 36^6 which mean 6 chars [a-z0-9] |
506 // 60466176 is 36^5 | 504 // 60466176 is 36^5 |
507 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 | 505 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 |
508 // chars even if Math.random() returns its minimum value 0.0 | 506 // chars even if Math.random() returns its minimum value 0.0 |
509 // | 507 // |
510 for (let i = 0; i < 5; i++) | 508 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); |
511 id += Math.floor(Math.random() * 2116316160 + 60466176).toString(36); | |
512 | |
513 return id; | |
514 } | 509 } |
515 | 510 |
516 function wrapPropertyAccess(object, property, descriptor) | 511 function wrapPropertyAccess(object, property, descriptor) |
517 { | 512 { |
518 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); | 513 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); |
519 if (!currentDescriptor) | 514 if (currentDescriptor && !currentDescriptor.configurable) |
520 currentDescriptor = {}; | 515 return false; |
521 if (typeof descriptor.get != "undefined") | 516 |
522 currentDescriptor.get = descriptor.get; | 517 Object.defineProperty(object, property, descriptor); |
523 if (typeof descriptor.set != "undefined") | 518 return true; |
524 currentDescriptor.set = descriptor.set; | 519 } |
525 Object.defineProperty(object, property, currentDescriptor); | 520 |
526 } | 521 /** |
527 | 522 * Patches a property on the window object to abort execution when the |
528 /** | 523 * property is read. |
529 * Will patch a property on the window object to abort when read. | 524 * |
530 * It will intercept the onerror callback and block it if tagged. | 525 * No error is be printed to the console. |
526 * | |
531 * The idea originates from | 527 * The idea originates from |
532 * 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}. |
533 * | 529 * |
534 * @todo handle properties of properties. | 530 * @param {string} property The name of the property. |
535 * | |
536 * @param {string} property the name of the property. | |
537 */ | 531 */ |
538 function abortOnPropertyRead(property) | 532 function abortOnPropertyRead(property) |
539 { | 533 { |
540 if (!property) | 534 if (!property) |
541 return; | 535 return; |
542 | 536 |
543 let rid = randomId(); | 537 let rid = randomId(); |
544 | 538 |
545 function abort() | 539 function abort() |
546 { | 540 { |
547 throw new ReferenceError(rid); | 541 throw new ReferenceError(rid); |
548 } | 542 } |
549 | 543 |
550 let {onerror} = window; | 544 let {onerror} = window; |
551 window.onerror = (message, ...rest) => | 545 if (wrapPropertyAccess(window, property, {get: abort, set() {}})) |
552 { | 546 { |
553 if (typeof message == "string" && message.includes(rid)) | 547 window.onerror = (message, ...rest) => |
554 return true; | 548 { |
555 if (typeof onerror == "function") | 549 if (typeof message == "string" && message.includes(rid)) |
556 return onerror.call(this, message, ...rest); | 550 return true; |
Manish Jethani
2018/10/25 22:22:52
OK, this one I already explained why there's no gu
hub
2018/10/26 15:19:06
Done.
| |
557 }; | 551 if (typeof onerror == "function") |
558 | 552 return (() => {}).call.call(onerror, this, message, ...rest); |
559 wrapPropertyAccess(window, property, {get: abort, set() {}}); | 553 }; |
554 } | |
560 } | 555 } |
561 | 556 |
562 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, | 557 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, |
563 wrapPropertyAccess, | 558 wrapPropertyAccess, |
564 randomId); | 559 randomId); |
LEFT | RIGHT |