Last active
February 11, 2020 17:21
-
-
Save kdzwinel/6c605abded373f075836032b1fbde2ff to your computer and use it in GitHub Desktop.
Unfinished globals proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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