forked from mhthnz/hashfair-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.js
More file actions
471 lines (418 loc) · 13 KB
/
Application.js
File metadata and controls
471 lines (418 loc) · 13 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/**
* Application class.
* Need for loading modules and save common module data (history page, btc price, etc...)
*
* @property {string} historyPage Content of history page
* @property {int} btcPrice Current btc price
* @property {array} modules Array of modules.
*/
class Application
{
/**
* Constructor of application class.
* Init class properties.
* @param {[]} config Array of config contains:
*
* config.modules = ([
* {module: 'chart', dependency: Object},
* {module: 'balance'},
* ... etc
* ])
* config.btcPrice = 1231
* config.historyPage = Html code
* config.language = 'eng'
*
*/
constructor(config = {})
{
/**
* Internationalization class.
* @type {I18n}
*/
this.i18n = null;
/**
* Content of history page.
* @type {string}
*/
this.historyPage = '';
/**
* Current btc price.
* @type {int}
*/
this.btcPrice = 0;
/**
* Modules for loading.
* @type {[]} of object
*/
this.modules = [];
/**
* Collection of ContractItem.
* @type {ContractsCollection}
*/
this.contracts = new ContractsCollection();
/**
* Was initialized contracts?
* @type {boolean}
*/
this.contractsInit = false;
/**
* Collection of PurchaseItem.
* @type {PurchasesCollection}
*/
this.purchases = new PurchasesCollection();
/**
* Was initialized purchases?
* @type {boolean}
*/
this.purchasesInit = false;
/**
* Collection of WithdrawalItem.
* @type {WithdrawalsCollection}
*/
this.withdrawals = new WithdrawalsCollection();
/**
* Was initialized withdrawals?
* @type {boolean}
*/
this.withdrawalsInit = false;
/**
* Collection of BtcPayoutItem and PayoutItem.
* @type {PayoutsCollection}
*/
this.payouts = new PayoutsCollection();
/**
* Was initialized payouts?
* @type {boolean}
*/
this.payoutsInit = false;
/**
* View debug data in console.
* @type boolean
*/
this.verbose = false;
// Fill properties
if (config.hasOwnProperty('historyPage') && config.historyPage != '') {
this.historyPage = config.historyPage;
}
if (config.hasOwnProperty('btcPrice') && config.btcPrice > 0) {
this.btcPrice = config.btcPrice;
}
if (config.hasOwnProperty('modules') && Array.isArray(config.modules)) {
this.modules = config.modules;
}
if ((config.hasOwnProperty('verbose') && config.verbose == 1) || window.location.search.match(new RegExp('verbose=1'))) {
this.verbose = true;
}
if (config.hasOwnProperty('language')) {
this.i18n = new I18n(config.language);
} else {
this.i18n = new I18n();
}
this.log("Init Application class.");
}
/**
* If verbose = true - print debug data in console.
* @param string text Debug text
*/
log(text)
{
if (this.verbose) {
console.log(text);
}
}
/**
* Return internationalization class.
* @returns {I18n}
*/
getI18n()
{
return this.i18n;
}
/**
* Lazyload getter for contracts
* @return ContractsCollection
*/
getContracts()
{
if (!this.contractsInit) {
this.initContracts();
}
return this.contracts;
}
/**
* Lazyload getter for purchases.
* @return instance of Contract collection
*/
getPurchases()
{
if (!this.purchasesInit) {
this.initPurchases();
}
return this.purchases;
}
/**
* Lazyload getter for withdrawals.
* @return instance of Contract collection
*/
getWithdrawals()
{
if (!this.withdrawalsInit) {
this.initWithdrawals();
}
return this.withdrawals;
}
/**
* Lazyload getter for payouts.
* @return instance of Payout collection
*/
getPayouts()
{
if (!this.payoutsInit) {
this.initPayouts();
}
return this.payouts;
}
/**
* Check class properties and run modules.
*/
run()
{
if (this.historyPage == '') {
this.log("Can't run application. History page not found.");
return;
}
if (this.btcPrice == 0) {
this.log("Can't run application. Btc price is not defined.");
return;
}
if (!Array.isArray(this.modules)) {
this.log("Can't run application. Expect array of object.");
return;
}
if (this.i18n == null) {
this.log("Can't run application. Language not found.");
}
// Load modules
for (var i = 0; i < this.modules.length; i++) {
let module = this.modules[i];
if (!module.hasOwnProperty('module')) {
this.log("Can't find module property on " + (i+1) + " object. Skipping.");
continue;
}
this.loadModule(module);
}
}
/**
* Load module from `modules` folder. Dependency is optional.
* @param object module {module: 'moduleName', dependency: Object}
*/
async loadModule(module)
{
this.log("Loading module: " + module.module);
var file = chrome.extension.getURL('modules/' + module.module + '.js');
var app = this;
var dependency = module.hasOwnProperty('dependency') ? module.dependency : null;
$.ajax({url:file}).done(function(script) {
eval(
script + "\r\n\
(new " + module.module + "(app, dependency)).run();\r\n\
");
});
}
/**
* Initialize withdrawals collection.
*/
initWithdrawals()
{
let time = new Date().getTime();
var table = $(this.historyPage).find('table').eq(2);
let app = this;
// Each all rows
$(table).find("tr").each(function(i, v){
let row = [];
let valid = true;
// Each all cols
$(this).children('td').each(function(j, vv){
let text = '';
// If date
if (j === 1) {
text = $(this).text();
text = text.substr(0, 8);
} else if (j === 3) {
text = $(this).html();
if (text.indexOf('text-success') + 1 == 0) {
valid = false;
}
} else {
text = $(this).text();
}
row[j] = text;
});
// Add item to collection
if (row.length > 0 && valid) {
let type = app.getWithdrawalType(row[2]);
let amount = parseFloat(row[2]);
let date = row[1];
app.withdrawals.addItem(new WithdrawalItem(date, type, amount));
}
});
this.log("Withdrawals init in " + (new Date().getTime() - time) + " ms.");
this.withdrawalsInit = 1;
}
getWithdrawalType(text)
{
if (text.indexOf('BTC') + 1 > 0) {
return WithdrawalItem.typeBTC;
} else if (text.indexOf('DASH') + 1 > 0) {
return WithdrawalItem.typeDASH;
} else if (text.indexOf('ETH') + 1 > 0) {
return WithdrawalItem.typeETH;
}
}
/**
* Initialize purchases collection.
*/
initPurchases()
{
let time = new Date().getTime();
var table = $(this.historyPage).find('table').eq(1);
let app = this;
// Each all rows
$(table).find("tr").each(function(i, v){
let row = [];
let valid = true;
// Each all cols
$(this).children('td').each(function(j, vv){
let text = '';
// If date
if (j === 5) {
text = $(this).text();
text = text.substr(0, 8);
} else if (j === 6) {
text = $(this).html();
if (text.indexOf('text-success') + 1 == 0) {
valid = false;
}
} else {
text = $(this).text();
}
row[j] = text;
});
// Add item to collection
if (row.length > 0 && valid) {
let type = app.getPurchaseType(row[1]);
let quantity = parseFloat(row[2]);
let paid = parseFloat(row[3].replace(',', ''));
let method = (row[4].indexOf('balance transfer') + 1) ? PurchaseItem.methodBalance: PurchaseItem.methodPaysystems;
let date = row[5];
app.purchases.addItem(new PurchaseItem(date, type, quantity, paid, method));
}
});
this.log("Purchases init in " + (new Date().getTime() - time) + " ms.");
this.purchasesInit = 1;
}
getPurchaseType(text)
{
if (text.indexOf('X11') + 1 > 0) {
return PurchaseItem.typeDASH;
} else if (text.indexOf('SHA-256') + 1 > 0) {
return PurchaseItem.typeSHA;
} else if (text.indexOf('Scrypt') + 1 > 0) {
return PurchaseItem.typeSCRYPT;
} else if (text.indexOf('ETHASH') + 1 > 0) {
return PurchaseItem.typeETH;
}
this.log("Unknown type: " + text);
}
/**
* Initialize payouts collection.
*/
initPayouts()
{
let time = new Date().getTime();
// Get table
var table = $(this.historyPage).find('table').last();
var data = {};
$(table).find("tr").each(function(i, v){
let row = [];
$(this).children('td').each(function(j, vv){
let text = '';
// If date
if (j === 1) {
text = $(this).text();
text = text.substr(0, 8);
} else {
text = $(this).text();
}
row[j] = text;
});
if (row.length > 0) {
let date = row[1];
if (!data.hasOwnProperty(date)) {
data[date] = [];
}
data[date].push(row);
}
});
var app = this;
// Fill payouts items
for(var prop in data) {
let date = prop;
for (var i = 0; i < data[date].length; i++) {
let row = data[date][i];
// Get name of tr
var title = row[0];
if (title !== "Scrypt payout (BTC)" && title !== "SHA-256 payout (BTC)" && title !== "X11 payout (DASH)" && title !== "ETHASH payout (ETH)") {
continue;
}
// Add item to collection
var scrypt = false;
switch(title) {
// Btc
case 'Scrypt payout (BTC)':
scrypt = true;
case 'SHA-256 payout (BTC)': {
// Calculate maintenance
let main = new Date().getTime();
var maintenance = app.getMaintenance(data[date], scrypt === false ? "SHA-256 maintenance (BTC)" : "Scrypt maintenance (BTC)");
if (maintenance === false) {
return;
}
var price = row[2];
app.payouts.addItem(new BtcPayoutItem(date, parseFloat(price), parseFloat(maintenance), scrypt === false ? BtcPayoutItem.typeSha : BtcPayoutItem.typeScrypt));
break;
}
// Dash
case 'X11 payout (DASH)': {
var price = row[6];
app.payouts.addItem(new DashItem(date, parseFloat(price)));
break;
}
// Ethereum
case 'ETHASH payout (ETH)': {
var price = row[4];
app.payouts.addItem(new EthItem(date, parseFloat(price)));
}
}
}
};
this.log("Payouts init in " + (new Date().getTime() - time) + " ms.");
this.payoutsInit = true;
}
/**
* Get maintenance from scrypt and sha.
* @param {string} date Format: DD.MM.YYYY
* @param {[]} rows
* @param {string}text
* @returns {float|boolean}
*/
getMaintenance(rows, text)
{
for (var i = 0; i < rows.length; i++) {
if (rows[i].includes(text)) {
return rows[i][2];
}
}
return false;
}
}