Created
February 24, 2017 11:49
-
-
Save ilucin/3b2802d36f61335d06aca68b5d7dfab6 to your computer and use it in GitHub Desktop.
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 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