From ebc42ae21e7900fafeaf1041038b94488b1d50e5 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 17 Jul 2021 14:03:50 -0400 Subject: [PATCH] Add abort-current-script scriptlet This scriplet supersedes abort-current-inline-script (acis), and accepts an optional third argument which is matched against the `src` property of script resources. When the third argument is not provided, the scriptlet behaves essentially the same as `acis`, and because of this `acis` is now aliased to `abort-current-script`, and all existing `acis` filters will execute with no change in behavior. In the long run, usage of `abort-current-inline-script` or its alias `acis` should go away and be replaced with `abort-current-script` or its alias `acs`. --- assets/resources/scriptlets.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 027bb2b35..d5b363b89 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -32,18 +32,30 @@ -/// abort-current-inline-script.js +/// abort-current-script.js +/// alias acs.js +/// alias abort-current-inline-script.js /// alias acis.js (function() { const target = '{{1}}'; if ( target === '' || target === '{{1}}' ) { return; } + const reRegexEscape = /[.*+?^${}()|[\]\\]/g; const needle = '{{2}}'; - let reText = '.?'; - if ( needle !== '' && needle !== '{{2}}' ) { - reText = /^\/.+\/$/.test(needle) - ? needle.slice(1,-1) - : needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); - } + const reText = (( ) => { + if ( needle === '' || needle === '{{2}}' ) { return /^/; } + if ( /^\/.+\/$/.test(needle) ) { + return new RegExp(needle.slice(1,-1)); + } + return new RegExp(needle.replace(reRegexEscape, '\\$&')); + })(); + const context = '{{3}}'; + const reContext = (( ) => { + if ( context === '' || context === '{{3}}' ) { return /^$/; } + if ( /^\/.+\/$/.test(context) ) { + return new RegExp(context.slice(1,-1)); + } + return new RegExp(context.replace(reRegexEscape, '\\$&')); + })(); const thisScript = document.currentScript; const re = new RegExp(reText); const chain = target.split('.'); @@ -70,7 +82,7 @@ const e = document.currentScript; if ( e instanceof HTMLScriptElement && - e.src === '' && + reContext.test(e.src) && e !== thisScript && re.test(e.textContent) ) {