-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonUtils.user.js
273 lines (248 loc) · 8.1 KB
/
CommonUtils.user.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
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
// ==UserScript==
// @id zunsthy-common-utils
// @name Common Utils
// @category utils
// @version 0.0.7
// @updateURL https://raw.githubusercontent.com/zunsthy/userscripts/master/CommonUtils.meta.js
// @downloadURL https://raw.githubusercontent.com/zunsthy/userscripts/master/CommonUtils.user.js
// @author ZunSThy <[email protected]>
// @description some common methods
// @namespace https://github.com/zunsthy
// @include http://*
// @include https://*
// @match http://*/*
// @match https://*/*
// @grant none
// ==/UserScript==
(() => {
const utils = window._utils = {};
utils.random = n => Math.floor(Math.random() * n);
const orandom = n => Math.random() * 2 - 1;
utils.orandom = orandom;
utils.normalDistrubution = (n) => {
const m = n + n % 2;
const arr = new Float64Array(m);
for (let i = 0; i < m; i += 2) {
let x, y, r;
do {
x = orandom();
y = orandom();
r = x * x + y * y;
} while (r >= 1 || r === 0);
const f = Math.sqrt(-2 * Math.log(r) / r);
arr[i] = x * f;
arr[i + 1] = y * f;
}
return arr;
};
const noop = () => {};
utils.noop = noop;
utils.seq = n => Array.apply(null, { length: n }).map(Function.call, Number);
const flatten = arr => (Array.isArray(arr) ? Array.prototype.concat([], arr.map(flatten)) : arr);
const comparator = (a, b) => {
if (typeof a === 'number' && typeof b === 'number') {
return a - b;
} else {
return String.prototype.localeCompare.call(`${a}`, `${b}`);
}
};
const max = (arr, cmp = comparator) => arr.reduce((m, a) => (cmp(m, a) > 0 ? m : a), arr[0]);
utils.max = max;
const min = (arr, cmp = comparator) => arr.reduce((m, a) => (cmp(m, a) < 0 ? m : a), arr[0]);
utils.min = min;
utils.blobToText = (blob, cb) => {
const reader = new FileReader();
reader.addEventListener('loadend', (ev) => {
cb(ev.srcElement.result);
});
reader.readAsText(blob);
};
utils.blobToText2 = (blob, cb) => {
const reader = new FileReader();
reader.addEventListener('loadend', (ev) => {
const result = ev.srcElement.result;
cb(new TextDecoder('utf-8').decode(result));
});
reader.readAsArrayBuffer(blob);
};
utils.getCookies = () => {
const cookies = Object.create(null);
if (!document.cookie) return cookies;
document.cookie.split('; ').forEach((str) => {
const sp = str.indexOf('=');
const name = decodeURIComponent(str.slice(0, sp));
const value = decodeURIComponent(str.slice(sp + 1));
cookies[name] = value;
});
return cookies;
};
const getParsedUrl = (url) => new URL(url || location.href);
utils.getParsedUrl = getParsedUrl;
utils.getQuery = (url) => getParsedUrl(url).searchParams();
utils.histogram = (arr, min, max, step) => {
const n = Math.ceil((max - min) / step);
const out = new Int32Array(n);
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= min && arr[i] <= max) {
out[Math.floor((arr[i] - min) / step)]++;
}
}
return out;
};
utils.printHistogram = (his, width, print) => {
const m = max(his);
const chart = new Int32Array(his.length);
for (let i = 0; i < his.length; i++) {
chart[i] = his[i] / max * width;
if (print) print(chart[i]);
}
return chart;
};
utils.serialExecute = list => (finnal = noop) =>
list.reverse().reduce((next, last) => (err, data) => err ? next(data) : last(data, next), finnal);
const walker = function *(type, root) {
const walker = document.createTreeWalker(
root || document.body,
type || NodeFilter.SHOW_TEXT,
null,
false);
while (walker.nextNode()) yield walker.currentNode;
};
utils.walker = walker;
utils.getAllElements = () => walker(NodeFilter.SHOW_ELEMENT);
utils.nodeIterator = function *(type, root) {
const result = document.createNodeIterator(
root || document.body,
type || NodeFilter.SHOW_TEXT);
while (result.nextNode()) yield result.referenceNode;
};
utils.xpathIterator = function *(path, root) {
const result = document.evaluate(
path || '//*/text()',
root || document,
null,
XPathResult.ORDERED_NODE_ITERATOR_TYPE,
null);
let node = null;
while (node = result.iterateNext()) yield node;
};
utils.nodeByType = function *(type) {
const list = document.querySelectorAll('body, body *');
const ntype = type || Node.TEXT_NODE;
const nodeList = [];
for (let i = 0; i < list.length; i++) {
const children = list[i].childNodes;
if (!children || !children.length) continue;
for (let j = 0; j < children.length; j++) {
if (children[j].nodeType === ntype) {
yield children[j];
}
}
}
return nodeList;
};
const pad = (str, bit = 2, padding = '0') => padding.repeat(bit).concat(str).slice(-bit);
utils.pad = pad;
utils.copy = (text) => {
const ta = document.createElement('textarea');
ta.value = text;
document.body.appendChild(ta);
ta.focus();
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
};
const formatDate = (date = new Date()) => `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
utils.formatDate = formatDate;
const formatTime = (date = new Date()) => `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
utils.formatTime = formatTime;
utils.formatDateTime = (date = new Date()) => formatDate(date) + ' ' + formatTime(date);
const tBase64StringToUint8Array = (b64str) => {
const b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const arr = [];
const buf = [];
for (let i = 0; i < b64str.length; i++) {
const c = b64str.charAt(i);
const idx = b.indexOf(c);
if (idx === -1) continue;
arr.push(idx);
}
for (let i = 0; i < arr.length; i+=4) {
const n1 = arr[i];
const n2 = arr[i+1];
const n3 = arr[i+2] || 0;
const n4 = arr[i+3] || 0;
// aaaaaabb bbbbcccc ccdddddd
const c1 = ((n1 & 0x3f) << 2) | ((n2 >> 4) & 0x03);
const c2 = ((n2 & 0x0f) << 4) | ((n3 >> 2) & 0x0f);
const c3 = ((n3 & 0x03) << 6) | ((n4 ) & 0x3f);
buf.push(c1);
if (n3 !== 64) buf.push(c2);
if (n4 !== 64) buf.push(c3);
}
return new Uint8Array(buf);
};
const tUint8ArrayToString = (buf) => {
const arr = [];
let c, h, c2, c3;
for (let i = 0; i < buf.length; i++) {
c = buf[i];
h = c >> 4; // hhhh llll
switch (h) {
case 0x0:
case 0x1:
case 0x2:
case 0x3:
case 0x4:
case 0x5:
case 0x6:
case 0x7: // 0xxx
// 0xxxxxxx
// ---->
// 0xxxxxxx
arr.push(String.fromCharCode(c));
break;
case 0x8: // 1000
case 0x9: // 1001
case 0xa: // 1010
case 0xb: // 1011
console.error(`ignore char code at ${i}: ${c}`);
break;
case 0xc: // 1100
case 0xd: // 1101
// 110xxxxx 11yyyyyy
// ---->
// 00000xxx xxyyyyyy
c2 = buf[++i];
arr.push(String.fromCodePoint(
((c & 0x1f) << 6)
| ((c2 & 0x3f) )
));
break;
case 0xe: // 1110
// 1110xxxx 10yyyyyy 10zzzzzz
// ---->
// xxxxyyyy yyzzzzzz
c2 = buf[++i];
c3 = buf[++i];
arr.push(String.fromCodePoint(
((c & 0x0f) << 12)
| ((c2 & 0x3f) << 6)
| ((c3 & 0x3f) )
));
break;
case 0xf:
console.error(`ignore char code at ${i}: ${c}`);
}
}
return arr.join('');
};
const tUint8ArrayToUtf8String = (buf) => {
const decoder = new TextDecoder('utf-8');
return decoder.decode(buf);
};
utils.tBase64StringToUint8Array = tBase64StringToUint8Array;
utils.tUint8ArrayToString = tUint8ArrayToString;
utils.tUint8ArrayToUtf8String = tUint8ArrayToUtf8String;
utils.tBase64StringToUtf8String = str => tUint8ArrayToUtf8String(tBase64StringToUint8Array(str));
})();