1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-22 18:32:45 +01:00

Fine tune scriptlet

This commit is contained in:
Raymond Hill 2023-03-14 08:21:22 -04:00
parent 9051df3f02
commit fc84fdee52
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 112 additions and 12 deletions

View File

@ -904,8 +904,42 @@
/// alias set.js
(function() {
const chain = '{{1}}';
if ( chain === '{{1}}' || chain === '' ) { return; }
let cValue = '{{2}}';
const trappedProp = (( ) => {
const pos = chain.lastIndexOf('.');
if ( pos === -1 ) { return chain; }
return chain.slice(pos+1);
})();
if ( trappedProp === '' ) { return; }
const thisScript = document.currentScript;
const objectDefineProperty = Object.defineProperty.bind(Object);
const cloakFunc = fn => {
objectDefineProperty(fn, 'name', { value: trappedProp });
const proxy = new Proxy(fn, {
defineProperty(target, prop) {
if ( prop !== 'toString' ) {
return Reflect.deleteProperty(...arguments);
}
return true;
},
deleteProperty(target, prop) {
if ( prop !== 'toString' ) {
return Reflect.deleteProperty(...arguments);
}
return true;
},
get(target, prop) {
if ( prop === 'toString' ) {
return function() {
return `function ${trappedProp}() { [native code] }`;
}.bind(null);
}
return Reflect.get(...arguments);
},
});
return proxy;
};
if ( cValue === 'undefined' ) {
cValue = undefined;
} else if ( cValue === 'false' ) {
@ -921,11 +955,11 @@
} else if ( cValue === '{}' ) {
cValue = {};
} else if ( cValue === 'noopFunc' ) {
cValue = function(){};
cValue = cloakFunc(function(){});
} else if ( cValue === 'trueFunc' ) {
cValue = function(){ return true; };
cValue = cloakFunc(function(){ return true; });
} else if ( cValue === 'falseFunc' ) {
cValue = function(){ return false; };
cValue = cloakFunc(function(){ return false; });
} else if ( /^\d+$/.test(cValue) ) {
cValue = parseFloat(cValue);
if ( isNaN(cValue) ) { return; }
@ -958,7 +992,7 @@
}
}
try {
Object.defineProperty(owner, prop, {
objectDefineProperty(owner, prop, {
configurable,
get() {
if ( prevGetter !== undefined ) {

View File

@ -52,6 +52,39 @@ const scriptlet = (
cValue = ''
) => {
if ( chain === '' ) { return; }
const trappedProp = (( ) => {
const pos = chain.lastIndexOf('.');
if ( pos === -1 ) { return chain; }
return chain.slice(pos+1);
})();
if ( trappedProp === '' ) { return; }
const objectDefineProperty = Object.defineProperty.bind(Object);
const cloakFunc = fn => {
objectDefineProperty(fn, 'name', { value: trappedProp });
const proxy = new Proxy(fn, {
defineProperty(target, prop) {
if ( prop !== 'toString' ) {
return Reflect.deleteProperty(...arguments);
}
return true;
},
deleteProperty(target, prop) {
if ( prop !== 'toString' ) {
return Reflect.deleteProperty(...arguments);
}
return true;
},
get(target, prop) {
if ( prop === 'toString' ) {
return function() {
return `function ${trappedProp}() { [native code] }`;
}.bind(null);
}
return Reflect.get(...arguments);
},
});
return proxy;
};
if ( cValue === 'undefined' ) {
cValue = undefined;
} else if ( cValue === 'false' ) {
@ -67,11 +100,11 @@ const scriptlet = (
} else if ( cValue === '{}' ) {
cValue = {};
} else if ( cValue === 'noopFunc' ) {
cValue = function(){};
cValue = cloakFunc(function(){});
} else if ( cValue === 'trueFunc' ) {
cValue = function(){ return true; };
cValue = cloakFunc(function(){ return true; });
} else if ( cValue === 'falseFunc' ) {
cValue = function(){ return false; };
cValue = cloakFunc(function(){ return false; });
} else if ( /^\d+$/.test(cValue) ) {
cValue = parseFloat(cValue);
if ( isNaN(cValue) ) { return; }
@ -104,7 +137,7 @@ const scriptlet = (
}
}
try {
Object.defineProperty(owner, prop, {
objectDefineProperty(owner, prop, {
configurable,
get() {
if ( prevGetter !== undefined ) {

View File

@ -52,6 +52,39 @@ const scriptlet = (
cValue = ''
) => {
if ( chain === '' ) { return; }
const trappedProp = (( ) => {
const pos = chain.lastIndexOf('.');
if ( pos === -1 ) { return chain; }
return chain.slice(pos+1);
})();
if ( trappedProp === '' ) { return; }
const objectDefineProperty = Object.defineProperty.bind(Object);
const cloakFunc = fn => {
objectDefineProperty(fn, 'name', { value: trappedProp });
const proxy = new Proxy(fn, {
defineProperty(target, prop) {
if ( prop !== 'toString' ) {
return Reflect.deleteProperty(...arguments);
}
return true;
},
deleteProperty(target, prop) {
if ( prop !== 'toString' ) {
return Reflect.deleteProperty(...arguments);
}
return true;
},
get(target, prop) {
if ( prop === 'toString' ) {
return function() {
return `function ${trappedProp}() { [native code] }`;
}.bind(null);
}
return Reflect.get(...arguments);
},
});
return proxy;
};
if ( cValue === 'undefined' ) {
cValue = undefined;
} else if ( cValue === 'false' ) {
@ -67,11 +100,11 @@ const scriptlet = (
} else if ( cValue === '{}' ) {
cValue = {};
} else if ( cValue === 'noopFunc' ) {
cValue = function(){};
cValue = cloakFunc(function(){});
} else if ( cValue === 'trueFunc' ) {
cValue = function(){ return true; };
cValue = cloakFunc(function(){ return true; });
} else if ( cValue === 'falseFunc' ) {
cValue = function(){ return false; };
cValue = cloakFunc(function(){ return false; });
} else if ( /^\d+$/.test(cValue) ) {
cValue = parseFloat(cValue);
if ( isNaN(cValue) ) { return; }
@ -104,7 +137,7 @@ const scriptlet = (
}
}
try {
Object.defineProperty(owner, prop, {
objectDefineProperty(owner, prop, {
configurable,
get() {
if ( prevGetter !== undefined ) {