-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (58 loc) · 2.09 KB
/
Copy pathscript.js
File metadata and controls
66 lines (58 loc) · 2.09 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
66
(function () {
const current = document.currentScript;
const currentUrl = current?.src || new URL("script.js", window.location.href).href;
const baseUrl = new URL(".", currentUrl);
const assetUrl = (name) => new URL(name, baseUrl).href;
function ensureV2Styles() {
if (document.querySelector('link[href$="portfolio-v2.css"]')) return;
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = assetUrl("portfolio-v2.css");
document.head.appendChild(link);
}
function hasScript(name) {
return Array.from(document.scripts).some((script) => {
try {
return new URL(script.src, window.location.href).pathname.endsWith(`/${name}`);
} catch (error) {
return false;
}
});
}
function parserBoot() {
const registryAlreadyDeclared = hasScript("portfolio-data.js");
ensureV2Styles();
if (registryAlreadyDeclared) {
document.write(`<script src="${assetUrl("legacy-script.js")}"><\/script>`);
return;
}
document.write(
`<script src="${assetUrl("portfolio-data.js")}"><\/script>` +
`<script src="${assetUrl("legacy-script.js")}"><\/script>` +
`<script src="${assetUrl("portfolio-v2.js")}"><\/script>`,
);
}
function loadScript(name) {
if (hasScript(name)) return Promise.resolve();
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = assetUrl(name);
script.async = false;
script.addEventListener("load", resolve, { once: true });
script.addEventListener("error", () => reject(new Error(`Failed to load ${name}`)), { once: true });
document.head.appendChild(script);
});
}
async function asyncBoot() {
ensureV2Styles();
try {
if (!window.KAAN_PORTFOLIO) await loadScript("portfolio-data.js");
await loadScript("legacy-script.js");
await loadScript("portfolio-v2.js");
} catch (error) {
console.error("Portfolio runtime boot failed", error);
}
}
if (document.readyState === "loading" && current) parserBoot();
else asyncBoot();
})();