Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Zemnmez
Created November 19, 2019 17:45
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 Zemnmez/b5f231e4ff76031ac540b0a8651e6a96 to your computer and use it in GitHub Desktop.
Save Zemnmez/b5f231e4ff76031ac540b0a8651e6a96 to your computer and use it in GitHub Desktop.
attempt to recursively hook iframe postMessages (doesn't work due to browser security policies)
((() => {
console.log("postMessage hook added");
new MutationObserver((mutations, observer) => {
const flatten = (a,c) => a.concat(c);
const allNodes = mutations.filter(({ type }) => type == "childList")
.map(({ addedNodes }) => Array.from(addedNodes)).reduce(flatten, []);
allNodes
.forEach(parentNode => {
if (!parentNode.getElementsByTagName) return;
[...parentNode.getElementsByTagName("iframe")].forEach(iframe => {
let x = iframe.contentWindow.postMessage;
console.log("hooked", iframe);
iframe.contentWindow.postMessage = function(...a) {
console.log(`SEND to`, iframe, `${a}`);
x.apply(iframe, [...a]);
}
})
})
}).observe(document.documentElement, {childList: true, subtree: true });
window.addEventListener("message", e => console.log(`RCV from`, e.origin, {...e}))
})(),false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment