Skip to content

Instantly share code, notes, and snippets.

@IhostVlad
Created April 19, 2019 09:26
Show Gist options
  • Save IhostVlad/fa739ac530005724ee0b249043281096 to your computer and use it in GitHub Desktop.
Save IhostVlad/fa739ac530005724ee0b249043281096 to your computer and use it in GitHub Desktop.
Eval with CSP
window.metaEval = async (code) => {
const evalId = "META_EVAL_"+ Date.now() + "_" + Math.floor(Math.random() * 1000000000)
document.body.innerHTML += "<iframe id=\""+ evalId +"\" src=\"about:blank\"></iframe>"
const ifr = document.getElementById(evalId)
let makeResolve = null
const promise = new Promise((resolve) => (makeResolve = resolve))
let result = null
const listener = (msg) => { result = msg.data; makeResolve(); }
window.addEventListener("message", listener)
ifr.src = "data:text/html,<script>top.postMessage(eval(decodeURIComponent(\""
+ encodeURIComponent(code) +
"\")), \""+ location.origin +"\")</script>"
await promise
document.body.removeChild(ifr)
window.removeEventListener("message", listener)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment