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`.
This commit is contained in:
Raymond Hill 2021-07-17 14:03:50 -04:00
parent 11e6ff5782
commit ebc42ae21e
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -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)
) {