Index: lib/content/snippets.js |
=================================================================== |
--- a/lib/content/snippets.js |
+++ b/lib/content/snippets.js |
@@ -431,8 +431,59 @@ |
object + ""; |
if (typeof dir == "function") |
dir.call(this, object); |
}; |
} |
exports["dir-string"] = makeInjector(dirString); |
+ |
+function randomId() |
+{ |
+ // 2176782336 is 36^6 which mean 6 chars [a-z0-9] |
+ // 60466176 is 36^5 |
+ // 2176782336 - 60466176 = 2116316160. This ensure always 6 chars |
+ // for when Math.random() returns 0.0 |
+ return (Math.floor(Math.random() * 2116316160 + 60466176)).toString(36); |
Manish Jethani
2018/09/25 12:25:22
Nit: The parentheses around `Math.foor(...)` are u
hub
2018/09/25 15:40:43
Done.
|
+} |
+ |
+/** |
+ * Will patch a property on the window object to abort when read. |
+ * It will intercept the onerror callback and block it if tagged. |
+ * |
+ * @todo handle properties of properties. |
+ * |
+ * @param {string} prop the name of the property. |
+ */ |
+function abortOnPropertyRead(...props) |
+{ |
+ if (props.length == 0) |
+ return; |
+ |
+ let magic = randomId(); |
+ |
+ let abort = function() |
+ { |
+ throw new ReferenceError(magic); |
+ }; |
+ |
+ let {onerror} = window; |
+ window.onerror = (message, ...rest) => |
+ { |
+ if (typeof message == "string" && message.includes(magic)) |
+ return true; |
+ if (onerror && typeof onerro == "function") |
+ return onerror(this, message, ...rest); |
+ }; |
+ |
+ let patch = function(o, p) |
+ { |
+ // simple property |
+ let d = Object.getOwnPropertyDescriptor(o, p); |
+ if (!d || d.get != abort) |
+ Object.defineProperty(o, p, {get: abort, set() {}}); |
+ }; |
+ for (let prop of props) |
+ patch(window, prop); |
+} |
+ |
+exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, randomId); |