Skip to content

Instantly share code, notes, and snippets.

@kdzwinel
Last active February 11, 2020 17:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdzwinel/6c605abded373f075836032b1fbde2ff to your computer and use it in GitHub Desktop.
Save kdzwinel/6c605abded373f075836032b1fbde2ff to your computer and use it in GitHub Desktop.
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