Skip to content

Instantly share code, notes, and snippets.

@t0rr3sp3dr0
Created January 12, 2018 07:10
Show Gist options
  • Save t0rr3sp3dr0/c0718f5e42ce3f26d3ce053dcb79a4dd to your computer and use it in GitHub Desktop.
Save t0rr3sp3dr0/c0718f5e42ce3f26d3ce053dcb79a4dd to your computer and use it in GitHub Desktop.
const run = (glue, stdin, stdout, stderr) => {
const iframe = document.createElement('iframe');
iframe.setAttribute('style', 'display: none; ');
iframe.src = 'about:blank';
iframe.onload = () => {
iframe.contentWindow.Module = {
preRun: [
() => {
const _in = new TextEncoder('ascii').encode(stdin).values();
iframe.contentWindow.FS.init(() => _in.next().value, null, null);
},
],
postRun: [
() => document.body.removeChild(iframe),
],
print: (...arguments) => stdout(arguments.join(' ')),
printErr: (...arguments) => stderr(arguments.join(' ')),
setStatus: console.log,
monitorRunDependencies: (left) => {
this.totalDependencies = Math.max(this.totalDependencies || 0, left);
console.log(left ? 'Preparing... (' + (this.totalDependencies - left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
},
};
const script = document.createElement('script');
script.setAttribute('async', '');
script.src = glue;
iframe.contentDocument.body.appendChild(script);
}
document.body.appendChild(iframe);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment