Skip to content

Instantly share code, notes, and snippets.

@loskael
Created June 8, 2022 05:17
Show Gist options
  • Save loskael/4f5372daef5b06fa271bf32f1694d702 to your computer and use it in GitHub Desktop.
Save loskael/4f5372daef5b06fa271bf32f1694d702 to your computer and use it in GitHub Desktop.
iframe sandbox
(function (script, callback) {
var iframe = document.createElement('iframe');
var blob = new Blob([`<script>(${script.toString()})();</script>`], { 'type': 'text/html' });
iframe.src = URL.createObjectURL(blob);
iframe.style.display = 'none';
iframe.onload = function () {
typeof callback === 'function' && callback(iframe.contentWindow);
};
document.body.appendChild(iframe);
})(
function () {
window.r = Math.random();
},
function (win) {
console.log(win.r);
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment