Skip to content

Commit ebc42ae

Browse files
committed
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`.
1 parent 11e6ff5 commit ebc42ae

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

assets/resources/scriptlets.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,30 @@
3232

3333

3434

35-
/// abort-current-inline-script.js
35+
/// abort-current-script.js
36+
/// alias acs.js
37+
/// alias abort-current-inline-script.js
3638
/// alias acis.js
3739
(function() {
3840
const target = '{{1}}';
3941
if ( target === '' || target === '{{1}}' ) { return; }
42+
const reRegexEscape = /[.*+?^${}()|[\]\\]/g;
4043
const needle = '{{2}}';
41-
let reText = '.?';
42-
if ( needle !== '' && needle !== '{{2}}' ) {
43-
reText = /^\/.+\/$/.test(needle)
44-
? needle.slice(1,-1)
45-
: needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
46-
}
44+
const reText = (( ) => {
45+
if ( needle === '' || needle === '{{2}}' ) { return /^/; }
46+
if ( /^\/.+\/$/.test(needle) ) {
47+
return new RegExp(needle.slice(1,-1));
48+
}
49+
return new RegExp(needle.replace(reRegexEscape, '\\$&'));
50+
})();
51+
const context = '{{3}}';
52+
const reContext = (( ) => {
53+
if ( context === '' || context === '{{3}}' ) { return /^$/; }
54+
if ( /^\/.+\/$/.test(context) ) {
55+
return new RegExp(context.slice(1,-1));
56+
}
57+
return new RegExp(context.replace(reRegexEscape, '\\$&'));
58+
})();
4759
const thisScript = document.currentScript;
4860
const re = new RegExp(reText);
4961
const chain = target.split('.');
@@ -70,7 +82,7 @@
7082
const e = document.currentScript;
7183
if (
7284
e instanceof HTMLScriptElement &&
73-
e.src === '' &&
85+
reContext.test(e.src) &&
7486
e !== thisScript &&
7587
re.test(e.textContent)
7688
) {

0 commit comments

Comments
 (0)