-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminerHelper.js
More file actions
203 lines (186 loc) · 4.31 KB
/
minerHelper.js
File metadata and controls
203 lines (186 loc) · 4.31 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
/**
* CallMeta
* {
* "before" : <BeforeMeta>,
* "next" : <NextMeta>
* }
*
* @typedef {Object.<string, BeforeMeta|NextMeta>
**/
/**
* BeforeMeta
* {
* "type" : "scroll|click",
* "element" : <DOMElement>,
* "text" : <string>
* }
*
* @typedef {Object.<string, DOMElement|string>} ScrapMeta
**/
/**
* NextMeta
* {
* "type" : "scroll|click",
* "element" : <DOMElement>,
* "text" : <string>
* }
*
* @typedef {Object.<string, DOMElement|string>} ScrapMeta
**/
/**
* At some times we can realize the javascript class
* is not always a comfortable object.
* Because no friend, no protected, no neighbor.
* That reason this class is a prototype.
*
* The miner class is necessary only
* when both scrapping and mining are required.
* The primary purpose of this class is to
* initiate a loop function that enables
* continuous mining with pagination.
*
*
* @param {Array.<DOMElement>}
* @param {Object.<string, string | Object<string, string>}
* @param {string}
* @param {string}
* @param {string}
* @param {?CallMeta}
* @class
**/
const MinerHelperClass = function(element_, meta_, key_, url_, call_meta_){
/**
* Unnecessary public gate for the mine function.
*
* @public
**/
this.mining = async function(){
_before();
return _mining();
};
/**
* Public gate function for the loop function.
*
* @public
**/
this.loop = function(){
_before();
if (typeof _next_meta === 'undefined')
return _mining();
if (_next_meta['type'] === 'click')
return _paginatorLoop();
return _scroll();
};
/**
* Public gate function for the loop function.
*
* @public
* @return {ScrapperHelperClass}
**/
this.sH = function(){
return _sH;
};
// pre constructor (backward comp)
if (typeof call_meta_ === 'undefined'){
call_meta_ = {};
}else if(typeof call_meta_['type'] !== 'undefined')
call_meta_ = {
"next" : call_meta_
};
/** @type {ScrapperHelperClass} **/
const _sH = new ScrapperHelperClass(url_);
/** @type {MinerToolClass} **/
const _mT = new MinerToolClass('miner', 4000, 2000);
/** @type {MinerToolClass.setRandOut} **/
const _setRandOut = _mT.setRandOut;
/** @type {MinerToolClass.log} **/
const _log = _mT.log;
/** @type {DOMElement} **/
const _element = element_;
/** @type {DOMElement} **/
const _main_element = element_;
/** @type {ScrapMeta} **/
const _meta_tag = meta_;
/** @type {string} **/
const _search_tag = key_;
/** @type {BeforeMeta} **/
const _before_meta = call_meta_['before'];
/** @type {NextMeta} **/
const _next_meta = call_meta_['next'];
/**
*
* @private
**/
const _mining = async function(){
_log('mining', 'start');
await _sH.mineAndSave(
_main_element,
_meta_tag,
_search_tag
);
_log('mining', 'done');
};
/**
*
* @private
**/
const _before = function(){
if (typeof _before_meta === 'undefined')
return;
if (_before_meta['type'] === 'click')
return _before_click();
};
/**
*
* @private
**/
const _before_click = function(){
if ( _before_meta['type'] === 'click')
_sH.cH.clickSmart(
_before_meta['element'],
_before_meta['text']
);
};
/**
*
* @private
**/
const _click = function(){
if ( _sH.cH.clickSmart(
_next_meta['element'],
_next_meta['text']
)
){
_setRandOut(_paginatorLoop);
};
};
/**
*
* @private
**/
const _scroll = async function(){
await (new ScrollerHelperClass(
_next_meta['element'],
_element
)).scroll();
_setRandOut(_mining);
};
/**
*
* @private
**/
const _next = function(){
if (_next_meta['type'] === 'click')
return _click();
}
/**
*
* @private
**/
const _paginatorLoop = async function(){
_setRandOut(async function(){
await _mining();
_setRandOut(_next);
});
};
};