forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmu-common.js
More file actions
385 lines (377 loc) 路 10.8 KB
/
Copy pathmu-common.js
File metadata and controls
385 lines (377 loc) 路 10.8 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
'use strict';
/**
* @typedef { object } userjs
*/
// if (typeof userjs === 'undefined') {
// let userjs = (self.userjs = {});
// }
if (typeof userjs === 'object' && userjs.isNull instanceof Function === false) {
const win = self ?? window;
const doc = win.document;
const isMobile = /Mobile|Tablet/.test(navigator.userAgent);
/**
* Object is typeof `Element`
* @template O
* @param { O } obj
* @returns { boolean }
*/
const isElem = (obj) => {
/** @type { string } */
const s = Object.prototype.toString.call(obj);
return s.includes('Element');
};
/**
* Object is typeof `Function`
* @template O
* @param { O } obj
* @returns { boolean }
*/
const isFN = (obj) => {
/** @type { string } */
const s = Object.prototype.toString.call(obj);
return s.includes('Function');
};
/**
* Object is typeof `object` / JSON Object
* @template O
* @param { O } obj
* @returns { boolean }
*/
const isObj = (obj) => {
/** @type { string } */
const s = Object.prototype.toString.call(obj);
return s.includes('Object');
};
/**
* Object is `null` or `undefined`
* @template O
* @param { O } obj
* @returns { boolean }
*/
const isNull = (obj) => {
return Object.is(obj, null) || Object.is(obj, undefined);
};
/**
* Object is Blank
* @template O
* @param { O } obj
* @returns { boolean }
*/
const isBlank = (obj) => {
return (
(typeof obj === 'string' && Object.is(obj.trim(), '')) ||
((obj instanceof Set || obj instanceof Map) && Object.is(obj.size, 0)) ||
(Array.isArray(obj) && Object.is(obj.length, 0)) ||
(isObj(obj) && Object.is(Object.keys(obj).length, 0))
);
};
/**
* Object is Empty
* @template O
* @param { O } obj
* @returns { boolean }
*/
const isEmpty = (obj) => {
return isNull(obj) || isBlank(obj);
};
/**
* @template T
* @param { T } target
* @param { boolean } toQuery
* @param { Element | Document | undefined } root
* @returns { T[] }
*/
const normalizeTarget = (target, toQuery = true, root) => {
if (Object.is(target, null) || Object.is(target, undefined)) {
return [];
}
if (Array.isArray(target)) {
return target;
}
if (typeof target === 'string') {
return toQuery ? Array.from((root || document).querySelectorAll(target)) : [target];
}
if (isElem(target)) {
return [target];
}
return Array.from(target);
};
/**
* Add Event Listener
* @template { HTMLElement } E
* @template { keyof HTMLElementEventMap } K
* @param { E } el
* @param { K } event
* @param { (this: E, ev: HTMLElementEventMap[K]) => any } callback
* @param { boolean | AddEventListenerOptions } options
*/
const ael = (el, event, callback, options = {}) => {
try {
for (const elem of normalizeTarget(el)) {
if (!elem) {
continue;
}
if (isMobile && event === 'click') {
elem.addEventListener('touchstart', callback);
return;
}
if (event === 'fclick') {
event = 'click';
}
elem.addEventListener(event, callback, options);
}
} catch (ex) {
console.error(ex);
}
};
/**
* Form Attributes of Element
* @template { keyof HTMLElementTagNameMap } K
* @param { K } elem
* @param { keyof HTMLElement } attr
*/
const formAttrs = (elem, attr = {}) => {
for (const key in attr) {
if (typeof attr[key] === 'object') {
formAttrs(elem[key], attr[key]);
} else if (isFN(attr[key])) {
if (key === 'container') {
key();
continue;
}
if (/^on/.test(key)) {
elem[key] = attr[key];
continue;
}
ael(elem, key, attr[key]);
} else if (key === 'class') {
elem.className = attr[key];
} else {
elem[key] = attr[key];
}
}
};
/**
* Make Element
* @template { keyof HTMLElementTagNameMap } K
* @param { K } tagName
* @param { string } cname
* @param { keyof HTMLElement } attrs
* @returns { HTMLElementTagNameMap[K] }
*/
const make = (tagName, cname, attrs = {}) => {
let el = null;
try {
el = document.createElement(tagName);
if (typeof cname === 'string' && !isEmpty(cname)) {
el.className = cname;
}
if (!isEmpty(attrs)) {
formAttrs(el, attrs);
}
} catch (ex) {
console.error(ex);
}
return el;
};
/**
* setTimeout w/ Promise
* @param { number } timeout - Timeout in milliseconds (ms)
* @returns { Promise<void> } Promise object
*/
const delay = (timeout = 5000) => new Promise((resolve) => setTimeout(resolve, timeout));
const Network = {
/**
* Fetch a URL with fetch API as fallback
*
* When GM is supported, makes a request like XMLHttpRequest, with some special capabilities, not restricted by same-origin policy
* @link https://violentmonkey.github.io/api/gm/#gm_xmlhttprequest
* @link https://developer.mozilla.org/docs/Web/API/Fetch_API
* @param { RequestInfo | URL } url - The URL to fetch
* @param { Request['method'] } method - Fetch method
* @param { 'buffer' | 'json' | 'text' | 'blob' | 'document' } responseType - Response type
* @param { RequestInit } data - Fetch parameters
* @returns { Promise<Response> } Fetch results
*/
async req(url, method = 'GET', responseType = 'json', data = {}) {
if (isEmpty(url)) {
throw new Error('"url" parameter is empty');
}
method = Network.bscStr(method, false);
responseType = Network.bscStr(responseType);
const params = {
method,
...data
};
return await new Promise((resolve, reject) => {
/**
* @param { Response } response
* @returns { Response | Document }
*/
const fetchResp = (response_1) => {
if (!response_1.ok) reject(response_1);
const check = (str_2 = 'text') => {
return isFN(response_1[str_2]) ? response_1[str_2]() : response_1;
};
if (responseType.match(/buffer/i)) {
resolve(check('arrayBuffer'));
} else if (responseType.match(/json/i)) {
resolve(check('json'));
} else if (responseType.match(/text/i)) {
resolve(check('text'));
} else if (responseType.match(/blob/i)) {
resolve(check('blob'));
} else if (responseType.match(/formdata/i)) {
resolve(check('formData'));
} else if (responseType.match(/clone/i)) {
resolve(check('clone'));
} else if (responseType.match(/document/i) && isFN(response_1.text)) {
const domParser = new DOMParser();
const respTxt = response_1.text();
if (respTxt instanceof Promise) {
respTxt.then((txt) => {
const doc = domParser.parseFromString(txt, 'text/html');
resolve(doc);
});
} else {
const doc = domParser.parseFromString(respTxt, 'text/html');
resolve(doc);
}
} else {
resolve(response_1);
}
};
fetch(url, params).then(fetchResp).catch(reject);
});
},
format(bytes, decimals = 2) {
if (Number.isNaN(bytes)) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${Network.sizes[i]}`;
},
sizes: ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
/**
* @template { string } S
* @param { S } str
* @param { boolean } lowerCase
* @returns { S }
*/
bscStr(str = '', lowerCase = true) {
const txt = str[lowerCase ? 'toLowerCase' : 'toUpperCase']();
return txt.replaceAll(/\W/g, '');
}
};
// const i18n$ = (...args) => {
// const arr = [];
// for (const arg of args) {
// arr.push(webext.i18n.getMessage(arg));
// }
// return arr.length !== 1 ? arr : arr[0];
// };
class Timeout {
constructor() {
this.ids = [];
}
set(delay, reason) {
return new Promise((resolve, reject) => {
const id = setTimeout(() => {
isNull(reason) ? resolve() : reject(reason);
this.clear(id);
}, delay);
this.ids.push(id);
});
}
clear(...ids) {
this.ids = this.ids.filter((id) => {
if (ids.includes(id)) {
clearTimeout(id);
return false;
}
return true;
});
}
}
class MUError extends Error {
/**
* @param {string} fnName - (Optional) Function name
* @param {...string} params - Extra error parameters
*/
constructor(fnName = 'muError', ...params) {
super(...params);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, MUError);
} else {
this.stack = new Error().stack;
}
this.fn = `[${fnName}]`;
this.name = this.constructor.name;
}
}
userjs.error = MUError;
userjs.isMobile = isMobile;
userjs.Timeout = Timeout;
userjs.isElem = isElem;
userjs.isFN = isFN;
userjs.isObj = isObj;
userjs.isNull = isNull;
userjs.isBlank = isBlank;
userjs.isEmpty = isEmpty;
userjs.ael = ael;
userjs.formAttrs = formAttrs;
userjs.make = make;
userjs.delay = delay;
userjs.normalizeTarget = normalizeTarget;
Object.assign(userjs, {
makeImage(imgSrc = '', attrs = {}, cname) {
const img = new Image();
img.alt = '';
img.referrerPolicy = 'no-referrer';
img.src = imgSrc;
if (!isEmpty(cname)) {
img.className = cname;
}
if (!isEmpty(attrs)) {
for (const key in attrs) {
img[key] = attrs[key];
}
}
return img;
},
halt(e) {
e.preventDefault();
e.stopPropagation();
},
injScript(text, remove = true) {
const inj = make('script', 'mu-injected', {
type: 'text/javascript',
innerHTML: text
});
(doc.head || doc.documentElement || doc).appendChild(inj);
if (!remove) {
return inj;
}
delay(1000).then(() => inj.remove());
},
/**
* @param {Node} element
* @param {MutationCallback} callback
* @param {MutationObserverInit} options
*/
observe(element, callback, options = { subtree: true, childList: true }) {
const observer = new MutationObserver(callback);
callback([], observer);
observer.observe(element, options);
return observer;
},
/**
* @param {string} url - URL of webpage to open
* @param {object} params - GM parameters
*/
openInTab(url, params = {}) {
params = Object.is(params, {}) ? '_blank' : params;
return win.open(url, params);
}
});
}