-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiframe.js
406 lines (281 loc) · 11.8 KB
/
iframe.js
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
let loadingTimeout;
// render iframe
async function renderFrame(url) {
// if an iframe already exists
if (document.querySelector('iframe')) {
// remove old iframe
document.querySelectorAll('iframe').forEach(iframe => { iframe.remove() });
}
// add protocol
if (url.search(/^http[s]?\:\/\//) == -1) {
url = 'https://' + url;
} else {
var urlObj = new URL(url);
urlObj.protocol = 'https:';
url = urlObj.href;
}
// push new url to history
window.history.pushState({}, Math.random(), (window.location.origin + '/?url=' + url));
// show loading screen
document.querySelector('.loading').classList = 'loading';
document.querySelector('.loading-image').classList.remove('loaded');
// set a loading timeout
clearLoadingTimeout();
loadingTimeout = window.setTimeout(awSnap, 10000);
// create a HTTP Request with CORS headers
const resp = await axios.get(url);
// inject html into temporary iframe
// for HTML manipulation
var tempFrame = document.createElement('iframe');
tempFrame.visibility = false;
document.body.appendChild(tempFrame);
var theTempDoc = tempFrame.contentDocument;
theTempDoc.documentElement.innerHTML = resp;
// add base url to iframe to prevent breaking relative URLs
var base = theTempDoc.createElement('base');
base.href = url;
theTempDoc.head.appendChild(base);
// create new iframe
var newFrame = document.createElement('iframe');
newFrame.frameBorder = 0;
newFrame.allow = 'camera; gyroscope; microphone; autoplay; clipboard-write; encrypted-media; picture-in-picture; accelerometer';
document.body.appendChild(newFrame);
var tempDoc = newFrame.contentDocument;
tempDoc.documentElement.innerHTML = theTempDoc.documentElement.innerHTML;
// remove old iframe
tempFrame.remove();
// add scepter shadow boundary CSS to iframe
let style = document.createElement('style');
style.textContent = scepterOutlyingCSS;
tempDoc.head.appendChild(style);
// show site image in loading screen
var loadingImage = document.querySelector('.loading-image');
var ogImage = tempDoc.querySelector('meta[property="og:image"]');
// if og image exists
if (ogImage) {
// get og image with base URL
var ogImageUrl = new URL(ogImage.content, url).href;
// show og image in loading screen
loadingImage.src = ogImageUrl;
loadingImage.onload = () => { loadingImage.classList.add('loaded') };
}
// redirect all links
tempDoc.querySelectorAll('a[href]').forEach((a) => {
// if URL does not redirect to current page
if (a.href != url && a.href != '#') {
// get href with base URL
var newHref = new URL(a.href, url).href;
a.onclick = (e) => {
e.preventDefault();
renderFrame(newHref);
};
} else {
a.onclick = (e) => {
e.preventDefault();
};
}
})
// redirect all images
tempDoc.querySelectorAll('img[src]').forEach((img) => {
// get src with base URL
var newSrc = new URL(img.src, url).href;
img.src = newSrc;
})
// run all scripts
tempDoc.querySelectorAll('script').forEach((script) => {
var code = '';
// if script is external
if (script.src) {
addScript(newFrame.contentWindow.document, script.src, script.type);
// delete original
script.remove();
} else {
code = script.innerHTML;
}
// filter scripts
// allow framing
if (code.includes('top!==self')) code = code.replace('top!==self','self!==self');
if (code.includes('window.top')) code = code.replace('window.top','window');
if (code.includes('window.parent')) code = code.replace('window.parent','window');
// prevent changing domain
if (code.includes('window.document.domain')) code = code.replace('window.document.domain','window.location.origin');
// redirect on changing location of window
/* if (code.includes('window.location.href=')) code = code.replace('window.location.href=','window.parent.renderFrame(');
if (code.includes('window.location.href =')) code = code.replace('window.location.href =','window.parent.renderFrame('); */
})
// add scepter to iframe
addScript(newFrame.contentWindow.document, '', false, scepterClass);
// add the scepter element to dom
var scepterElem = tempDoc.createElement('scepter-element');
tempDoc.body.appendChild(scepterElem);
}
var axios = {
'get': async (url) => {
let resp = await fetch('/api/cors', {
headers: {
'request-url': url
}
});
resp = await resp.text();
return resp;
}
}
function addScript(documentNode, src, type, code) {
var script = documentNode.createElement('script');
script.type = (type !== false && type !== '') ? type : 'application/javascript';
if (code) {
script.appendChild(documentNode.createTextNode(code));
} else {
script.src = src;
script.defer = true;
script.async = false;
}
script.onerror = (e) => {
documentNode.defaultView.console.error(e);
}
documentNode.head.appendChild(script);
}
// display "Aw, snap!" error message
function awSnap() {
/* what did it take?.. */
document.querySelector('.loading').classList.add('snap');
/* ..everything */
document.querySelector('.loading .subtitle').innerText = 'Aw, snap! Timed out.';
}
// clear loading timeout
function clearLoadingTimeout() {
window.clearTimeout(loadingTimeout);
}
var scepterClass = `
// create a class for the scepter element
class ScepterElement extends HTMLElement {
constructor() {
// always call super first in constructor
super();
// get parent window
let parentWindow = window.parent;
// create a shadow root
let shadow = this.attachShadow({mode: 'open'});
// add scepter HTML to shadow dom
shadow.innerHTML = parentWindow.scepterHTML;
// apply external styles to the shadow dom
const linkElem = document.createElement('link');
linkElem.setAttribute('rel', 'stylesheet');
linkElem.setAttribute('href', '`+ window.location.origin +`/scepter.css');
// hide loader when styles are loaded
linkElem.onload = () => {
if (!parentWindow.document.querySelector('.loading').classList.contains('snap')) {
parentWindow.document.querySelector('.loading').classList.add('hidden');
// clear loading timeout
parentWindow.clearLoadingTimeout();
}
};
// attach the created element to the shadow dom
shadow.appendChild(linkElem);
// reload page
function fireEvent(element, event) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
}
var links = document.getElementsByTagName("link");
var st = [];
for (var x = 0; x < links.length; x++) {
if (links[x].getAttribute('rel') == 'stylesheet') {
st.push(links[x]);
links[x].wasAtt = links[x].getAttribute('href');
links[x].setAttribute('href', '');
}
}
setTimeout(() => {
for (var x = 0; x < st.length; x++) {
st[x].setAttribute('href', st[x].wasAtt);
// if could not fetch the resource normally, try a CORS fetch
st[x].onerror = function() {
/*
st[x].setAttribute('crossorigin', '');
st[x].setAttribute('href', '');
setTimeout(() => {
st[x].setAttribute('href', st[x].wasAtt);
}, 0);*/
};
}
setTimeout(() => {
fireEvent(window, "load");
setTimeout(() => {
parentWindow.document.querySelector('iframe').onload = () => {
parentWindow.pushUrl();
};
}, 0);
}, 0);
}, 0);
// init scepter
parentWindow.scepter.init(document.body, shadow);
}
}
// define the scepter element
window.customElements.define('scepter-element', ScepterElement);
`;
var scepterHTML = `
<div class="overlay"></div>
<div class="inspector">
<div class="option class">
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0V0z" fill="none"></path><path d="M18 2H6c-1.1 0-2 .9-2 2v16c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zM6 4h5v8l-2.5-1.5L6 12V4z" fill="currentColor"></path></svg>
<a>Classes and IDs</a>
</div>
<div class="option console">
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0V0z" fill="none"></path><path d="M15 7.29V3c0-.55-.45-1-1-1h-4c-.55 0-1 .45-1 1v4.29c0 .13.05.26.15.35l2.5 2.5c.2.2.51.2.71 0l2.5-2.5c.09-.09.14-.21.14-.35zM7.29 9H3c-.55 0-1 .45-1 1v4c0 .55.45 1 1 1h4.29c.13 0 .26-.05.35-.15l2.5-2.5c.2-.2.2-.51 0-.71l-2.5-2.5C7.55 9.05 7.43 9 7.29 9zM9 16.71V21c0 .55.45 1 1 1h4c.55 0 1-.45 1-1v-4.29c0-.13-.05-.26-.15-.35l-2.5-2.5c-.2-.2-.51-.2-.71 0l-2.5 2.5c-.09.09-.14.21-.14.35zm7.35-7.56l-2.5 2.5c-.2.2-.2.51 0 .71l2.5 2.5c.09.09.22.15.35.15H21c.55 0 1-.45 1-1v-4c0-.55-.45-1-1-1h-4.29c-.14-.01-.26.04-.36.14z" fill="currentColor"></path></svg>
<a>Console</a>
</div>
<div class="option code" style="display:none">
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0V0z" fill="none"></path><path d="M9.4 16.6L4.8 12l4.6-4.6L8 6l-6 6 6 6 1.4-1.4zm5.2 0l4.6-4.6-4.6-4.6L16 6l6 6-6 6-1.4-1.4z" fill="currentColor"></path></svg>
<a>Add new CSS</a>
</div>
</div>
<div class="expanded--overlay"></div>
<div class="popover">
<div class="header">
<div class="type"><div></div>
<svg class="close" xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"></path><path d="M12 2C6.47 2 2 6.47 2 12s4.47 10 10 10 10-4.47 10-10S17.53 2 12 2zm5 13.59L15.59 17 12 13.41 8.41 17 7 15.59 10.59 12 7 8.41 8.41 7 12 10.59 15.59 7 17 8.41 13.41 12 17 15.59z" fill="currentColor"></path></svg>
</div>
<div class="content">
<div class="item">
<div class="desc">Classes and IDs</div><div class="text classes" dir="auto">.actions #a</div>
</div>
<div class="title">Children</div>
<div class="actions">
<div class="action">
<div class="desc">.action .actions--first .act .acti .act</div>
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" class="icon"><path d="M0 0h24v24H0z" fill="none"></path><path d="M19 12h-2v3h-3v2h5v-5zM7 9h3V7H5v5h2V9zm14-6H3c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16.01H3V4.99h18v14.02z" fill="currentColor"></path></svg>
</div>
</div>
</div>
</div>
`;
var scepterOutlyingCSS = `
body, html {
user-select: none;
-webkit-user-select: none;
-webkit-touch-callout: none;
-webkit-tap-highlight-color: transparent;
}
body .seElected {
border-radius: 1px !important;
box-shadow: inset 0 0 0 500em rgb(104 187 228 / 12%), 0 0 0 10px rgb(104 187 228 / 12%) !important;
transition: 0.25s cubic-bezier(0.18, 0.89, 0.32, 1.2) !important;
transition-property: box-shadow, border-radius !important;
}
`;
function pushUrl() {
var url = new URL(window.location.href),
requestedURL = url.searchParams.get('url');
if (requestedURL) {
renderFrame(requestedURL);
} else {
renderFrame('https://berryscript.com');
}
}
// rerender iframe when pressed "back" button in browser
window.addEventListener('popstate', pushUrl);
// render iframe
pushUrl();