forked from xingguangcuican6666/ABK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
50 lines (44 loc) · 1.37 KB
/
Copy pathutils.js
File metadata and controls
50 lines (44 loc) · 1.37 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
/**
* 通用工具函数
*/
import { RUNTIME_CACHE_KEY, SUSFS_COMPAT_MIN } from './config.js';
// HTML 转义,防止 XSS
export function esc(str) {
var el = document.createElement('span');
el.textContent = str;
return el.innerHTML;
}
// 复制文本到剪贴板(兼容旧浏览器)
export function copyText(text) {
if (navigator.clipboard && navigator.clipboard.writeText) {
return navigator.clipboard.writeText(text);
}
var ta = document.createElement('textarea');
ta.value = text;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
return Promise.resolve();
}
// 给 URL 追加缓存破坏参数
export function withCacheKey(url) {
return url + (url.indexOf('?') === -1 ? '?' : '&') + 'v=' + encodeURIComponent(RUNTIME_CACHE_KEY);
}
// 强制无缓存请求 JSON
export async function fetchJsonFresh(url) {
var r = await fetch(withCacheKey(url), { cache: 'no-store' });
if (!r.ok) throw new Error('HTTP ' + r.status);
return r.json();
}
// 判断内核版本是否兼容 SUSFS
export function isSusfsCompat(kernelStr) {
var parts = kernelStr.split('.');
var major = parts[0] + '.' + parts[1];
var min = SUSFS_COMPAT_MIN[major];
if (min == null) return false;
var sublevel = parseInt(parts[2], 10);
return sublevel >= min;
}