Repository navigation
Expand file tree
/
Copy pathsite.js
More file actions
125 lines (117 loc) · 5.98 KB
/
Copy pathsite.js
File metadata and controls
125 lines (117 loc) · 5.98 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
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
// xmoj-script.uk 官网脚本:主题切换、浏览器识别、版本号
(function () {
const root = document.documentElement;
function ApplyTheme(theme) {
root.setAttribute("data-bs-theme", theme);
const toggle = document.getElementById("ThemeToggle");
if (toggle) {
toggle.textContent = theme === "dark" ? "亮色" : "暗色";
toggle.setAttribute("aria-label", theme === "dark" ? "切换到亮色模式" : "切换到暗色模式");
}
}
function DetectBrowser() {
const ua = navigator.userAgent;
if (/Firefox\//.test(ua)) return "firefox";
if (/Edg(e|A|iOS)?\//.test(ua)) return "edge";
if (/Chrome\/|Chromium\//.test(ua)) return "chrome";
if (/Safari\//.test(ua)) return "safari";
return "chrome";
}
function MajorVersion(browser) {
// Chromium Edge 的标识是 Edg/(以及 EdgA/、EdgiOS/);旧版 EdgeHTML 的 Edge/18 不参与版本判断
const pattern = browser === "edge" ? /Edg(?:A|iOS)?\/(\d+)/ : /Chrom(?:e|ium)\/(\d+)/;
const match = navigator.userAgent.match(pattern);
return match ? parseInt(match[1], 10) : 0;
}
function SelectBrowser(browser) {
document.querySelectorAll(".browser-tabs button").forEach((button) => {
button.setAttribute("aria-pressed", button.dataset.browser === browser ? "true" : "false");
});
document.querySelectorAll("[data-for]").forEach((element) => {
element.hidden = !element.dataset.for.split(" ").includes(browser);
});
}
document.addEventListener("DOMContentLoaded", () => {
ApplyTheme(root.getAttribute("data-bs-theme") || "light");
const toggle = document.getElementById("ThemeToggle");
if (toggle) {
toggle.addEventListener("click", () => {
const next = root.getAttribute("data-bs-theme") === "dark" ? "light" : "dark";
ApplyTheme(next);
try {
localStorage.setItem("theme", next);
} catch (e) {
}
});
}
const tabs = document.querySelector(".browser-tabs");
if (tabs) {
const browser = DetectBrowser();
const hint = document.getElementById("BrowserHint");
if (hint) {
const names = {chrome: "Chrome", edge: "Edge", firefox: "Firefox", safari: "Safari"};
hint.textContent = "检测到你正在使用 " + names[browser] + ",已自动选择对应步骤。";
}
// Chrome 和 Edge 都从 138 起改用「允许用户脚本」开关
const version = MajorVersion(browser);
if ((browser === "chrome" || browser === "edge") && version > 0) {
const name = browser === "edge" ? "Edge" : "Chrome";
const note = document.getElementById(name + "VersionNote");
if (note) {
note.textContent = "你的 " + name + " 版本是 " + version + "," +
(version >= 138 ? "请按「" + name + " 138 及以上」操作。" : "请按「" + name + " 137 及以下」操作。");
}
}
tabs.addEventListener("click", (event) => {
const button = event.target.closest("button[data-browser]");
if (button) SelectBrowser(button.dataset.browser);
});
SelectBrowser(browser);
}
// 手机上展开的菜单占据页面高度:先收起菜单,再跳到对应章节,标题才不会被导航栏挡住
const siteNav = document.getElementById("SiteNav");
if (siteNav && window.bootstrap) {
const PagePath = (path) => path.replace(/\/index\.html$/, "/");
// 收起动画期间 show 类已被移除,用 closing 记录状态;动画中再点别的链接,以最后一次为准
let closing = false;
let pendingLink = null;
siteNav.addEventListener("hide.bs.collapse", () => {
closing = true;
});
siteNav.addEventListener("hidden.bs.collapse", () => {
closing = false;
if (!pendingLink) return;
const target = document.getElementById(pendingLink.hash.slice(1));
history.pushState(null, "", pendingLink.hash);
pendingLink = null;
if (target) target.scrollIntoView();
});
siteNav.addEventListener("click", (event) => {
if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
const link = event.target.closest("a[href]");
if (!link || !link.hash || PagePath(link.pathname) !== PagePath(location.pathname)) return;
if (!document.getElementById(link.hash.slice(1))) return;
if (!siteNav.classList.contains("show") && !closing) return;
event.preventDefault();
pendingLink = link;
if (!closing) bootstrap.Collapse.getOrCreateInstance(siteNav).hide();
});
}
const versionElements = document.querySelectorAll("[data-latest-version]");
if (versionElements.length > 0) {
fetch("Update.json", {cache: "no-cache"})
.then((response) => response.json())
.then((data) => {
// 只显示正式版;全部是预发布版时才退回到最新一条
const versions = Object.keys(data.UpdateHistory);
const releases = versions.filter((version) => !data.UpdateHistory[version].Prerelease);
const latest = releases.length > 0 ? releases[releases.length - 1] : versions[versions.length - 1];
versionElements.forEach((element) => {
element.textContent = "v" + latest;
});
})
.catch(() => {
});
}
});
})();