Skip to content

Instantly share code, notes, and snippets.

@ilucin
Created February 24, 2017 11:49
Show Gist options
  • Save ilucin/3b2802d36f61335d06aca68b5d7dfab6 to your computer and use it in GitHub Desktop.
Save ilucin/3b2802d36f61335d06aca68b5d7dfab6 to your computer and use it in GitHub Desktop.
function proxyMethods(proxyObj, originalObj, props) {
props.forEach(function(method) {
proxyObj[method] = function() {
return originalObj[method](...arguments);
};
});
}
function proxyProps(proxyObj, originalObj, props) {
props.forEach(function(prop) {
Object.defineProperty(proxyObj, prop, {
get() {
return originalObj[prop];
},
set(val) {
originalObj[prop] = val;
}
});
});
}
export default function(iframe) {
const fakeDocument = {};
const fakeWindow = {document: fakeDocument};
proxyMethods(fakeDocument, window.document, ['querySelector', 'createElement', 'appendChild', 'getElementById', 'getElementsByTagName', 'addEventListener', 'removeEventListener']);
proxyMethods(fakeWindow, window, ['addEventListener', 'removeEventListener']);
proxyProps(fakeDocument, window.document, ['body', 'location']);
proxyProps(fakeWindow, window, ['Intercom', 'location']);
iframe.contentWindow.parent = fakeWindow;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment