-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy path__client.js
74 lines (68 loc) · 1.58 KB
/
__client.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
// Hack into the alert that's used in some tutorials and send a message prior to the alert,
// else the parent thinks we lost contact and wrongfully reloads the page.
// The drawback is that alert is no longer blocking, but no tutorial relies on this.
const alert = window.alert;
window.alert = (message) => {
parent.postMessage(
{
type: 'ping-pause'
},
'*'
);
setTimeout(() => {
alert(message);
});
};
window.addEventListener('message', async (e) => {
// Belts and braces against malicious messages
if (e.data.type === 'goto' && e.data.path?.startsWith('/')) {
// SvelteKit's client.js will pick this up
const a = document.createElement('a');
a.href = e.data.path;
document.firstElementChild.append(a);
a.click();
a.remove();
}
});
history.pushState = function (state, title, url) {
// Don't create a new history entry for better back/forward navigation in the parent window
history.replaceState(state, title, url);
};
function ping() {
parent.postMessage(
{
type: 'ping',
data: {
path: location.pathname + location.search + location.hash
}
},
'*'
);
}
setInterval(ping, 100);
ping();
if (import.meta.hot) {
import.meta.hot.on('vite:beforeUpdate', (event) => {
parent.postMessage(
{
type: 'hmr',
data: event.updates
},
'*'
);
});
}
/**
* The iframe sometimes takes focus control in ways we can't prevent
* while the editor is focussed. Refocus the editor in these cases.
*/
window.addEventListener('focusin', (e) => {
if (e.target.tagName === 'BODY') {
parent.postMessage(
{
type: 'focus_on_editor'
},
'*'
);
}
});