Skip to content

Instantly share code, notes, and snippets.

@joker314
Created August 7, 2018 05:07
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 joker314/bfb5bce4b9ee344d185d74f720628acf to your computer and use it in GitHub Desktop.
Save joker314/bfb5bce4b9ee344d185d74f720628acf to your computer and use it in GitHub Desktop.
// Editor
const iframe = document.querySelector("iframe").contextWindow
const VIEWING_DOMAIN = "https://viewing.example.com"
function runJS(codeString, args, cb) {
const id = rememberCallback(cb)
iframe.postMessage({codeString, args, id}, EDITOR_DOMAIN)
}
window.addEventListener("message", function({id, result}, origin, source) {
// id, and result, are NOT trusted.
if(origin != VIEWING_DOMAIN) return;
getCallback(id)(result)
})
// Viewer
const EDITOR_DOMAIN = "https://editing.example.com"
window.addEventListener("message", function({codeString, args, id}, origin, source) {
if(origin != EDITOR_DOMAIN) return;
const func = eval("function() {" + codeString + "}") // or however you do it currently
const evaluated = func(...args)
source.postMessage({result: evaluated.toString(), id})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment