-
Notifications
You must be signed in to change notification settings - Fork 64
/
beta.html
80 lines (65 loc) · 2.31 KB
/
beta.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html lang="en">
<head>
<title>Programming Quantum Computers</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript">
window.onerror = function(message, source, lineno, colno, error)
{ alert('Beta Error: ' + message + ' src:' + source + ' line:' + lineno + ' col:' + colno); }
</script>
<script type="text/javascript" src="./sample_shortcuts.js"></script>
</head>
<body>
<iframe id="sandbox_iframe" src="./beta_sandbox.html" width="100%" height="1000" sandbox="allow-scripts"></iframe>
<script>
function load_code_from_url_and_send(sample_url)
{
var frame = document.getElementById('sandbox_iframe');
var http_request = new XMLHttpRequest();
http_request.open('GET', sample_url, true);
// If specified, responseType must be empty string or "text"
http_request.responseType = 'text';
http_request.onload = function () {
if (http_request.readyState === http_request.DONE)
{
if (http_request.status === 200)
{
frame.contentWindow.postMessage({cmd:'got_sample',sample_url:sample_url,sample_code:http_request.responseText}, '*');
}
else
{
frame.contentWindow.postMessage({cmd:'get_sample_failed',sample_url:sample_url}, '*');
}
}
};
http_request.onerror = function ()
{
frame.contentWindow.postMessage({cmd:'get_sample_failed',sample_url:sample_url}, '*');
};
try {
http_request.send(null);
}
catch (error) {
frame.contentWindow.postMessage({cmd:'get_sample_failed',sample_url:sample_url}, '*');
}
}
window.addEventListener('message', function(event) {
// IMPORTANT: Check the origin of the data!
if (1 || ~event.origin.indexOf('http://yoursite.com')) {
// The data has been sent from your site
// The data sent with postMessage is stored in event.data
console.log(event.data);
if (event.data.cmd == 'get_sample')
{
load_code_from_url_and_send(event.data.sample_url);
}
} else {
// The data hasn't been sent from your site!
// Be careful! Do not use it.
return;
}
});
</script>
</body>
</html>