Skip to content

Instantly share code, notes, and snippets.

@eligrey
Last active December 7, 2023 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eligrey/b4530639c3deed04e1b32d7a3cae52cc to your computer and use it in GitHub Desktop.
Save eligrey/b4530639c3deed04e1b32d7a3cae52cc to your computer and use it in GitHub Desktop.
Execute all scripts in a document. You'll need this if you're inserting external documents.
var doc = (new DOMParser).parseFromString("<html><script>alert(1)</script></html>", "text/html");
document.replaceChild(doc.documentElement, document.documentElement);
execute_scripts(); // alert(1)
var execute_scripts = function() {
for (let script of Array.from(document.scripts)) {
script.replaceWith(document.importNode(script, true), script);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment