-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
142 lines (123 loc) · 4.49 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PostMessage Child</title>
<style type="text/css">
html {
font-size: 16px;
font-family: sans-serif;
}
body {
color: rgba(0, 0, 0, 0.8);
margin: 0;
}
* {
box-sizing: border-box;
}
form {
width: 480px;
box-shadow: 2px 2px 5px 0 rgba(0, 0, 0, 0.3);
background: white;
padding: 1rem;
margin: 50px auto;
}
button,
textarea,
input {
padding: 0.5rem;
font-size: 1rem;
}
textarea,
input[type="text"] {
width: 100%;
}
textarea {
height: 5rem;
}
.row {
padding-bottom: 1rem;
text-align: right;
}
</style>
</head>
<body>
<form>
<div class="row">
<textarea id="message" placeholder="Message" autofocus></textarea>
</div>
<div class="row">
<label for="json">JSON</label><input type="checkbox" id="json">
</div>
<div class="row">
<input type="text" placeholder='Target Origin (defaults to "*")' id="origin">
</div>
<div class="row">
<button type="submit" id="send-button">Send</button>
</div>
<hr>
<p>
<strong>Pro tip:</strong> You can prepopulate this form via the query string parameters:
<strong>message</strong>, <strong>json</strong>, and
<strong>origin</strong>.
</p>
<div class="row">
<button type="button" id="generate">Generate URL from Data</button>
</div>
<div class="row">
<input type="text" readonly id="url">
</div>
</form>
<script>
(function() {
'use strict';
const formEl = document.querySelector('form');
const messageEl = document.querySelector('#message');
const jsonEl = document.querySelector('#json');
const originEl = document.querySelector('#origin');
const generateEl = document.querySelector('#generate');
const urlEl = document.querySelector('#url');
const params = window.location.search
.split('&')
.reduce((paramsObject, queryString) => {
const matches = queryString.match(/\??([^=]*)=([^&]*)/);
if (matches) {
paramsObject[
window.decodeURIComponent(matches[1])
] = window.decodeURIComponent(matches[2]);
}
return paramsObject;
}, {});
messageEl.value = params.message || '';
jsonEl.checked = params.json;
originEl.value = params.origin || '';
formEl.addEventListener('submit', event => {
event.preventDefault();
window.parent.postMessage(
jsonEl.checked ? JSON.parse(messageEl.value) : messageEl.value,
originEl.value || '*'
);
});
generateEl.addEventListener('click', () => {
const message = window.encodeURIComponent(messageEl.value);
const origin = window.encodeURIComponent(originEl.value);
const params = [];
if (message) {
params.push(`message=${message}`);
}
if (jsonEl.checked) {
params.push('json=1');
}
if (origin) {
params.push(`origin=${origin}`);
}
if (params.length) {
urlEl.value = `${window.location.origin}${window.location.pathname}?${params.join('&')}`;
} else {
urlEl.value = '';
}
});
}());
</script>
</body>
</html>