forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
120 lines (118 loc) · 3.35 KB
/
Copy pathapi.js
File metadata and controls
120 lines (118 loc) · 3.35 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
'use strict';
const win = self ?? window,
doc = win.document,
us = {
estr(str) {
return Object.is(str,null) || Object.is(str,undefined) || typeof str === 'string' && Object.is(str.trim(),'')
},
ael(elm,event,callback) {
elm = elm ?? doc;
if(typeof screen.orientation === 'undefined' || (navigator.maxTouchPoints || 'ontouchstart' in document.documentElement)) {
if(event === 'click') {
elm.addEventListener('mouseup', callback);
elm.addEventListener('touchstart', callback);
elm.addEventListener('touchend', callback);
}
};
return elm.addEventListener(event, callback);
},
/** Waits until args return true */
async check(args) {
while (this.estr(args)) {
await new Promise(resolve => requestAnimationFrame(resolve) )
};
return args;
},
/** Can make various elements */
make(elm,cname,attrs = {}) {
let el = doc.createElement(elm);
if(cname || !this.estr(cname)) {
el.className = `mujs-${cname}`;
};
if(attrs) {
for (let key in attrs) {
el[key] = attrs[key];
};
};
return el;
},
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
getItem(key) {
return localStorage.getItem(key);
},
fetchURL(url,method = 'GET',responseType = 'json',params = {}) {
return new Promise((resolve, reject) => {
fetch(url, {
method: method,
...params,
}).then((response) => {
if(!response.ok) reject(response);
if(responseType.match(/json/gi)) {
resolve(response.json());
} else if(responseType.match(/text/gi)) {
resolve(response.text());
} else if(responseType.match(/blob/gi)) {
resolve(response.blob());
};
resolve(response);
}).catch(reason => console.info(reason));
});
},
fetchFile(path,responseType = 'text') {
return new Promise((resolve, reject) => {
fetch(path).then((response) => {
if(!response.ok) reject(response);
if(responseType.match(/json/gi)) {
resolve(response.json());
} else if(responseType.match(/text/gi)) {
resolve(response.text());
} else if(responseType.match(/blob/gi)) {
resolve(response.blob());
};
resolve(response);
}).catch(reason => console.info(reason));
});
},
halt(e) {
e.preventDefault();
e.stopPropagation();
},
inject(src) {
let s = this.make('script','injected', {
type: 'text/javascript',
innerHTML: src,
});
(doc.head || doc.documentElement || doc).appendChild(s);
if(s) {
s.remove();
};
},
/**
* @param {Node} element
* @param {MutationCallback} callback
* @param {MutationObserverInit} options
*/
observe(element, callback, options = {subtree:true,childList:true}) {
let observer = new MutationObserver(callback);
callback([], observer);
observer.observe(element, options);
return observer;
},
/** Waits until element exists */
async query(selector,root) {
root = root ?? doc;
while(this.estr(root.querySelector(selector))) {
await new Promise(resolve => requestAnimationFrame(resolve))
};
return root.querySelector(selector);
},
removeItem(key) {
return localStorage.removeItem(key);
},
setItem(key,value) {
return localStorage.setItem(key,value);
},
};
export { us };