-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathindex.ts
More file actions
389 lines (354 loc) · 11.3 KB
/
index.ts
File metadata and controls
389 lines (354 loc) · 11.3 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import intl from 'react-intl-universal';
import { LANG_MAP, LOG_END_SYMBOL } from './const';
import { CronExpressionParser } from 'cron-parser';
import { ICrontab } from '@/pages/crontab/type';
export default function browserType() {
// 权重:系统 + 系统版本 > 平台 > 内核 + 载体 + 内核版本 + 载体版本 > 外壳 + 外壳版本
const ua = navigator.userAgent.toLowerCase();
const testUa = (regexp: RegExp) => regexp.test(ua);
const testVs = (regexp: RegExp) =>
(ua.match(regexp) || [])
.toString()
.replace(/[^0-9|_.]/g, '')
.replace(/_/g, '.');
// 系统
let system = 'unknow';
if (testUa(/windows|win32|win64|wow32|wow64/g)) {
system = 'windows'; // windows系统
} else if (testUa(/macintosh|macintel/g)) {
system = 'macos'; // macos系统
} else if (testUa(/x11/g)) {
system = 'linux'; // linux系统
} else if (testUa(/android|adr/g)) {
system = 'android'; // android系统
} else if (testUa(/ios|iphone|ipad|ipod|iwatch/g)) {
system = 'ios'; // ios系统
}
// 系统版本
let systemVs = 'unknow';
if (system === 'windows') {
if (testUa(/windows nt 5.0|windows 2000/g)) {
systemVs = '2000';
} else if (testUa(/windows nt 5.1|windows xp/g)) {
systemVs = 'xp';
} else if (testUa(/windows nt 5.2|windows 2003/g)) {
systemVs = '2003';
} else if (testUa(/windows nt 6.0|windows vista/g)) {
systemVs = 'vista';
} else if (testUa(/windows nt 6.1|windows 7/g)) {
systemVs = '7';
} else if (testUa(/windows nt 6.2|windows 8/g)) {
systemVs = '8';
} else if (testUa(/windows nt 6.3|windows 8.1/g)) {
systemVs = '8.1';
} else if (testUa(/windows nt 10.0|windows 10/g)) {
systemVs = '10';
}
} else if (system === 'macos') {
systemVs = testVs(/os x [\d._]+/g);
} else if (system === 'android') {
systemVs = testVs(/android [\d._]+/g);
} else if (system === 'ios') {
systemVs = testVs(/os [\d._]+/g);
}
// 平台
let platform = 'unknow';
if (system === 'windows' || system === 'macos' || system === 'linux') {
platform = 'desktop'; // 桌面端
} else if (system === 'android' || system === 'ios' || testUa(/mobile/g)) {
platform = 'mobile'; // 移动端
}
// 内核和载体
let engine = 'unknow';
let supporter = 'unknow';
if (testUa(/applewebkit/g)) {
engine = 'webkit'; // webkit内核
if (testUa(/edge/g)) {
supporter = 'edge'; // edge浏览器
} else if (testUa(/opr/g)) {
supporter = 'opera'; // opera浏览器
} else if (testUa(/chrome/g)) {
supporter = 'chrome'; // chrome浏览器
} else if (testUa(/safari/g)) {
supporter = 'safari'; // safari浏览器
}
} else if (testUa(/gecko/g) && testUa(/firefox/g)) {
engine = 'gecko'; // gecko内核
supporter = 'firefox'; // firefox浏览器
} else if (testUa(/presto/g)) {
engine = 'presto'; // presto内核
supporter = 'opera'; // opera浏览器
} else if (testUa(/trident|compatible|msie/g)) {
engine = 'trident'; // trident内核
supporter = 'iexplore'; // iexplore浏览器
}
// 内核版本
let engineVs = 'unknow';
if (engine === 'webkit') {
engineVs = testVs(/applewebkit\/[\d._]+/g);
} else if (engine === 'gecko') {
engineVs = testVs(/gecko\/[\d._]+/g);
} else if (engine === 'presto') {
engineVs = testVs(/presto\/[\d._]+/g);
} else if (engine === 'trident') {
engineVs = testVs(/trident\/[\d._]+/g);
}
// 载体版本
let supporterVs = 'unknow';
if (supporter === 'chrome') {
supporterVs = testVs(/chrome\/[\d._]+/g);
} else if (supporter === 'safari') {
supporterVs = testVs(/version\/[\d._]+/g);
} else if (supporter === 'firefox') {
supporterVs = testVs(/firefox\/[\d._]+/g);
} else if (supporter === 'opera') {
supporterVs = testVs(/opr\/[\d._]+/g);
} else if (supporter === 'iexplore') {
supporterVs = testVs(/(msie [\d._]+)|(rv:[\d._]+)/g);
} else if (supporter === 'edge') {
supporterVs = testVs(/edge\/[\d._]+/g);
}
// 外壳和外壳版本
let shell = 'none';
let shellVs = 'unknow';
if (testUa(/micromessenger/g)) {
shell = 'wechat'; // 微信浏览器
shellVs = testVs(/micromessenger\/[\d._]+/g);
} else if (testUa(/qqbrowser/g)) {
shell = 'qq'; // QQ浏览器
shellVs = testVs(/qqbrowser\/[\d._]+/g);
} else if (testUa(/ucbrowser/g)) {
shell = 'uc'; // UC浏览器
shellVs = testVs(/ucbrowser\/[\d._]+/g);
} else if (testUa(/qihu 360se/g)) {
shell = '360'; // 360浏览器(无版本)
} else if (testUa(/2345explorer/g)) {
shell = '2345'; // 2345浏览器
shellVs = testVs(/2345explorer\/[\d._]+/g);
} else if (testUa(/metasr/g)) {
shell = 'sougou'; // 搜狗浏览器(无版本)
} else if (testUa(/lbbrowser/g)) {
shell = 'liebao'; // 猎豹浏览器(无版本)
} else if (testUa(/maxthon/g)) {
shell = 'maxthon'; // 遨游浏览器
shellVs = testVs(/maxthon\/[\d._]+/g);
}
const result = Object.assign(
{
engine, // webkit gecko presto trident
engineVs,
platform, // desktop mobile
supporter, // chrome safari firefox opera iexplore edge
supporterVs,
system, // windows macos linux android ios
systemVs,
},
shell === 'none'
? {}
: {
shell, // wechat qq uc 360 2345 sougou liebao maxthon
shellVs,
},
);
console.log(
"%c\n .d88b. d888888b d8b db d888b db .d88b. d8b db d888b \n.8P Y8. `88' 888o 88 88' Y8b 88 .8P Y8. 888o 88 88' Y8b \n88 88 88 88V8o 88 88 88 88 88 88V8o 88 88 \n88 88 88 88 V8o88 88 ooo 88 88 88 88 V8o88 88 ooo \n`8P d8' .88. 88 V888 88. ~8~ 88booo. `8b d8' 88 V888 88. ~8~ \n `Y88'Y8 Y888888P VP V8P Y888P Y88888P `Y88P' VP V8P Y888P \n \n \n",
'color: blue;font-size: 14px;',
);
console.log(
'%c忘形雨笠烟蓑,知心牧唱樵歌。明月清风共我,闲人三个,从他今古消磨。\n',
'color: yellow;font-size: 18px;',
);
console.log(
`%c青龙运行环境:\n\n系统:${result.system}/${result.systemVs}\n浏览器:${result.supporter}/${result.supporterVs}\n内核:${result.engine}/${result.engineVs}`,
'color: green;font-size: 14px;font-weight: bold;',
);
return result;
}
/**
* 获取第一个表格的可视化高度
* @param {*} extraHeight 额外的高度(表格底部的内容高度 Number类型,默认为74)
* @param {*} id 当前页面中有多个table时需要制定table的id
*/
export function getTableScroll({
extraHeight,
target,
}: { extraHeight?: number; target?: HTMLElement } = {}) {
if (typeof extraHeight === 'undefined') {
// 47 + 40 + 12
extraHeight = 99;
}
let tHeader = null;
if (target) {
tHeader = target;
} else {
tHeader = document.querySelector('.ant-table-wrapper');
}
//表格内容距离顶部的距离
let mainTop = 0;
if (tHeader) {
mainTop = tHeader.getBoundingClientRect().top;
}
//窗体高度-表格内容顶部的高度-表格内容底部的高度
let height = document.body.clientHeight - mainTop - extraHeight;
return height;
}
// 自动触发点击事件
function automaticClick(elment: HTMLElement) {
const ev = document.createEvent('MouseEvents');
ev.initMouseEvent(
'click',
true,
false,
window,
0,
0,
0,
0,
0,
false,
false,
false,
false,
0,
null,
);
elment.dispatchEvent(ev);
}
// 导出文件
export function exportJson(name: string, data: string) {
const urlObject = window.URL || window.webkitURL || window;
const export_blob = new Blob([data]);
const createA = document.createElementNS(
'http://www.w3.org/1999/xhtml',
'a',
) as any;
createA.href = urlObject.createObjectURL(export_blob);
createA.download = name;
automaticClick(createA);
}
export function depthFirstSearch<
T extends Record<string, any> & { children?: T[] },
>(children: T[], condition: (column: T) => boolean, item?: T) {
const c = [...children];
const keys = [];
(function find(cls: T[] | undefined) {
if (!cls) return;
for (let i = 0; i < cls?.length; i++) {
if (condition(cls[i])) {
if (!item) {
cls.splice(i, 1);
return;
}
if (cls[i].children) {
cls[i].children!.unshift(item);
} else {
cls[i].children = [item];
}
return;
}
if (cls[i].children) {
keys.push(cls[i].key);
find(cls[i].children);
}
}
})(c);
return c;
}
export function findNode<T extends Record<string, any> & { children?: T[] }>(
children: T[],
condition: (column: T) => boolean,
) {
const c = [...children];
let item;
function find(cls: T[] | undefined): T | undefined {
if (!cls) return;
for (let i = 0; i < cls?.length; i++) {
if (condition(cls[i])) {
item = cls[i];
} else if (cls[i].children) {
find(cls[i].children);
}
}
}
find(c);
return item as T | undefined;
}
export function logEnded(log: string): boolean {
const endTips = [LOG_END_SYMBOL, intl.get('执行结束')];
return endTips.some((x) => log.includes(x));
}
export function getCommandScript(
command: string,
): [string, string] | string | undefined {
const cmd = command.split(' ') as string[];
if (cmd[1] === 'repo' || cmd[1] === 'raw') {
return cmd[2];
}
let scriptsPart = cmd.find((x) =>
['.js', '.ts', '.sh', '.py'].some((y) => x.endsWith(y)),
);
if (!scriptsPart) return;
const scriptDir = `${window.__ENV__QL_DIR}/data/scripts`;
if (scriptsPart.startsWith(scriptDir)) {
scriptsPart = scriptsPart.replace(scriptDir, '');
}
let p: string, s: string;
let index = scriptsPart.lastIndexOf('/');
if (index >= 0) {
s = scriptsPart.slice(index + 1);
p = scriptsPart.slice(0, index);
} else {
s = scriptsPart;
p = '';
}
return [s, p];
}
export function parseCrontab(schedule: string): Date | null {
try {
const time = CronExpressionParser.parse(schedule);
if (time) {
return time.next().toDate();
}
} catch (error) { }
return null;
}
export function getCrontabsNextDate(
schedule: string,
extra_schedules: ICrontab['extra_schedules'],
): Date | null {
let date = parseCrontab(schedule);
if (extra_schedules?.length) {
extra_schedules.forEach((x) => {
const _date = parseCrontab(x.schedule);
if (_date && (!date || _date < date)) {
date = _date;
}
});
}
return date;
}
export function getExtension(filename: string) {
if (!filename) return '';
const arr = filename.split('.');
return `.${arr[arr.length - 1]}`;
}
export function getEditorMode(filename: string) {
const extension = getExtension(filename) as keyof typeof LANG_MAP;
return LANG_MAP[extension];
}
export function disableBody() {
const overlay = document.createElement('div');
overlay.style.position = 'fixed';
overlay.style.top = '0px';
overlay.style.left = '0px';
overlay.style.width = '100%';
overlay.style.height = '100%';
overlay.style.backgroundColor = 'transparent';
overlay.style.zIndex = '9999';
document.body.appendChild(overlay);
overlay.addEventListener('click', function (event) {
event.stopPropagation();
event.preventDefault();
});
document.body.style.overflow = 'hidden';
}