const makeSandboxedIframe = ( bodyHtml: string, scripts: string[], ) => { const iframe = document.createElement('iframe'); // allow-modals: Allows confirm/alert dialogs. iframe.setAttribute('sandbox', 'allow-scripts allow-modals'); iframe.addEventListener('load', async () => { iframe.contentWindow.postMessage({ kind: 'add-script', scripts, }, '*'); }, { once: true }); iframe.srcdoc = ` ${bodyHtml} `; return { iframe, loadPromise: new Promise(resolve => { iframe.addEventListener('load', () => resolve(), { once: true }); }), }; }; export default makeSandboxedIframe;