Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/content/snippets.js

Issue 29828610: Issue 6791 - Implement abort-current-inline-script snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created July 12, 2018, 5:38 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/content/snippets.js
===================================================================
--- a/lib/content/snippets.js
+++ b/lib/content/snippets.js
@@ -62,8 +62,85 @@
exports.log = log;
function uabinjectDefuser()
{
window.trckd = window.uabpdl = window.uabInject = window.uabDetect = true;
}
exports["uabinject-defuser"] = makeInjector(uabinjectDefuser);
+
+function abortCurrentInlineScript(target, needle)
+{
+ "use strict";
+
+ if (!target)
+ return;
+
+ let reText = ".?";
+
+ if (needle)
+ {
+ reText = /^\/.+\/$/.test(needle) ?
+ needle.slice(1, -1) :
+ needle.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+ }
+
+ let re = new RegExp(reText);
+ let chain = target.split(".");
+
+ let owner = window;
+ let property = null;
+
+ while (true)
+ {
+ property = chain.shift();
+ if (chain.length == 0)
+ break;
+
+ owner = owner[property];
+ if (!(owner instanceof Object))
+ return;
+ }
+
+ let descriptor = Object.getOwnPropertyDescriptor(owner, property);
+ if (descriptor && descriptor.get)
+ return;
+
+ let magic = String.fromCharCode(Date.now() % 26 + 97) +
+ Math.floor(Math.random() * 982451653 + 982451653).toString(36);
+ let value = owner[property];
+
+ let validate = () =>
+ {
+ let element = document.currentScript;
+ if (element instanceof HTMLScriptElement &&
+ element.src == "" && re.test(element.textContent))
+ {
+ throw new ReferenceError(magic);
+ }
+ };
+
+ Object.defineProperty(owner, property, {
+ get()
+ {
+ validate();
+ return value;
+ },
+ set(newValue)
+ {
+ validate();
+ value = newValue;
+ }
+ });
+
+ let {onerror} = window;
+ window.onerror = function(message, ...rest)
+ {
+ if (typeof message == "string" && message.indexOf(magic) != -1)
+ return true;
+
+ if (onerror instanceof Function)
+ return onerror.call(this, message, ...rest);
+ };
+}
+
+exports["abort-current-inline-script"] = makeInjector(abortCurrentInlineScript);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld