mirror of https://github.com/gorhill/uBlock.git
Improve various scriptlets
Specifically, improve proxying of native methods.
This commit is contained in:
parent
9ced01ebf7
commit
56dfdd2568
|
@ -1446,9 +1446,6 @@ function replaceFetchResponseFn(
|
|||
builtinScriptlets.push({
|
||||
name: 'proxy-apply.fn',
|
||||
fn: proxyApplyFn,
|
||||
dependencies: [
|
||||
'safe-self.fn',
|
||||
],
|
||||
});
|
||||
function proxyApplyFn(
|
||||
target = '',
|
||||
|
@ -1465,12 +1462,28 @@ function proxyApplyFn(
|
|||
}
|
||||
const fn = context[prop];
|
||||
if ( typeof fn !== 'function' ) { return; }
|
||||
const fnname = fn.name;
|
||||
const toString = (function toString() {
|
||||
return `function ${fnname}() { [native code] }`;
|
||||
}).bind(null);
|
||||
if ( fn.prototype && fn.prototype.constructor === fn ) {
|
||||
context[prop] = new Proxy(fn, { construct: handler });
|
||||
return (...args) => { return Reflect.construct(...args); };
|
||||
context[prop] = new Proxy(fn, {
|
||||
construct: handler,
|
||||
get(target, prop, receiver) {
|
||||
if ( prop === 'toString' ) { return toString; }
|
||||
return Reflect.get(target, prop, receiver);
|
||||
},
|
||||
});
|
||||
return (...args) => Reflect.construct(...args);
|
||||
}
|
||||
context[prop] = new Proxy(fn, { apply: handler });
|
||||
return (...args) => { return Reflect.apply(...args); };
|
||||
context[prop] = new Proxy(fn, {
|
||||
apply: handler,
|
||||
get(target, prop, receiver) {
|
||||
if ( prop === 'toString' ) { return toString; }
|
||||
return Reflect.get(target, prop, receiver);
|
||||
},
|
||||
});
|
||||
return (...args) => Reflect.apply(...args);
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1676,6 +1689,7 @@ builtinScriptlets.push({
|
|||
],
|
||||
fn: addEventListenerDefuser,
|
||||
dependencies: [
|
||||
'proxy-apply.fn',
|
||||
'run-at.fn',
|
||||
'safe-self.fn',
|
||||
'should-debug.fn',
|
||||
|
@ -1732,9 +1746,8 @@ function addEventListenerDefuser(
|
|||
}
|
||||
return matchesBoth;
|
||||
};
|
||||
const trapEddEventListeners = ( ) => {
|
||||
const eventListenerHandler = {
|
||||
apply: function(target, thisArg, args) {
|
||||
runAt(( ) => {
|
||||
proxyApplyFn('EventTarget.prototype.addEventListener', function(target, thisArg, args) {
|
||||
let t, h;
|
||||
try {
|
||||
t = String(args[0]);
|
||||
|
@ -1755,21 +1768,7 @@ function addEventListenerDefuser(
|
|||
return safe.uboLog(logPrefix, `Prevented: ${t}\n${h}\n${elementDetails(thisArg)}`);
|
||||
}
|
||||
return Reflect.apply(target, thisArg, args);
|
||||
},
|
||||
get(target, prop, receiver) {
|
||||
if ( prop === 'toString' ) {
|
||||
return target.toString.bind(target);
|
||||
}
|
||||
return Reflect.get(target, prop, receiver);
|
||||
},
|
||||
};
|
||||
self.EventTarget.prototype.addEventListener = new Proxy(
|
||||
self.EventTarget.prototype.addEventListener,
|
||||
eventListenerHandler
|
||||
);
|
||||
};
|
||||
runAt(( ) => {
|
||||
trapEddEventListeners();
|
||||
});
|
||||
}, extraArgs.runAt);
|
||||
}
|
||||
|
||||
|
@ -2475,6 +2474,7 @@ builtinScriptlets.push({
|
|||
],
|
||||
fn: noSetIntervalIf,
|
||||
dependencies: [
|
||||
'proxy-apply.fn',
|
||||
'safe-self.fn',
|
||||
],
|
||||
});
|
||||
|
@ -2495,8 +2495,7 @@ function noSetIntervalIf(
|
|||
delay = parseInt(delay, 10);
|
||||
}
|
||||
const reNeedle = safe.patternToRegex(needle);
|
||||
self.setInterval = new Proxy(self.setInterval, {
|
||||
apply: function(target, thisArg, args) {
|
||||
proxyApplyFn('setInterval', function setInterval(target, thisArg, args) {
|
||||
const a = args[0] instanceof Function
|
||||
? String(safe.Function_toString(args[0]))
|
||||
: String(args[0]);
|
||||
|
@ -2517,13 +2516,6 @@ function noSetIntervalIf(
|
|||
safe.uboLog(logPrefix, `Prevented:\n${a}\n${b}`);
|
||||
}
|
||||
return Reflect.apply(target, thisArg, args);
|
||||
},
|
||||
get(target, prop, receiver) {
|
||||
if ( prop === 'toString' ) {
|
||||
return target.toString.bind(target);
|
||||
}
|
||||
return Reflect.get(target, prop, receiver);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -2538,6 +2530,7 @@ builtinScriptlets.push({
|
|||
],
|
||||
fn: noSetTimeoutIf,
|
||||
dependencies: [
|
||||
'proxy-apply.fn',
|
||||
'safe-self.fn',
|
||||
],
|
||||
});
|
||||
|
@ -2558,8 +2551,7 @@ function noSetTimeoutIf(
|
|||
delay = parseInt(delay, 10);
|
||||
}
|
||||
const reNeedle = safe.patternToRegex(needle);
|
||||
self.setTimeout = new Proxy(self.setTimeout, {
|
||||
apply: function(target, thisArg, args) {
|
||||
proxyApplyFn('setTimeout', function setTimeout(target, thisArg, args) {
|
||||
const a = args[0] instanceof Function
|
||||
? String(safe.Function_toString(args[0]))
|
||||
: String(args[0]);
|
||||
|
@ -2580,13 +2572,6 @@ function noSetTimeoutIf(
|
|||
safe.uboLog(logPrefix, `Prevented:\n${a}\n${b}`);
|
||||
}
|
||||
return Reflect.apply(target, thisArg, args);
|
||||
},
|
||||
get(target, prop, receiver) {
|
||||
if ( prop === 'toString' ) {
|
||||
return target.toString.bind(target);
|
||||
}
|
||||
return Reflect.get(target, prop, receiver);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -2815,6 +2800,7 @@ builtinScriptlets.push({
|
|||
],
|
||||
fn: noWindowOpenIf,
|
||||
dependencies: [
|
||||
'proxy-apply.fn',
|
||||
'safe-self.fn',
|
||||
],
|
||||
});
|
||||
|
@ -2845,8 +2831,7 @@ function noWindowOpenIf(
|
|||
setTimeout(( ) => { decoyElem.remove(); }, autoRemoveAfter * 1000);
|
||||
return decoyElem;
|
||||
};
|
||||
window.open = new Proxy(window.open, {
|
||||
apply: function(target, thisArg, args) {
|
||||
proxyApplyFn('open', function open(target, thisArg, args) {
|
||||
const haystack = args.join(' ');
|
||||
if ( rePattern.test(haystack) !== targetMatchResult ) {
|
||||
if ( safe.logLevel > 1 ) {
|
||||
|
@ -2863,7 +2848,7 @@ function noWindowOpenIf(
|
|||
if ( typeof popup === 'object' && popup !== null ) {
|
||||
Object.defineProperty(popup, 'closed', { value: false });
|
||||
} else {
|
||||
const noopFunc = (function(){}).bind(self);
|
||||
const noopFunc = function open(){};
|
||||
popup = new Proxy(self, {
|
||||
get: function(target, prop) {
|
||||
if ( prop === 'closed' ) { return false; }
|
||||
|
@ -2889,7 +2874,6 @@ function noWindowOpenIf(
|
|||
});
|
||||
}
|
||||
return popup;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue