Skip to content

Commit 57ade5f

Browse files
committed
Improve scriptlets framework
Related issue: uBlockOrigin/uBlock-issues#4090
1 parent 39a0106 commit 57ade5f

4 files changed

Lines changed: 12 additions & 15 deletions

File tree

src/js/resources/prevent-addeventlistener.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ function preventAddEventListener(
9191
return parts.join('');
9292
};
9393
const shouldPrevent = (thisArg, type, handler) => {
94-
const matchesType = safe.RegExp_test.call(reType, type);
95-
const matchesHandler = safe.RegExp_test.call(rePattern, handler);
94+
const matchesType = safe.RegExp_test(reType, type);
95+
const matchesHandler = safe.RegExp_test(rePattern, handler);
9696
const matchesEither = matchesType || matchesHandler;
9797
const matchesBoth = matchesType && matchesHandler;
9898
if ( safe.logLevel > 1 && matchesEither ) {

src/js/resources/replace-argument.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export function trustedReplaceArgument(
109109
}
110110
const argBefore = getArg(context);
111111
if ( extraArgs.condition !== undefined ) {
112-
if ( safe.RegExp_test.call(reCondition, argBefore) === false ) {
112+
if ( safe.RegExp_test(reCondition, argBefore) === false ) {
113113
return context.reflect();
114114
}
115115
}

src/js/resources/safe-self.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ export function safeSelf() {
3535
const safe = {
3636
'Array_from': Array.from,
3737
'Error': self.Error,
38-
'Function_toStringFn': self.Function.prototype.toString,
39-
'Function_toString': thisArg => safe.Function_toStringFn.call(thisArg),
38+
'Function_toString': Function.prototype.call.bind(self.Function.prototype.toString),
4039
'Math_floor': Math.floor,
4140
'Math_max': Math.max,
4241
'Math_min': Math.min,
@@ -49,7 +48,7 @@ export function safeSelf() {
4948
'Object_hasOwn': Object.hasOwn.bind(Object),
5049
'Object_toString': Object.prototype.toString,
5150
'RegExp': self.RegExp,
52-
'RegExp_test': self.RegExp.prototype.test,
51+
'RegExp_test': Function.prototype.call.bind(self.RegExp.prototype.test),
5352
'RegExp_exec': self.RegExp.prototype.exec,
5453
'Request_clone': self.Request.prototype.clone,
5554
'String': self.String,
@@ -60,10 +59,8 @@ export function safeSelf() {
6059
'removeEventListener': self.EventTarget.prototype.removeEventListener,
6160
'fetch': self.fetch,
6261
'JSON': self.JSON,
63-
'JSON_parseFn': self.JSON.parse,
64-
'JSON_stringifyFn': self.JSON.stringify,
65-
'JSON_parse': (...args) => safe.JSON_parseFn.call(safe.JSON, ...args),
66-
'JSON_stringify': (...args) => safe.JSON_stringifyFn.call(safe.JSON, ...args),
62+
'JSON_parse': Function.prototype.call.bind(self.JSON.parse, self.JSON),
63+
'JSON_stringify': Function.prototype.call.bind(self.JSON.stringify, self.JSON),
6764
'log': console.log.bind(console),
6865
// Properties
6966
logLevel: 0,
@@ -116,7 +113,7 @@ export function safeSelf() {
116113
testPattern(details, haystack) {
117114
if ( details.matchAll ) { return true; }
118115
if ( details.re ) {
119-
return this.RegExp_test.call(details.re, haystack) === details.expect;
116+
return this.RegExp_test(details.re, haystack) === details.expect;
120117
}
121118
return haystack.includes(details.pattern) === details.expect;
122119
},

src/js/resources/scriptlets.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ function replaceNodeTextFn(
123123
const before = node.textContent;
124124
if ( reIncludes ) {
125125
reIncludes.lastIndex = 0;
126-
if ( safe.RegExp_test.call(reIncludes, before) === false ) { return true; }
126+
if ( safe.RegExp_test(reIncludes, before) === false ) { return true; }
127127
}
128128
if ( reExcludes ) {
129129
reExcludes.lastIndex = 0;
130-
if ( safe.RegExp_test.call(reExcludes, before) ) { return true; }
130+
if ( safe.RegExp_test(reExcludes, before) ) { return true; }
131131
}
132132
rePattern.lastIndex = 0;
133-
if ( safe.RegExp_test.call(rePattern, before) === false ) { return true; }
133+
if ( safe.RegExp_test(rePattern, before) === false ) { return true; }
134134
rePattern.lastIndex = 0;
135135
const after = pattern !== ''
136136
? before.replace(rePattern, replacement)
@@ -1935,7 +1935,7 @@ function trustedSuppressNativeMethod(
19351935
}
19361936
}
19371937
if ( signatureArg.type === 'pattern' ) {
1938-
if ( safe.RegExp_test.call(signatureArg.re, targetArg) === false ) {
1938+
if ( safe.RegExp_test(signatureArg.re, targetArg) === false ) {
19391939
return context.reflect();
19401940
}
19411941
}

0 commit comments

Comments
 (0)