-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.js
152 lines (147 loc) · 3.91 KB
/
theme.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
window.sofill = {};
window.sofill.cp = {};
window.sofill.funs = {};
var fs = null;
var path = null;
var isAppMode = document
.getElementsByTagName("body")[0]
.classList.contains("android")
? false
: document.getElementsByTagName("body")[0].classList.contains("client--browser")
? false
: window.siyuan.config.system.os == "windows" ||
window.siyuan.config.system.os == "darwin"
? true
: false;
if (isAppMode) {
fs = require("fs");
path = require("path");
console.log("isAppMode");
}
window.sofill.funs.loadStyle = function (href, id = null) {
let style = document.createElement("link");
if (id) style.id = id;
style.type = "text/css";
style.rel = "stylesheet";
style.href = href;
document.head.appendChild(style);
};
window.sofill.funs.updateStyle = function (id, href) {
let style = document.getElementById(id);
if (style) {
style.setAttribute("href", href);
} else {
window.sofill.funs.loadStyle(href, id);
}
};
window.sofill.funs.loadScript = function (
src,
type = "module",
async = false,
defer = false
) {
const script = document.createElement("script");
if (type) script.type = type;
if (async) script.async = true;
if (defer) script.defer = true;
script.src = src;
document.head.appendChild(script);
};
window.sofill.funs.addURLParam = function (
url,
param = {
v: window.siyuan.config.appearance.themeVer,
}
) {
let new_url;
switch (true) {
case url.startsWith("//"):
new_url = new URL(`https:${url}`);
break;
case url.startsWith("http://"):
case url.startsWith("https://"):
new_url = new URL(url);
break;
case url.startsWith("/"):
new_url = new URL(url, window.location.origin);
break;
default:
new_url = new URL(url, window.location.origin + window.location.pathname);
break;
}
for (let [key, value] of Object.entries(param)) {
new_url.searchParams.set(key, value);
}
switch (true) {
case url.startsWith("//"):
return new_url.href.substring(new_url.protocol.length);
case url.startsWith("http://"):
case url.startsWith("https://"):
return new_url.href;
case url.startsWith("/"):
return new_url.href.substring(new_url.origin.length);
default:
return new_url.href.substring(
(window.location.origin + window.location.pathname).length
);
}
};
var SelfProtection = localStorage.getItem(
"SC_winsay_cp_system__SelfProtection"
);
var P = [];
var bP = "";
var bP_lili = "";
path
? (bP = path.join(
window.siyuan.config.system.confDir,
"appearance",
"themes",
"Sofill-"
))
: null;
path
? (bP_lili = path.join(
window.siyuan.config.system.confDir,
"appearance",
"themes",
"Sofill="
))
: null;
fs
? fs.access(bP_lili, (e) => {
if (e) {
console.error(e);
alert(
`主题根目录未正确命名或不存在:【致命错误】${bP_lili} 文件夹不存在。修正错误后,需重启思源并重新选择主题。`
);
}
})
: null;
if (SelfProtection && SelfProtection === "true") {
try {
path ? P.push(path.join(bP, "script", "utils", "api.min.js")) : null;
path ? P.push(path.join(bP, "script", "CP.js")) : null;
path ? P.push(path.join(bP, "script", "config.js")) : null;
path ? P.push(path.join(bP, "style", "Init.min.css")) : null;
path ? P.push(path.join(bP, "style", "Block.css")) : null;
path ? P.push(path.join(bP_lili, "script", "kernel.js")) : null;
P.forEach((i) => {
fs
? fs.readFile(i, "utf-8", (e, data) => {
if (e) {
console.error(e);
alert(`主题自我保护检测到异常:【致命错误】${i} 文件不存在`);
}
})
: null;
});
} catch (e) {
console.error(e);
}
}
window.sofill.funs.loadScript(
window.sofill.funs.addURLParam("/appearance/themes/Sofill=/script/kernel.js"),
undefined,
true
);