|
23 | 23 | */
|
24 | 24 |
|
25 | 25 | {
|
26 |
| - let thisPath = ""; |
27 |
| - for (let i of document.getElementsByTagName("script")) |
28 |
| - if (i.src.includes("NightTime")) |
29 |
| - thisPath = i.getAttribute("src"); |
| 26 | + let thisPath = document.getElementsByTagName("script"); |
| 27 | + thisPath = thisPath[thisPath.length - 1].getAttribute("src"); |
30 | 28 | thisPath = thisPath.slice(0, thisPath.lastIndexOf("/") + 1);
|
31 | 29 | let path = (href = "") => thisPath + href;
|
32 |
| - let type = obj => { |
33 |
| - return Object.prototype.toString.call(obj).slice(8, -1); |
34 |
| - } |
35 |
| - let profile = new Object; |
36 |
| - if (type(window["NightTime"]) === "Object") |
| 30 | + let type = obj => (obj ? obj.constructor.name : Object.prototype.toString.call(obj).slice(8, -1)); |
| 31 | + let profile = {}; |
| 32 | + if (type(window.NightTime) === "Object") |
37 | 33 | Object.assign(profile, NightTime);
|
38 | 34 |
|
39 |
| - var NightTime = new Object; |
| 35 | + var NightTime = {}; |
40 | 36 |
|
41 |
| - let _this = NightTime; |
| 37 | + let _ = NightTime; |
42 | 38 | let hour = new Date().getHours();
|
43 |
| - if (hour < 7 || hour >= 19) |
44 |
| - _this.darkModeFact = true; //晚上7点到次日早上7点启动深色主题 |
45 |
| - if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) |
46 |
| - _this.darkModeFact = true; //若浏览器开启了深色主题,也会启动深色主题 |
47 |
| - _this.theme = ""; |
48 |
| - _this.nightDo = _this.lightDo = () => {}; |
| 39 | + if (hour < 7 || hour >= 19 || //晚上7点到次日早上7点启动深色主题 |
| 40 | + matchMedia && matchMedia("(prefers-color-scheme: dark)").matches) //若浏览器开启了深色主题,也会启动深色主题 |
| 41 | + _.darkModeFact = true; |
| 42 | + _.theme = ""; |
| 43 | + _.nightDo = _.lightDo = () => { }; |
49 | 44 | /** method description
|
50 | 45 | * 切换到当前主题
|
51 | 46 | * @returns void - 切换到正常情况下配置而应该呈现的主题
|
52 | 47 | */
|
53 |
| - _this.CurrentModeStyle = () => { |
54 |
| - _this.css = document.getElementById("css"); |
55 |
| - if (_this.css) _this.theme = _this.css.getAttribute("data-theme").toLowerCase(); |
56 |
| - if (_this.darkModeFact) return _this.DarkModeStyle(); |
57 |
| - else return _this.LightModeStyle(); |
| 48 | + _.CurrentModeStyle = () => { |
| 49 | + _.css = document.getElementById("css"); |
| 50 | + if (_.css) _.theme = _.css.getAttribute("data-theme").toLowerCase(); |
| 51 | + if (_.darkModeFact) return _.DarkModeStyle(); |
| 52 | + else return _.LightModeStyle(); |
58 | 53 | }
|
59 | 54 | /** method description
|
60 | 55 | * 切换到对立主题
|
61 | 56 | * @returns void - 切换到不是当前状态下的主题,即浅色深色模式互相切换
|
62 | 57 | */
|
63 |
| - _this.ToggleModeStyle = () => { |
64 |
| - _this.darkModeFact = !_this.darkModeFact; |
65 |
| - return _this.CurrentModeStyle(); |
| 58 | + _.ToggleModeStyle = () => { |
| 59 | + _.darkModeFact = !_.darkModeFact; |
| 60 | + return _.CurrentModeStyle(); |
66 | 61 | }
|
67 |
| - _this.LightBackgroundColor = "white"; |
68 |
| - _this.DarkBackgroundColor = "#222"; |
69 |
| - _this.LightCSS = _this.DarkCSS = undefined; |
70 |
| - _this.defineCustomBackgroundColor = false; |
| 62 | + _.LightBackgroundColor = "white"; |
| 63 | + _.DarkBackgroundColor = "#222"; |
| 64 | + _.LightCSS = _.DarkCSS = undefined; |
| 65 | + _.defineCustomBackgroundColor = false; |
71 | 66 | String.prototype.i = function (index, character = "") {
|
72 | 67 | return this.slice(0, index) + character + this.slice(index + 1);
|
73 | 68 | }
|
|
79 | 74 | }
|
80 | 75 | return hyphenCase;
|
81 | 76 | }
|
82 |
| - _this.scrollbar = () => { |
83 |
| - _this.appendStyleByAjax(`css/scrollbar.css`, "scrollbar", { |
84 |
| - "var(--text-color-parts)": (_this.darkModeFact ? "255, 255, 255" : "0, 0, 0") |
| 77 | + _.scrollbar = () => { |
| 78 | + _.appendStyleByAjax(`css/scrollbar.css`, "scrollbar", { |
| 79 | + "var(--text-color-parts)": (_.darkModeFact ? "255, 255, 255" : "0, 0, 0") |
85 | 80 | });
|
86 | 81 | }
|
87 | 82 | const notAvailableLog = new Error("The ID you specified has been occupied by the other element, please try to use other ID!");
|
|
92 | 87 | * @returns true - 添加成功
|
93 | 88 | * @exception false - 添加失败,输入的 id 名称被占用且不为 style 标签
|
94 | 89 | */
|
95 |
| - _this.appendStyle = (style, id = "") => { |
| 90 | + _.appendStyle = (style, id = "") => { |
96 | 91 | var css;
|
97 | 92 | if (type(style) == "Object") {
|
98 | 93 | css = JSON.stringify(style, undefined, "\t");
|
|
119 | 114 | * @returns true - 添加成功
|
120 | 115 | * @exception false - 添加失败,输入的 id 名称被占用且不为 link 标签
|
121 | 116 | */
|
122 |
| - _this.appendCSSLink = (href, id = "") => { |
| 117 | + _.appendCSSLink = (href, id = "") => { |
123 | 118 | var linkTag = document.getElementById(id);
|
124 | 119 | if (!linkTag) {
|
125 | 120 | linkTag = document.createElement("link");
|
|
143 | 138 | * @returns true - 添加成功
|
144 | 139 | * @exception false - 添加失败,输入的 id 名称被占用且不为 link 标签
|
145 | 140 | */
|
146 |
| - _this.appendStyleByAjax = (href, id = "", variable) => { |
| 141 | + _.appendStyleByAjax = (href, id = "", variable) => { |
147 | 142 | var css = new XMLHttpRequest();
|
148 | 143 | css.onreadystatechange = () => {
|
149 | 144 | if (css.readyState == 4 && css.status == 200) {
|
|
178 | 173 | * @returns true - 移除成功
|
179 | 174 | * @exception false - 移除失败,移除的不是 style 或 link 标签
|
180 | 175 | */
|
181 |
| - _this.removeStyle = _this.removeCSSLink = id => { |
| 176 | + _.removeStyle = _.removeCSSLink = id => { |
182 | 177 | let el = document.getElementById(id);
|
183 | 178 | if (!el) return false;
|
184 | 179 | if (el.tagName != "STYLE" && el.tagName != "LINK") return false;
|
|
189 | 184 | * 强制切换到深色主题
|
190 | 185 | * @returns void
|
191 | 186 | */
|
192 |
| - _this.DarkModeStyle = () => { |
193 |
| - _this.darkModeFact = true; |
194 |
| - if (_this.theme.includes("bootstrap")) { |
195 |
| - _this.appendCSSLink("https://bootswatch.com/4/darkly/bootstrap.min.css", "css"); |
| 187 | + _.DarkModeStyle = () => { |
| 188 | + _.darkModeFact = true; |
| 189 | + if (_.theme.includes("bootstrap")) { |
| 190 | + _.appendCSSLink("https://bootswatch.com/4/darkly/bootstrap.min.css", "css"); |
196 | 191 | for (let i of document.getElementsByClassName("navbar")) {
|
197 | 192 | i.classList.remove("navbar-light");
|
198 | 193 | i.classList.remove("bg-light");
|
199 | 194 | i.classList.add("navbar-dark");
|
200 | 195 | i.classList.add("bg-dark");
|
201 | 196 | }
|
202 | 197 | const bbolsf = "bootstrap-btn-outline-light-style-fix";
|
203 |
| - _this.appendStyleByAjax(`css/${bbolsf}.css`, bbolsf); |
| 198 | + _.appendStyleByAjax(`css/${bbolsf}.css`, bbolsf); |
204 | 199 | }
|
205 |
| - _this.scrollbar(); |
206 |
| - _this.removeStyle("light-root"); |
207 |
| - _this.appendStyle({ |
| 200 | + _.scrollbar(); |
| 201 | + _.removeStyle("light-root"); |
| 202 | + _.appendStyle({ |
208 | 203 | ":root": {
|
209 | 204 | "--text-color": "white",
|
210 |
| - "--background-color": _this.DarkBackgroundColor |
| 205 | + "--background-color": _.DarkBackgroundColor |
211 | 206 | }
|
212 | 207 | }, "dark-root");
|
213 |
| - _this.nightDo(); |
214 |
| - if (_this.defineCustomBackgroundColor) document.body.style.backgroundColor = _this.DarkBackgroundColor; |
215 |
| - if (_this.DarkCSS) _this.appendCSSLink(_this.DarkCSS, "css"); |
| 208 | + _.nightDo(); |
| 209 | + if (_.defineCustomBackgroundColor) document.body.style.backgroundColor = _.DarkBackgroundColor; |
| 210 | + if (_.DarkCSS) _.appendCSSLink(_.DarkCSS, "css"); |
216 | 211 | return true;
|
217 | 212 | }
|
218 | 213 | /** method description
|
219 | 214 | * 强制切换到浅色主题
|
220 | 215 | * @returns void
|
221 | 216 | */
|
222 |
| - _this.LightModeStyle = () => { |
223 |
| - _this.darkModeFact = false; |
224 |
| - if (_this.theme.includes("bootstrap")) { |
225 |
| - _this.appendCSSLink("https://bootswatch.com/_vendor/bootstrap/dist/css/bootstrap.min.css", "css"); |
| 217 | + _.LightModeStyle = () => { |
| 218 | + _.darkModeFact = false; |
| 219 | + if (_.theme.includes("bootstrap")) { |
| 220 | + _.appendCSSLink("https://bootswatch.com/_vendor/bootstrap/dist/css/bootstrap.min.css", "css"); |
226 | 221 | for (let i of document.getElementsByClassName("navbar")) {
|
227 | 222 | i.classList.remove("navbar-dark");
|
228 | 223 | i.classList.remove("bg-dark");
|
229 | 224 | i.classList.add("navbar-light");
|
230 | 225 | i.classList.add("bg-light");
|
231 | 226 | }
|
232 | 227 | }
|
233 |
| - _this.scrollbar(); |
234 |
| - _this.removeStyle("bootstrap-btn-outline-light-style-fix"); |
235 |
| - _this.removeStyle("dark-root"); |
236 |
| - _this.appendStyle({ |
| 228 | + _.scrollbar(); |
| 229 | + _.removeStyle("bootstrap-btn-outline-light-style-fix"); |
| 230 | + _.removeStyle("dark-root"); |
| 231 | + _.appendStyle({ |
237 | 232 | ":root": {
|
238 | 233 | "--text-color": "black",
|
239 |
| - "--background-color": _this.LightBackgroundColor |
| 234 | + "--background-color": _.LightBackgroundColor |
240 | 235 | }
|
241 | 236 | }, "light-root");
|
242 |
| - _this.lightDo(); |
243 |
| - if (_this.defineCustomBackgroundColor) document.body.style.backgroundColor = _this.LightBackgroundColor; |
244 |
| - if (_this.LightCSS) _this.appendCSSLink(_this.LightCSS, "css"); |
| 237 | + _.lightDo(); |
| 238 | + if (_.defineCustomBackgroundColor) document.body.style.backgroundColor = _.LightBackgroundColor; |
| 239 | + if (_.LightCSS) _.appendCSSLink(_.LightCSS, "css"); |
245 | 240 | return true;
|
246 | 241 | }
|
247 |
| - if (type(profile.darkMode) === "Boolean") _this.darkModeFact = Boolean(profile.darkMode); |
248 |
| - if (profile.nightDo && type(profile.nightDo) === "Function") _this.nightDo = profile.nightDo; |
249 |
| - if (profile.darkDo && type(profile.darkDo) === "Function") _this.nightDo = profile.darkDo; |
250 |
| - if (profile.lightDo && type(profile.lightDo) === "Function") _this.lightDo = profile.lightDo; |
| 242 | + if (type(profile.darkMode) === "Boolean") _.darkModeFact = Boolean(profile.darkMode); |
| 243 | + if (type(profile.nightDo) === "Function") _.nightDo = profile.nightDo; |
| 244 | + if (type(profile.darkDo) === "Function") _.nightDo = profile.darkDo; |
| 245 | + if (type(profile.lightDo) === "Function") _.lightDo = profile.lightDo; |
251 | 246 | if (profile.lightBackgroundColor || profile.darkBackgroundColor) {
|
252 |
| - if (profile.lightBackgroundColor) _this.LightBackgroundColor = profile.lightBackgroundColor; |
253 |
| - if (profile.darkBackgroundColor) _this.DarkBackgroundColor = profile.darkBackgroundColor; |
254 |
| - _this.defineCustomBackgroundColor = true; |
| 247 | + if (profile.lightBackgroundColor) _.LightBackgroundColor = profile.lightBackgroundColor; |
| 248 | + if (profile.darkBackgroundColor) _.DarkBackgroundColor = profile.darkBackgroundColor; |
| 249 | + _.defineCustomBackgroundColor = true; |
255 | 250 | }
|
256 |
| - if (profile.lightCSS) _this.LightCSS = profile.lightCSS; |
257 |
| - if (profile.darkCSS) _this.DarkCSS = profile.darkCSS; |
258 |
| - _this.CurrentModeStyle(); |
| 251 | + if (profile.lightCSS) _.LightCSS = profile.lightCSS; |
| 252 | + if (profile.darkCSS) _.DarkCSS = profile.darkCSS; |
| 253 | + _.CurrentModeStyle(); |
259 | 254 | }
|
0 commit comments