How the sandbox works
Your code is placed in a generated HTML document and loaded into a hidden iframe through srcdoc. The iframe has sandbox="allow-scripts" and nothing else. MDN notes that omitting allow-same-origin gives the framed document an opaque origin, which is what stops it from touching this page's storage or DOM.
The document also carries a Content Security Policy with default-src 'none' and connect-src 'none', so the snippet cannot call out to a server or load images. The only channel back is postMessage, and the parent page accepts a message only if it comes from that frame, carries the per-run token, and has the expected shape.
- Evaluation: the code runs through indirect eval, so top-level var and function declarations behave like global code.
- Console: log, info, warn and error are replaced to forward their arguments to the output panel.
- Errors: uncaught errors and unhandled rejections are reported with the line number from the stack when the browser provides one.