Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
Created December 15, 2012 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBruant/4290075 to your computer and use it in GitHub Desktop.
Save DavidBruant/4290075 to your computer and use it in GitHub Desktop.
Attempt to describe a self-hosted version of WindowProxy
// Reusing parts of https://gist.github.com/4279162
function WindowProxyHandler(target){
this.target = target;
}
WindowProxyHandler.prototype = {
get: function (_, name) {
return this.target[name];
},
set: function (_, name, value) {
return this.target[name] = value;
},
getOwnPropertyDescriptor: function (_, name) {
return Object.getOwnPropertyDescriptor(this.target, name);
},
defineProperty: function(_, name, desc){
if(desc.configurable === false)
throw new TypeError('Nope');
else
return Object.defineProperty(this.target, name, desc);
},
isExtensible: function () {
return true;
},
isSealed: function () {
return false;
},
isFrozen: function () {
return false;
},
preventExtensions: function(){
throw new TypeError('Nope');
},
freeze: function(){
throw new TypeError('Nope');
},
seal: function(){
throw new TypeError('Nope');
}
};
function retargetableProxy(target, dummyTarget) {
var handler = new WindowProxyHandler(target);
return {
proxy: new Proxy(dummyTarget, handler),
setTarget: function(newTarget){
handler.target = newTarget;
}
};
}
function Window(){/* create window instances */}
var dummyWindowProxyTarget = {
get location(){},
set location(url){},
get document(){}
// ...
};
function WindowProxy(){
var {proxy, setTarget} = retargetableProxy(new Window(), dummyWindowProxyTarget);
return proxy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment