-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
128 lines (118 loc) · 4.46 KB
/
script.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
// change URL/API here!
const URL = 'https://manish404.pythonanywhere.com/db/send';
// html elements;
let total_el, nepse_index_el, sensitive_index_el, dateEl, data_table;
function getElements() {
// fetches html element after website gets loaded!
total_el = document.querySelector("body > app-root > div > main > div > app-live-market > div > div > div:nth-child(2) > div.d-flex.flex-column.align-items-start.justify-content-start > div.livemarket__adv.d-flex.align-items-center > div"),
nepse_index_el = document.querySelector("body > app-root > div > main > div > app-live-market > div > div > div:nth-child(3) > div > div:nth-child(1) > div"),
sensitive_index_el = document.querySelector("body > app-root > div > main > div > app-live-market > div > div > div:nth-child(3) > div > div:nth-child(2) > div"),
dateEl = document.querySelector("body > app-root > div > main > div > app-live-market > div > div > div:nth-child(1) > div > div > span"),
data_table = document.querySelector("body > app-root > div > main > div > app-live-market > div > div > div.table-responsive > table > tbody");
}
// functions
function fn(top_el, el, child, convertInto = 'f') {
/*
top_el : parent element
el : element name; eg: div, #..., etc.
child : child element
convertInto [options] : [s: string, i: integer, f: float]
*/
let text = top_el.querySelector(`${el}:nth-child(${child})`).innerText;
text = text.replace(/\,|\(|\)|\%/g, '');
if (convertInto === 'i') text = parseInt(text);
else if (convertInto === 'f') text = parseFloat(text);
return text;
}
// Stock class
class Scrip {
constructor(row) {
return this.extract(row);
}
extract(row) {
const col2 = row.querySelector('td:nth-child(2)');
// return [symbol, data_from_row]
return [col2.innerText, {
'name': col2.title,
'point_change': fn(row, 'td', 5),
'percent_change': fn(row, 'td', 6),
'price': {
'latest_traded': fn(row, 'td', 3),
'average': fn(row, 'td', 10),
'open': fn(row, 'td', 7),
'high': fn(row, 'td', 8),
'low': fn(row, 'td', 9),
'previous_closing': fn(row, 'td', 12)
},
'volume': fn(row, 'td', 11, 'i')
}]
}
}
// important functions
function fetch_data() {
// console.log("[+] Fetching data!");
const db = {};
/*
scrape data and return jsonified data;
i) All details of shares;
ii) Today's summary;
*/
// today's market summary;
db['date'] = dateEl.innerText.split(' ').splice(2, 6).join(' ');
db['nepse_index'] = {
'point': fn(nepse_index_el, 'span', 1, 'i'),
'point_change': fn(nepse_index_el, 'span', 3),
'percent_change': fn(nepse_index_el, 'span', 4)
};
db['sensitive_index'] = {
'point': fn(sensitive_index_el, 'span', 1, 'i'),
'point_change': fn(sensitive_index_el, 'span', 3),
'percent_change': fn(sensitive_index_el, 'span', 4)
};
db['total'] = {
'turnover': parseFloat(fn(total_el, 'div', 1, 's').split(' ').splice(3)[0]),
'traded_shares': parseInt(fn(total_el, 'div', 2, 's').split(' ').splice(3)[0]),
'transactions': parseInt(fn(total_el, 'div', 3, 's').split(' ').splice(2)[0]),
'scrips_traded': parseInt(fn(total_el, 'div', 4, 's').split(' ').splice(3)[0])
};
// company/share data;
db['companies'] = {};
const rows = data_table.querySelectorAll('tr');
rows.forEach(row => {
const [name, scrip_detail] = new Scrip(row);
db['companies'][name] = scrip_detail;
});
return db;
}
function send_data(data) {
// console.log("[+] Sending data!");
fetch(URL, {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
mode: 'no-cors',
body: JSON.stringify(data)
});
// console.log("[+] Data sent!");
return 'Data has been sent!';
}
// publish updates on loop (checks every 5 seconds, sends if change is detected!)
function main() {
let data, key, temp;
key = dateEl.innerText;
setInterval(() => {
temp = dateEl.innerText;
if (temp != key) {
data = fetch_data();
send_data(data);
key = temp;
}
}, (1000 * 5));
}
// M A I N
setTimeout(() => {
getElements();
// console.log("[+] Extension Started!");
main();
}, 1000); // starts after 1 second