-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest-embed.html
More file actions
144 lines (137 loc) · 3.62 KB
/
test-embed.html
File metadata and controls
144 lines (137 loc) · 3.62 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Embed Test</title>
<style media="screen">
body {
margin: 0;
padding: 30px;
font-family: sans-serif;
}
.row {
width: 100%;
display: flex;
justify-content: space-between;
align-items: stretch;
}
body > .row {
flex-grow: 1;
min-width: 200px;
flex-wrap: wrap;
margin-top: 1em;
}
body > .row > * {
flex-grow: 1;
min-width: 200px;
}
.row input,
.row textarea {
width: 100% !important;
margin-bottom: 1em;
}
#sendMessage,
#clearOutput {
position: sticky;
top: 1em;
}
#output {
font-family: monospace;
white-space: pre-wrap;
}
</style>
</head>
<body>
<iframe
id="ninja"
src="http://localhost:8081/"
width="100%"
height="600px"
style="width: 100%; height: 600px; max-height: 100vh"
frameborder="0"
></iframe>
<!-- Add 'allowfullscreen' above to show fullscreen toggle button -->
<!-- Input/Output -->
<div class="row">
<fieldset>
<legend>Send Message</legend>
<form id="sendMessage" method="post">
<input name="action" placeholder="Action" />
<textarea name="value" placeholder="Value"></textarea>
<div class="row">
<input type="reset" value="Clear" />
<input type="submit" value="Send" />
</div>
</form>
</fieldset>
<fieldset>
<legend>Received Messages</legend>
<input
id="clearOutput"
type="button"
value="Clear"
onclick="output.value = ''"
/>
<output id="output"></output>
</fieldset>
</div>
<script type="text/javascript">
// Set up message receiver
const ninja = document.getElementById("ninja").contentWindow;
let ptnNinjaHasLoaded = false;
window.addEventListener(
"message",
(event) => {
// Filter out messages from browser extensions etc.
if (event.source !== ninja) {
return;
}
// Wait for PTN Ninja to finish loading
if (!ptnNinjaHasLoaded) {
if (event.data.action === "GAME_STATE") {
ptnNinjaHasLoaded = true;
} else {
// Ignore other messages until fully loaded
return;
}
}
// Handle actions
switch (event.data.action) {
// case "ACTION":
// do something
// break;
default:
document.getElementById("output").value +=
JSON.stringify(event.data, null, 2) + "\n";
}
},
false
);
// Send messages from the textarea
const form = document.getElementById("sendMessage");
form.addEventListener(
"submit",
(event) => {
event.preventDefault();
const data = new FormData(event.target);
const action = data.get("action");
const valueString = data.get("value");
let value;
try {
value = JSON.parse(valueString);
} catch (error) {
value = valueString;
}
try {
ninja.postMessage({ action, value }, "*");
} catch (error) {
console.error(error);
}
return false;
},
true
);
</script>
</body>
</html>