Skip to content

Instantly share code, notes, and snippets.

@kdzwinel kdzwinel/catch-globals.js
Last active Oct 10, 2019

Embed
What would you like to do?
Unfinished globals proxy
function evalCode(code) {
const func = new Function ('window', `with (window) { ${code} }`);
const obj = {};
const proxy = new Proxy(obj, {
get(target, propKey, receiver) {
console.log(`GET ${String(propKey)}`);
if (propKey === 'window') return proxy;
return Reflect.get(window, propKey, receiver);
},
set(target, propKey, value, receiver) {
console.log(`SET ${String(propKey)}=${value}`);
return Reflect.set(window, propKey, value, receiver);
},
apply: function(target, thisArg, argumentsList) {
console.log(`APPLY: ${argumentsList}`);
return Reflect.apply(window, thisArg, argumentsList);
}
});
func(proxy);
console.log(obj)
}
evalCode(`
// catch all of those globals
var a = 1;
let b = 2;
const c = 3;
d = 4;
window.e = 5;
// this should still work
console.log(window.navigator.userAgent);
`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.