forked from unbug/codelf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTools.js
More file actions
104 lines (93 loc) · 2.71 KB
/
Tools.js
File metadata and controls
104 lines (93 loc) · 2.71 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
let appCache = window.applicationCache;
appCache.addEventListener('updateready', function () {
if (appCache.status == appCache.UPDATEREADY) {
try {
appCache.update();
if (appCache.status == appCache.UPDATEREADY) {
try {
appCache.swapCache();
window.location.reload(false);
} catch (err) {
}
}
} catch (err) {
}
}
}, false);
const ua = navigator.userAgent;
const android = ua.match(/(Android);?[\s/]+([\d.]+)?/);
const ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
const ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
const iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);
let os = {};
if (android) os.android = true, os.version = android[2];
if (iphone && !ipod) os.ios = os.iphone = true, os.version = iphone[2].replace(/_/g, '.');
if (ipad) os.ios = os.ipad = true, os.version = ipad[2].replace(/_/g, '.');
if (ipod) os.ios = os.ipod = true, os.version = ipod[3] ? ipod[3].replace(/_/g, '.') : null;
const thisPage = window.location.href.replace(window.location.hash, '');
const thisPath = thisPage.substring(0, thisPage.lastIndexOf('/') + 1);
const randomColor = () => {
const letters = '0123456789ABCDEF'.split('');
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
const randomList = (list, len, verify, ratio) => {
let rs = [], _list = list.slice(0);
len = len || _list.length;
ratio = ratio ? ratio : 0;
function rd(_array) {
_array.sort(function () {
return (0.5 - Math.random());
});
}
while (ratio) {
rd(_list);
ratio--;
}
if (_list.length <= len) {
rs = _list;
} else {
while (rs.length < len) {
let index = Math.floor(Math.random() * _list.length),
item = _list[index];
if ((verify && verify.call(this, item, _list)) || !verify) {
rs.push(item);
_list.splice(index, 1);
}
}
}
return rs;
}
const InlineWebWorker = {
ready: window.Blob && window.Worker && window.URL,
create: function create(selector) {
return new Worker(window.URL.createObjectURL(new Blob([document.querySelector(selector).textContent])));
}
}
const uuid = len => {
let res = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
let num = Math.random() * 16 | 0, v = c === 'x' ? num : (num & 0x3 | 0x8);
return v.toString(16);
});
return len ? res.substr(0, len) : res;
}
const randomLabelColor = () => {
const colors = [
'red',
'orange',
'yellow',
'olive',
'green',
'teal',
'blue',
'violet',
'purple',
'pink',
'brown',
];
return randomList(colors, 1)[0];
};
export {os, thisPage, thisPath, randomColor, InlineWebWorker, uuid, randomLabelColor}