Skip to content

Commit bcd57a6

Browse files
committed
Refactor the iframe generator
1 parent e04b254 commit bcd57a6

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

src/theme/book.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,20 @@ function playground_text(playground) {
144144
.then(response => {
145145
result_block.innerText = "";
146146
var iframe = result_block.appendChild(document.createElement('iframe')),
147-
doc = iframe.contentWindow.document,
148-
options = {
149-
objid: 152,
150-
key: 316541321
151-
},
152-
//src = "host/widget.js",
153-
uri = encodeURIComponent(JSON.stringify(options));
154-
doc.someVar = "Hello World!";
155-
iframe.id = "iframewidget";
156-
iframe.width = result_block.width;
157-
iframe.height = result_block.height;
158-
159-
var html = '<body onload="var d=document;' +
160-
'var script=d.createElement(\'script\');script.type=\'module\';script.src=\'wasm-test.js\';' +
161-
'd.getElementsByTagName(\'head\')[0].appendChild(script);"><div id="button_click"></div></body>';
162-
doc.open().write(html);
163-
doc.close();
147+
doc = iframe.contentWindow.document;
148+
iframe.id = "wasm-rendering";
149+
iframe.style.width = "100%";
150+
iframe.style.height = "100%";
151+
var xhr = new XMLHttpRequest();
152+
xhr.open('GET', 'iframe.html', true);
153+
xhr.onreadystatechange = function () {
154+
if (this.readyState !== 4) return;
155+
if (this.status !== 200) return; // or whatever error handling you want
156+
var html = this.responseText;
157+
doc.open().write(html);
158+
doc.close();
159+
};
160+
xhr.send();
164161
})
165162
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
166163

src/theme/iframe.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!-- Code injected from iframe.html -->
2+
<head>
3+
<script>
4+
// dynamically create script element and append to head
5+
// call wasm-entry to load the built wasm.js
6+
function body_onload() {
7+
var d = document;
8+
var script = d.createElement('script');
9+
script.type = 'module';
10+
script.src = 'wasm-entry.mjs';
11+
d.getElementsByTagName('head')[0].appendChild(script);
12+
}
13+
</script>
14+
</head>
15+
16+
<body onload="body_onload();">
17+
<div id="_start"></div>
18+
</body>

src/theme/wasm-entry.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import init from './wasm.js';
2+
3+
init();

0 commit comments

Comments
 (0)