-
-
Save joker314/bfb5bce4b9ee344d185d74f720628acf to your computer and use it in GitHub Desktop.
This file contains 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
// 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) | |
}) |
This file contains 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
// 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