-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathutils.ts
More file actions
278 lines (256 loc) · 6.77 KB
/
Copy pathutils.ts
File metadata and controls
278 lines (256 loc) · 6.77 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import Logger from "@App/app/logger/logger";
import { Metadata, Script } from "@App/app/repo/scripts";
import { CronTime } from "cron";
import crypto from "crypto-js";
import dayjs from "dayjs";
import semver from "semver";
export function nextTime(crontab: string): string {
let oncePos = 0;
if (crontab.indexOf("once") !== -1) {
const vals = crontab.split(" ");
vals.forEach((val, index) => {
if (val === "once") {
oncePos = index;
}
});
if (vals.length === 5) {
oncePos += 1;
}
}
let cron: CronTime;
try {
cron = new CronTime(crontab.replace(/once/g, "*"));
} catch {
throw new Error("错误的定时表达式");
}
if (oncePos) {
switch (oncePos) {
case 1: // 每分钟
return cron.sendAt().toFormat("yyyy-MM-dd HH:mm 每分钟运行一次");
case 2: // 每小时
return cron.sendAt().plus({ hour: 1 }).toFormat("yyyy-MM-dd HH 每小时运行一次");
case 3: // 每天
return cron.sendAt().plus({ day: 1 }).toFormat("yyyy-MM-dd 每天运行一次");
case 4: // 每月
return cron.sendAt().plus({ month: 1 }).toFormat("yyyy-MM 每月运行一次");
case 5: // 每星期
return cron.sendAt().plus({ week: 1 }).toFormat("yyyy-MM-dd 每星期运行一次");
}
throw new Error("错误表达式");
}
return cron.sendAt().toFormat("yyyy-MM-dd HH:mm:ss");
}
export function formatTime(time: Date) {
return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
}
export function formatUnixTime(time: number) {
return dayjs.unix(time).format("YYYY-MM-DD HH:mm:ss");
}
export function semTime(time: Date) {
return dayjs().to(dayjs(time));
}
export function randomString(e: number) {
e = e || 32;
const t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz";
const a = t.length;
let n = "";
for (let i = 0; i < e; i += 1) {
n += t.charAt(Math.floor(Math.random() * a));
}
return n;
}
export function dealSymbol(source: string): string {
source = source.replace(/("|\\)/g, "\\$1");
source = source.replace(/(\r\n|\n)/g, "\\n");
return source;
}
export function dealScript(source: string): string {
return dealSymbol(source);
}
export function isFirefox() {
if (navigator.userAgent.indexOf("Firefox") >= 0) {
return true;
}
return false;
}
export function InfoNotification(title: string, msg: string) {
chrome.notifications.create({
type: "basic",
title,
message: msg,
iconUrl: chrome.runtime.getURL("assets/logo.png"),
});
}
export function valueType(val: unknown) {
switch (typeof val) {
case "string":
case "number":
case "boolean":
case "object":
return typeof val;
default:
return "unknown";
}
}
export function toStorageValueStr(val: unknown): string {
switch (typeof val) {
case "string":
return `s${val}`;
case "number":
return `n${val.toString()}`;
case "boolean":
return `b${val ? "true" : "false"}`;
default:
try {
return `o${JSON.stringify(val)}`;
} catch {
return "";
}
}
}
export function parseStorageValue(str: string): unknown {
if (str === "") {
return undefined;
}
const t = str[0];
const s = str.substring(1);
switch (t) {
case "b":
return s === "true";
case "n":
return parseFloat(s);
case "o":
try {
return JSON.parse(s);
} catch {
return str;
}
case "s":
return s;
default:
return str;
}
}
// 对比版本大小
export function ltever(newVersion: string, oldVersion: string, logger?: Logger) {
// 先验证符不符合语义化版本规范
try {
return semver.lte(newVersion, oldVersion);
} catch (e) {
logger?.warn("does not conform to the Semantic Versioning specification", Logger.E(e));
}
const newVer = newVersion.split(".");
const oldVer = oldVersion.split(".");
for (let i = 0; i < newVer.length; i += 1) {
if (Number(newVer[i]) > Number(oldVer[i])) {
return false;
}
if (Number(newVer[i]) < Number(oldVer[i])) {
return true;
}
}
return true;
}
// 在当前页后打开一个新页面
export function openInCurrentTab(url: string) {
chrome.tabs.query(
{
active: true,
},
(tabs) => {
if (tabs.length) {
chrome.tabs.create({
url,
index: tabs[0].index + 1,
});
} else {
chrome.tabs.create({
url,
});
}
}
);
}
export function isDebug() {
return process.env.NODE_ENV === "development";
}
// 检查订阅规则是否改变,是否能够静默更新
export function checkSilenceUpdate(oldMeta: Metadata, newMeta: Metadata): boolean {
// 判断connect是否改变
const oldConnect: { [key: string]: boolean } = {};
const newConnect: { [key: string]: boolean } = {};
oldMeta.connect &&
oldMeta.connect.forEach((val) => {
oldConnect[val] = true;
});
newMeta.connect &&
newMeta.connect.forEach((val) => {
newConnect[val] = true;
});
// 老的里面没有新的就需要用户确认了
const keys = Object.keys(newConnect);
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
if (!oldConnect[key]) {
return false;
}
}
return true;
}
export function sleep(time: number) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
export function getStorageName(script: Script): string {
if (script.metadata && script.metadata.storagename) {
return script.metadata.storagename[0];
}
return script.uuid;
}
export function getIcon(script: Script): string | undefined {
return (
(script.metadata.icon && script.metadata.icon[0]) ||
(script.metadata.iconurl && script.metadata.iconurl[0]) ||
(script.metadata.defaulticon && script.metadata.defaulticon[0]) ||
(script.metadata.icon64 && script.metadata.icon64[0]) ||
(script.metadata.icon64url && script.metadata.icon64url[0])
);
}
export function calculateMd5(blob: Blob) {
return new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.readAsArrayBuffer(blob);
reader.onloadend = () => {
if (!reader.result) {
reject(new Error("result is null"));
} else {
const wordArray = crypto.lib.WordArray.create(<ArrayBuffer>reader.result);
resolve(crypto.MD5(wordArray).toString());
}
};
});
}
export function errorMsg(e: any): string {
if (typeof e === "string") {
return e;
}
if (e instanceof Error) {
return e.message;
}
if (typeof e === "object") {
return JSON.stringify(e);
}
return "";
}
export function isUserScriptsAvailable() {
try {
// Property access which throws if developer mode is not enabled.
chrome.userScripts;
// 兼容chrome的写法
return !!chrome.userScripts;
} catch {
// Not available.
return false;
}
}