-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmkdocs.custom.js
More file actions
65 lines (53 loc) · 1.68 KB
/
mkdocs.custom.js
File metadata and controls
65 lines (53 loc) · 1.68 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
;(() => {
if (window.parent === window) return;
const isDev = location.hostname === 'localhost' || location.hostname === '127.0.0.1';
const parentOrigin = isDev ? 'http://localhost:4202' : 'https://taon.dev';
let hasSentReady = false;
const tellParentReady = () => {
if (hasSentReady) return;
hasSentReady = true;
window.parent.postMessage(
{ type: 'IFRAME_READY' },
parentOrigin
);
};
const tellParentPath = () => {
window.parent.postMessage(
{ type: 'IFRAME_PATH_UPDATE', path: location.pathname + location.search },
parentOrigin
);
};
// Send READY + path on first real load
const onFirstLoad = () => {
tellParentReady();
tellParentPath();
};
// Initial load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', onFirstLoad);
} else {
onFirstLoad();
}
// Subsequent SPA navigations
window.addEventListener('popstate', tellParentPath);
window.addEventListener('hashchange', tellParentPath);
['pushState', 'replaceState'].forEach(method => {
const orig = history[method];
history[method] = function (...args) {
orig.apply(this, args);
setTimeout(tellParentPath, 0);
};
});
// Listen for parent navigation
window.addEventListener('message', event => {
if (event.origin !== parentOrigin) return;
if (event.data?.type === 'NAVIGATE') {
const newPath = event.data.path || '/';
const current = location.pathname + location.search;
if (newPath !== current && newPath !== location.pathname) {
hasSentReady = false; // Reset so next page will send READY again
location.href = newPath;
}
}
});
})();