-
Notifications
You must be signed in to change notification settings - Fork 30
/
Template.ejs
133 lines (123 loc) · 5.1 KB
/
Template.ejs
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="renderer" content="webkit" />
<title>CC98论坛</title>
<link type="image/x-icon" rel="icon" href="/static/98icon.ico" sizes="16x16 32x32 48x48 64x64 128x128 256x256" />
<link type="text/css" rel="stylesheet" href="/static/content/font-awesome/css/font-awesome.min.css" />
<link type="text/css" rel="stylesheet" href="/static/content/DPlayer.min.css" />
<link type="text/css" rel="stylesheet" href="/static/content/APlayer.min.css" />
<script type="text/javascript">
// 注册报错信息
window.onerror = async (event, source, line, col, error) => {
if (source.indexOf("vendors") !== -1) {
window.errorInfo = {
event,
source,
line,
col,
error,
};
// 获取模板内容
/** @type {HTMLTemplateElement} */
const errorTemplate = document.getElementById("error-template");
// 插入模板内容
/** @type {HTMLDivElement} */
const root = document.getElementById("root");
root.replaceWith(errorTemplate.content.cloneNode(true));
}
};
</script>
<script type="text/javascript">
// 控制用户缓存
// 需要清理缓存时更新 version
const version = "3.5.0";
if (localStorage.getItem("version") !== version) {
localStorage.clear();
localStorage.setItem("version", version);
}
</script>
</head>
<body style="display: flex; justify-content: center">
<!-- 错误处理模板 -->
<template id="error-template">
<%= errorTemplate %>
</template>
<!-- 不支持的浏览器模板 -->
<template id="unsupported-template">
<%= unsupportedTemplate %>
</template>
<div id="root"></div>
<script type="text/javascript" src="/static/scripts/lib/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/static/scripts/lib/moment/moment.js"></script>
<script type="text/javascript" src="/static/scripts/lib/spectrum/spectrum.js"></script>
<script type="text/javascript" src="/static/scripts/lib/mathjax-full/bundle/tex-mml-chtml.js"></script>
<% const jsFileNames=[]; htmlWebpackPlugin.files.js.forEach((jsFileName)=> jsFileName.indexOf("css") === -1 && jsFileNames.push(jsFileName.split("/").pop())); %>
<script type="text/javascript">
// 兼容性检查
if (!("flex" in document.documentElement.style)) {
// 获取模板内容
/** @type {HTMLTemplateElement} */
const unsupportedTemplate = document.getElementById("unsupported-template");
// 插入模板内容
/** @type {HTMLDivElement} */
const root = document.getElementById("root");
root.replaceWith(unsupportedTemplate.content.cloneNode(true));
}
// 运行主脚本
else {
const jsFileNames = <%= JSON.stringify(jsFileNames) %>;
jsFileNames.forEach((jsFileName) => {
const scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = `/static/scripts/${jsFileName}`;
document.body.appendChild(scriptElement);
});
}
</script>
<% const themeNames=["default_theme_placeholder"]; htmlWebpackPlugin.files.css.forEach((cssFile)=> themeNames.push(cssFile.split("/").pop())); %>
<script type="text/javascript">
// 动态载入样式
const themeNames = <%= JSON.stringify(themeNames) %>;
try {
const refreshTokenExpirationTime = localStorage.getItem(
"refresh_token_expirationTime"
);
const isLogOn =
refreshTokenExpirationTime &&
parseInt(refreshTokenExpirationTime, 10) * 1000 > Date.now();
if (!isLogOn) {
throw new Error("用户未登录");
}
let userSelectedThemeIndex = -1;
// 是否已经记录了上次更新的主题,如果有,则使用上次主题
const useTheme = localStorage.getItem("use-theme");
if (useTheme !== null) {
userSelectedThemeIndex = parseInt(localStorage.getItem("use-theme").slice(4));
}
// 如果未记录之前的主题主题,则从用户信息中提取主题
if (isNaN(userSelectedThemeIndex)) {
userSelectedThemeIndex = JSON.parse(
localStorage.getItem("userInfo").slice(4)
).theme;
}
const themeName = themeNames[userSelectedThemeIndex] || themeNames[themeNames.length - 1];
if (!themeName) {
throw new Error("用户未选择主题");
}
document.head.insertAdjacentHTML(
"beforeend",
`<link id="mainStylesheet" type="text/css" rel="stylesheet" href="/static/content/${themeName}">`
);
} catch (e) {
window.addEventListener("configload", ({ detail: config }) => {
document.head.insertAdjacentHTML(
"beforeend",
`<link id="mainStylesheet" type="text/css" rel="stylesheet" href="/static/content/${themeNames[config.defaultTheme]}">`
);
})
}
</script>
</body>
</html>