-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
iframe-api-explorer.html
239 lines (214 loc) · 9.61 KB
/
iframe-api-explorer.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html>
<head>
<title>API Explorer</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
margin: 0;
padding: 20px;
background: #f0f0f0;
}
iframe {
width: 100%;
border: 1px solid #ccc;
border-radius: 4px;
background: white;
transition: height 0.2s ease;
}
</style>
</head>
<body>
<iframe id="explorerFrame" sandbox="allow-scripts allow-forms"></iframe>
<script>
// Create the HTML content for the iframe
const iframeContent = `
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
margin: 0;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
}
.input-group {
display: flex;
gap: 8px;
margin-bottom: 20px;
}
input[type="text"] {
flex: 1;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
button {
padding: 8px 16px;
background: #0066cc;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background: #0052a3;
}
pre {
background: #f5f5f5;
padding: 20px;
border-radius: 4px;
overflow-x: auto;
white-space: pre-wrap;
word-wrap: break-word;
}
.error {
color: #cc0000;
}
</style>
</head>
<body>
<div class="container">
<h1>API Explorer</h1>
<p>This is an experiment in <code><iframe></code> sandboxing and <code>window.parent.postMessage()</code>.</p>
<form onsubmit="makeRequest(); return false;">
<div class="input-group">
<input type="text" id="urlInput" placeholder="Enter API URL" value="https://datasette.io/content/stats.json?_size=1">
<button type="submit">Submit</button>
</div>
</form>
<pre id="output">// Response will appear here</pre>
</div>
<script>
class APIClient {
constructor(targetOrigin = '*') {
this.targetOrigin = targetOrigin;
this.pendingRequests = new Map();
this.requestId = 0;
// Set up message listener
window.addEventListener('message', this.handleMessage.bind(this));
}
handleMessage(event) {
if (event.data?.type !== 'api-response' || !event.data?.requestId) {
return;
}
const { requestId, error, response } = event.data;
const pendingRequest = this.pendingRequests.get(requestId);
if (!pendingRequest) {
return;
}
this.pendingRequests.delete(requestId);
if (error) {
pendingRequest.reject(new Error(error));
} else {
pendingRequest.resolve(response);
}
}
async callAPI(url) {
const requestId = ++this.requestId;
const promise = new Promise((resolve, reject) => {
this.pendingRequests.set(requestId, { resolve, reject });
// Set a timeout to prevent hanging promises
setTimeout(() => {
if (this.pendingRequests.has(requestId)) {
this.pendingRequests.delete(requestId);
reject(new Error('Request timed out'));
}
}, 30000); // 30 second timeout
});
// Send the request to parent
window.parent.postMessage({
type: 'api-request',
requestId,
url,
method: 'GET'
}, this.targetOrigin);
return promise;
}
}
const api = new APIClient();
// Height management
let currentHeight = null;
function updateParentHeight() {
let newHeight = Math.min(
Math.max(
document.body.offsetHeight, document.documentElement.offsetHeight
),
Math.max(
Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight
),
Math.max(
document.body.clientHeight, document.documentElement.clientHeight
)
)
) + 10;
if (currentHeight === newHeight) {
return;
}
currentHeight = newHeight;
window.parent.postMessage({
type: 'resize',
height: newHeight
}, '*');
}
updateParentHeight();
setInterval(updateParentHeight, 1000);
async function makeRequest() {
const url = document.getElementById('urlInput').value;
const output = document.getElementById('output');
output.textContent = 'Loading...';
try {
const response = await api.callAPI(url);
output.textContent = JSON.stringify(response, null, 2);
output.classList.remove('error');
} catch (error) {
output.textContent = 'Error: ' + error.message;
output.classList.add('error');
}
// Content has changed, update height
setTimeout(updateParentHeight, 100);
}
SCUB
</body>
</html>
`.replace('SCUB', '</' + 'script>');
// Set up the iframe with the content
const iframe = document.getElementById('explorerFrame');
iframe.src = 'data:text/html;charset=utf-8,' + encodeURIComponent(iframeContent);
// Listen for messages from the iframe
window.addEventListener('message', async function(event) {
if (event.data.type === 'api-request') {
try {
const response = await fetch(event.data.url, {
method: event.data.method
});
const data = await response.json();
// Send response back to iframe
iframe.contentWindow.postMessage({
type: 'api-response',
requestId: event.data.requestId,
response: data
}, '*');
} catch (error) {
// Send error back to iframe
iframe.contentWindow.postMessage({
type: 'api-response',
requestId: event.data.requestId,
error: error.message
}, '*');
}
} else if (event.data.type === 'resize') {
// Update iframe height when child requests it
iframe.style.height = event.data.height + 'px';
}
});
</script>
</body>
</html>