Index: lib/content/snippets.js |
=================================================================== |
--- a/lib/content/snippets.js |
+++ b/lib/content/snippets.js |
@@ -486,8 +486,72 @@ |
{ |
event.preventDefault(); |
} |
}, |
true); |
} |
exports["prevent-inline-scripts"] = preventInlineScripts; |
+ |
+/** |
+ * Generates a random alphanumeric ID consisting of 6 base-36 digits |
+ * from the range 100000..zzzzzz (both inclusive). |
+ * |
+ * @returns {string} The random ID. |
+ */ |
+function randomId() |
+{ |
+ // 2176782336 is 36^6 which mean 6 chars [a-z0-9] |
+ // 60466176 is 36^5 |
+ // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 |
+ // chars even if Math.random() returns its minimum value 0.0 |
+ // |
+ return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); |
+} |
+ |
+function wrapPropertyAccess(object, property, descriptor) |
+{ |
+ let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); |
+ if (currentDescriptor && !currentDescriptor.configurable) |
+ return false; |
+ |
+ Object.defineProperty(object, property, descriptor); |
+ return true; |
+} |
+ |
+/** |
+ * Patches a property on the window object to abort execution when the |
+ * property is read. |
+ * No error will be printed in the console. |
Manish Jethani
2018/10/31 19:54:49
Nit: Again, is the "No error will be printed" part
Manish Jethani
2018/10/31 19:54:49
Nit: Even though it doesn't matter to the generate
hub
2018/10/31 20:44:16
Done.
hub
2018/10/31 20:44:17
Done.
|
+ * The idea originates from |
Manish Jethani
2018/10/31 19:54:49
Nit: Period at the end of the sentence.
hub
2018/10/31 20:44:16
Done.
|
+ * {@link https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237bfc268ecfc9d9d2b/filters/resources.txt#L1703 uBlock Origin} |
+ * |
+ * @param {string} property The name of the property. |
+ */ |
+function abortOnPropertyRead(property) |
+{ |
+ if (!property) |
+ return; |
+ |
+ let rid = randomId(); |
+ |
+ function abort() |
+ { |
+ throw new ReferenceError(rid); |
+ } |
+ |
+ if (wrapPropertyAccess(window, property, {get: abort, set() {}})) |
+ { |
+ let {onerror} = window; |
Manish Jethani
2018/10/31 19:38:16
Can we move this line outside the if block? In the
hub
2018/10/31 20:44:16
It can be moved.
But it would still bomb if it wa
Manish Jethani
2018/10/31 20:52:22
Alright, fair enough.
|
+ window.onerror = (message, ...rest) => |
+ { |
+ if (typeof message == "string" && message.includes(rid)) |
+ return true; |
+ if (typeof onerror == "function") |
+ return (() => {}).call.call(onerror, this, message, ...rest); |
+ }; |
+ } |
+} |
+ |
+exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, |
+ wrapPropertyAccess, |
+ randomId); |