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 /// alias set.js
(function() { (function() {
const chain = '{{1}}'; const chain = '{{1}}';
if ( chain === '{{1}}' || chain === '' ) { return; }
let cValue = '{{2}}'; 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 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' ) { if ( cValue === 'undefined' ) {
cValue = undefined; cValue = undefined;
} else if ( cValue === 'false' ) { } else if ( cValue === 'false' ) {
@ -921,11 +955,11 @@
} else if ( cValue === '{}' ) { } else if ( cValue === '{}' ) {
cValue = {}; cValue = {};
} else if ( cValue === 'noopFunc' ) { } else if ( cValue === 'noopFunc' ) {
cValue = function(){}; cValue = cloakFunc(function(){});
} else if ( cValue === 'trueFunc' ) { } else if ( cValue === 'trueFunc' ) {
cValue = function(){ return true; }; cValue = cloakFunc(function(){ return true; });
} else if ( cValue === 'falseFunc' ) { } else if ( cValue === 'falseFunc' ) {
cValue = function(){ return false; }; cValue = cloakFunc(function(){ return false; });
} else if ( /^\d+$/.test(cValue) ) { } else if ( /^\d+$/.test(cValue) ) {
cValue = parseFloat(cValue); cValue = parseFloat(cValue);
if ( isNaN(cValue) ) { return; } if ( isNaN(cValue) ) { return; }
@ -958,7 +992,7 @@
} }
} }
try { try {
Object.defineProperty(owner, prop, { objectDefineProperty(owner, prop, {
configurable, configurable,
get() { get() {
if ( prevGetter !== undefined ) { if ( prevGetter !== undefined ) {

View File

@ -52,6 +52,39 @@ const scriptlet = (
cValue = '' cValue = ''
) => { ) => {
if ( chain === '' ) { return; } 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' ) { if ( cValue === 'undefined' ) {
cValue = undefined; cValue = undefined;
} else if ( cValue === 'false' ) { } else if ( cValue === 'false' ) {
@ -67,11 +100,11 @@ const scriptlet = (
} else if ( cValue === '{}' ) { } else if ( cValue === '{}' ) {
cValue = {}; cValue = {};
} else if ( cValue === 'noopFunc' ) { } else if ( cValue === 'noopFunc' ) {
cValue = function(){}; cValue = cloakFunc(function(){});
} else if ( cValue === 'trueFunc' ) { } else if ( cValue === 'trueFunc' ) {
cValue = function(){ return true; }; cValue = cloakFunc(function(){ return true; });
} else if ( cValue === 'falseFunc' ) { } else if ( cValue === 'falseFunc' ) {
cValue = function(){ return false; }; cValue = cloakFunc(function(){ return false; });
} else if ( /^\d+$/.test(cValue) ) { } else if ( /^\d+$/.test(cValue) ) {
cValue = parseFloat(cValue); cValue = parseFloat(cValue);
if ( isNaN(cValue) ) { return; } if ( isNaN(cValue) ) { return; }
@ -104,7 +137,7 @@ const scriptlet = (
} }
} }
try { try {
Object.defineProperty(owner, prop, { objectDefineProperty(owner, prop, {
configurable, configurable,
get() { get() {
if ( prevGetter !== undefined ) { if ( prevGetter !== undefined ) {

View File

@ -52,6 +52,39 @@ const scriptlet = (
cValue = '' cValue = ''
) => { ) => {
if ( chain === '' ) { return; } 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' ) { if ( cValue === 'undefined' ) {
cValue = undefined; cValue = undefined;
} else if ( cValue === 'false' ) { } else if ( cValue === 'false' ) {
@ -67,11 +100,11 @@ const scriptlet = (
} else if ( cValue === '{}' ) { } else if ( cValue === '{}' ) {
cValue = {}; cValue = {};
} else if ( cValue === 'noopFunc' ) { } else if ( cValue === 'noopFunc' ) {
cValue = function(){}; cValue = cloakFunc(function(){});
} else if ( cValue === 'trueFunc' ) { } else if ( cValue === 'trueFunc' ) {
cValue = function(){ return true; }; cValue = cloakFunc(function(){ return true; });
} else if ( cValue === 'falseFunc' ) { } else if ( cValue === 'falseFunc' ) {
cValue = function(){ return false; }; cValue = cloakFunc(function(){ return false; });
} else if ( /^\d+$/.test(cValue) ) { } else if ( /^\d+$/.test(cValue) ) {
cValue = parseFloat(cValue); cValue = parseFloat(cValue);
if ( isNaN(cValue) ) { return; } if ( isNaN(cValue) ) { return; }
@ -104,7 +137,7 @@ const scriptlet = (
} }
} }
try { try {
Object.defineProperty(owner, prop, { objectDefineProperty(owner, prop, {
configurable, configurable,
get() { get() {
if ( prevGetter !== undefined ) { if ( prevGetter !== undefined ) {