-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetHelper.js
More file actions
143 lines (136 loc) · 3.62 KB
/
Copy pathnetHelper.js
File metadata and controls
143 lines (136 loc) · 3.62 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
/**
* This is a really simple solution for checking and saving data.
* This class should exist in 4 ways.
* 1 use a fetch to save data,
* 2 use Gm.xmlhttpRequest to save data,
* 3 use WebSocket, and 4 use the web scrapper API.
* 4 use indexedB
* I think using local storage is not ideal. Because of the size of the data.
* Unfortunately at the moment, the greasemonkey has no options for IndexedDb
*
* @todo add fetch support
* @todo add simple scapper api support support
* @todo add indexedb support
*
* @class
*
**/
const NetHelperClass = class{
/**
*
* @private
* @type {string}// @var {str}
**/
#url = '';
/**
* @private
* @type {boolean}// @var {bool}
**/
#debug_status = false
/**
*
* @param {string} url_ // @param {str}
* @param {boolean} [debug_=false]// @param {?bool=false}
* @constructs
**/
constructor(url_ , debug_ = false) {
this.#url = (url_).toString();
this.#debug_status = !!debug_;
}
/**
*
* @param {any}
* @private
*
**/
#debug(any_,){
if(!this.#debug_status)
return;
console.table(any_);
console.log(any_);
}
/**
*
* @param {Object.<string, string|number>}// @param {map<str, str|int>}
* // @async
* @public
* @returns {Promise<string> }// @return {str}
**/
get(query_){
/* @type {string}// @const {str} */
const _url = (
this.#url+
"?"+
(new URLSearchParams(query_)).toString()
);
return new Promise(function (resolve, reject) {
GM.xmlHttpRequest({
method: "GET",
url: (_url),
headers: {
"Content-Type": "application/json"
},
onload: function(response) {
resolve(response.responseText);
}
});
});
};
/**
*
* @param {Object.<string, string|number>}// {map<str, str|int>}
* @async
* @public
* @returns {boolean}// {bool}
**/
async check(query_){
/* @type {string}// @const {str} */
const result = await this.get(query_);
if (result === "{}" || result === "[]" )
return true;
return false;
};
/**
*
* @param {Object.<string, any>} // {map<str, str|int>}
* // @async
* @public
* @returns {Promise<string> }// {str}
**/
add(data_){
/* @type {string}// @const {str} */
const _url = this.#url.toString();
/* @type {Object.<string, string|number>}// @var {map<str, int|str>} */
const _data = JSON.stringify(data_);
return new Promise(function (resolve, reject) {
GM.xmlHttpRequest({
method: "POST",
url: _url,
data: (_data),
headers: {
"Content-Type": "application/json"
},
onload: function(response) {
resolve(response.responseText);
}
});
});
};
/**
*
* @param {Object.<string, string|number>}// {map<str, str|int>}
* @param {?Object.<string, string|number>}// {?map<str, str|int>}
* @async
* @public
* @returns {boolean}// {bool}
**/
async checkAndAdd(query_, data_){
this.#debug(data_);
if ( typeof data_ === 'undefined' )
data_ = query_;
const result = await this.check(query_);
if ( result )
await this.add(data_);
return result;
};
};