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