Skip to content

Commit 0e6f8b2

Browse files
committed
Add require shims, sandboxed frame
1 parent 1e8c6b5 commit 0e6f8b2

File tree

8 files changed

+420
-277
lines changed

8 files changed

+420
-277
lines changed

frame.html

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>Try PureScript!</title>
5+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
6+
<meta content="utf-8" http-equiv="encoding">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<link rel="stylesheet" href="css/style.css">
9+
</head>
10+
<body>
11+
<main id="main"></main>
12+
<script type="text/javascript">
13+
(function() {
14+
function evalPureScript(sources) {
15+
var modules = {};
16+
return function load(name) {
17+
if (modules[name]) {
18+
return modules[name].exports;
19+
}
20+
function require(path) {
21+
if (path === "./foreign.js") {
22+
return load(name + "$Foreign");
23+
}
24+
if (path.indexOf("../") === 0) {
25+
return load(path.slice(3).replace(/\/index\.js$/, ""));
26+
}
27+
return load(path);
28+
}
29+
var module = modules[name] = { exports: {} };
30+
new Function("module", "exports", "require", sources[name])(module, module.exports, require);
31+
return module.exports;
32+
};
33+
}
34+
35+
function loadScripts(scripts, cb) {
36+
if (scripts.length === 0) {
37+
return cb();
38+
}
39+
var script = document.createElement("script");
40+
script.type = "text/javascript";
41+
script.src = scripts[0];
42+
script.addEventListener("load", function() {
43+
loadScripts(scripts.slice(1), cb);
44+
}, { once: true });
45+
document.head.appendChild(script);
46+
}
47+
48+
document.addEventListener("DOMContentLoaded", function() {
49+
window.addEventListener("message", function(event) {
50+
loadScripts(event.data.scripts, function() {
51+
var file = evalPureScript(event.data.sources)("<file>");
52+
if (file.main && typeof file.main === "function") {
53+
file.main();
54+
}
55+
});
56+
event.source.postMessage("trypurescript", "*");
57+
}, { once: true });
58+
}, { once: true });
59+
})();
60+
</script>
61+
</body>
62+
</html>

index.html

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,30 +188,20 @@
188188
})(marker));
189189
}
190190

191-
function setupIFrame($ctr, html, js) {
192-
var $iframe = $('<iframe id="output-iframe">');
191+
function setupIFrame($ctr, data) {
192+
var $iframe = $('<iframe sandbox="allow-scripts" id="output-iframe" src="frame.html">');
193193

194194
$ctr
195195
.empty()
196196
.append($iframe);
197197

198-
var iframe = $iframe.get(0).contentWindow.document;
199-
iframe.open();
200-
iframe.write(html);
201-
iframe.close();
202-
203-
var script = iframe.createElement('script');
204-
script.appendChild(iframe.createTextNode(js));
205-
206-
$iframe.ready(function() {
207-
var checkExists = setInterval(function() {
208-
var body = iframe.getElementsByTagName('body')[0];
209-
if (body) {
210-
body.appendChild(script);
211-
clearInterval(checkExists);
212-
}
213-
}, 100);
214-
});
198+
var sendSources = setInterval(function() {
199+
$iframe.get(0).contentWindow.postMessage(data, "*");
200+
}, 100);
201+
202+
window.addEventListener("message", function() {
203+
clearInterval(sendSources);
204+
}, { once: true });
215205

216206
return $iframe;
217207
}

0 commit comments

Comments
 (0)