This repository was archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeground.js
345 lines (232 loc) · 8.75 KB
/
foreground.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
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
// Disable multiple injection on same tab
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.text === 'are_you_there_content_script?') {
sendResponse({status: "yes"});
}
});
class COTPSBot {
constructor(){
var parent = this;
this.manifestData = chrome.runtime.getManifest();
this.options = null;
this.$wrap = document.querySelector('.transaction-wrap');
this.$debugBlock;
this.wallets = {};
this.counterInterval;
this.currentUrl;
this.refreshStepIndex;
this.nextRefreshStepIndex;
this.createDebugbar();
this.setMinWallet(5);
this.setWallet(0);
this.setTransaction(0);
this.setTotal(0);
// Check when loading is over
this.initInterval = setInterval(function(){
if(parent.isLoading() == false){
clearInterval(parent.initInterval);
parent.getOptions().then(function(){
parent.init();
});
}
}, 500);
}
createDebugbar(){
var parent = this;
this.$debugBlock = document.createElement('div');
var debugHTML = '';
debugHTML += '<h1>Bot <span>by harkor</span></h1>';
debugHTML += '<ul>';
debugHTML += '<li class="wallet">Wallet: <span class="value">?</span>$</li>';
debugHTML += '<li class="transaction">In transaction: <span class="value">?</span>$</li>';
debugHTML += '<li class="total">Total: <span class="value">?</span>$</li>';
debugHTML += '</ul>';
debugHTML += '<ul>';
debugHTML += '<li class="minWallet">Minimum balance to make order(s): <span class="value">?</span>$</li>';
debugHTML += '</ul>';
debugHTML += '<ul>';
debugHTML += '<li class="refresh">Refresh in <span class="value">?</span> seconds <button class="reset-refresh-timer">reset</button></li>';
debugHTML += '</ul>';
debugHTML += '<span class="version">v'+ this.manifestData.version +'</span>';
this.$debugBlock.classList.add('debugbar');
this.$debugBlock.innerHTML = debugHTML;
this.$debugBlock.querySelector('.reset-refresh-timer').addEventListener('click', function(){
chrome.storage.local.remove(['refreshStepIndex']);
parent.setRefreshStepIndex(0);
location.reload();
});
// Add result to wrapper
this.$wrap.appendChild(this.$debugBlock);
}
init(){
var parent = this;
console.log('App is loaded');
document.querySelectorAll('.uni-tabbar-bottom .uni-tabbar__item').forEach(function(item){
item.addEventListener('click', function(){
var $page = document.querySelector('uni-page');
this.url = $page.getAttribute('data-page');
});
});
var wallets = this.checkWallets();
var minWallet = 0;
minWallet = wallets.total * (this.options.minimum_wallet / 100);
if(this.options.min_wallet_system == 'fixed_val'){
minWallet = this.options.minimum_wallet;
}
this.setMinWallet(minWallet); // Do order only if we have receive 90% of our total
chrome.storage.local.get(['refreshStepIndex'], function(result) {
if(result.refreshStepIndex == undefined){
result.refreshStepIndex = 0;
}
parent.setRefreshStepIndex(result.refreshStepIndex);
parent.doOrder(); // Try to make order on load
});
}
isLoading(){
var status = false;
var $unitoast = document.querySelectorAll('uni-toast');
if($unitoast.length > 0){
$unitoast.forEach(function(item){
var $unitoastContent = $unitoast[0].querySelector('.uni-toast__content');
var text = $unitoastContent.innerHTML.trim();
if(text == 'loading'){
status = true;
}
});
}
return status;
}
checkWallets(){
// Wallet
var $wallet = document.querySelector('.division-right .division-num');
var walletBalance = parseFloat($wallet.innerHTML);
this.setWallet(walletBalance);
if(walletBalance < this.minimumWallet){
$wallet.classList.add('money-too-low');
} else {
$wallet.classList.remove('money-too-low');
}
// Transaction
var $transaction = document.querySelector('.division-left .division-num');
var transactionBalance = parseFloat($transaction.innerHTML);
this.setTransaction(transactionBalance)
// Total
// var $total = document.querySelector('.money-num');
// var totalBalance = parseFloat($total.innerHTML);
var totalBalance = walletBalance + transactionBalance;
this.setTotal(Math.floor(totalBalance));
return this.wallets;
}
canMakeOrder(){
var status = false;
if(this.checkWallets().wallet > this.minimumWallet){
status = true;
} else {
status = false;
}
return status;
}
async doOrder(){
var parent = this;
if(this.canMakeOrder()){
chrome.storage.local.remove(['refreshStepIndex']);
this.setRefreshStepIndex(0);
this.setMinWallet(5);
document.querySelector('.orderBtn').click();
await sleep(this.options.delay_between_actions * 1000);
document.querySelector('.fui-dialog__inner .buttons uni-button[type=primary]').click();
await sleep(this.options.delay_between_actions);
document.querySelector('.fui-wrap__show uni-button[type=primary]').click();
await sleep(this.options.delay_between_actions);
this.doOrder();
} else {
var nextRefreshStepIndex = this.refreshStepIndex + 1;
if(this.options.refresh_steps[nextRefreshStepIndex] == undefined){
nextRefreshStepIndex = this.options.refresh_steps.length-1;
}
this.saveRefreshStepIndex(nextRefreshStepIndex);
var counter = this.options.refresh_steps[this.refreshStepIndex];
this.counterInterval = setInterval(function(){
counter--;
document.querySelector('.debugbar .refresh .value').innerHTML = counter;
if(counter <= 0){
location.reload();
}
}, 1000);
}
}
// WIP
checkLastOrder(){
var $records = document.querySelectorAll('.record-list');
if($records.length >= 2){
var time = document.querySelectorAll('.record-list')[1].querySelector('.time').innerHTML;
time = time.split(' ');
date = time[0].split('-');
time = time[1].split(':');
var myDate = new Date();
myDate.setDate(date[1]);
myDate.setMonth(date[0]-1);
myDate.setHours(time[0], time[1], 0);
}
}
setMinWallet(value){
this.minimumWallet = value;
this.$debugBlock.querySelector('.minWallet .value').innerHTML = value;
}
setWallet(value){
this.wallets.wallet = value;
this.$debugBlock.querySelector('.wallet .value').innerHTML = value;
}
setTransaction(value){
this.wallets.in_transaction = value;
this.$debugBlock.querySelector('.transaction .value').innerHTML = value;
}
setTotal(value){
this.wallets.total = value;
this.$debugBlock.querySelector('.total .value').innerHTML = value;
}
setRefreshStepIndex(value){
this.refreshStepIndex = value;
}
saveRefreshStepIndex(value){
chrome.storage.local.set({'refreshStepIndex': value});
}
getOptions(){
var parent = this;
return new Promise((resolve, reject) => {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function(){
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var defaultOptions = JSON.parse(httpRequest.response);
chrome.storage.local.get('options', (data) => {
var savedOptions;
if(data.options != undefined){
savedOptions = data.options;
} else {
savedOptions = {};
}
var mergedOptions = { ...defaultOptions, ...savedOptions };
parent.setOptions(mergedOptions);
resolve(mergedOptions);
});
}
}
};
httpRequest.open('GET', chrome.runtime.getURL('defaultOptions.json'));
httpRequest.send();
});
}
setOptions(value){
value.delay_between_actions = parseFloat(value.delay_between_actions);
value.minimum_wallet = parseFloat(value.minimum_wallet);
value.refresh_steps = value.refresh_steps.split(',');
value.refresh_steps.forEach(function(item, key){
value.refresh_steps[key] = parseFloat(item) * 60;
});
this.options = value;
}
}
console.log('Bot injected');
new COTPSBot;
const sleep = ms => new Promise(r => setTimeout(r, ms));