Refactor the iframe generator

This commit is contained in:
titaneric 2021-03-21 00:53:20 +08:00
parent e04b254b48
commit bcd57a6b22
3 changed files with 35 additions and 17 deletions

View File

@ -144,23 +144,20 @@ function playground_text(playground) {
.then(response => { .then(response => {
result_block.innerText = ""; result_block.innerText = "";
var iframe = result_block.appendChild(document.createElement('iframe')), var iframe = result_block.appendChild(document.createElement('iframe')),
doc = iframe.contentWindow.document, doc = iframe.contentWindow.document;
options = { iframe.id = "wasm-rendering";
objid: 152, iframe.style.width = "100%";
key: 316541321 iframe.style.height = "100%";
}, var xhr = new XMLHttpRequest();
//src = "host/widget.js", xhr.open('GET', 'iframe.html', true);
uri = encodeURIComponent(JSON.stringify(options)); xhr.onreadystatechange = function () {
doc.someVar = "Hello World!"; if (this.readyState !== 4) return;
iframe.id = "iframewidget"; if (this.status !== 200) return; // or whatever error handling you want
iframe.width = result_block.width; var html = this.responseText;
iframe.height = result_block.height; doc.open().write(html);
doc.close();
var html = '<body onload="var d=document;' + };
'var script=d.createElement(\'script\');script.type=\'module\';script.src=\'wasm-test.js\';' + xhr.send();
'd.getElementsByTagName(\'head\')[0].appendChild(script);"><div id="button_click"></div></body>';
doc.open().write(html);
doc.close();
}) })
.catch(error => result_block.innerText = "Playground Communication: " + error.message); .catch(error => result_block.innerText = "Playground Communication: " + error.message);

18
src/theme/iframe.html Normal file
View File

@ -0,0 +1,18 @@
<!-- Code injected from iframe.html -->
<head>
<script>
// dynamically create script element and append to head
// call wasm-entry to load the built wasm.js
function body_onload() {
var d = document;
var script = d.createElement('script');
script.type = 'module';
script.src = 'wasm-entry.mjs';
d.getElementsByTagName('head')[0].appendChild(script);
}
</script>
</head>
<body onload="body_onload();">
<div id="_start"></div>
</body>

3
src/theme/wasm-entry.mjs Normal file
View File

@ -0,0 +1,3 @@
import init from './wasm.js';
init();