-
Notifications
You must be signed in to change notification settings - Fork 13
/
track.js
184 lines (146 loc) · 4.4 KB
/
track.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
var csvContent = "data:text/csv;charset=utf-8,Name,From,To,Duration\n";
var cameOnline = false;
var stalking = false;
var buttonAdded = false;
var showNotif = false;
var starttime;
var endtime;
var running = -1;
function f() {
try {
// select all elements first
const name = document.querySelector("#main > header > div._24-Ff > div > div > span").textContent;
const statusDiv = document.querySelector("#main > header > div._24-Ff > div.zzgSd._3e6xi > span");
const dpDiv = document.querySelector("#main > header > div._2YnE3 > div > img");
const status = statusDiv ? statusDiv.textContent : null;
const dp_url = dpDiv ? dpDiv.src : 'https://img.icons8.com/ios-filled/50/000000/whatsapp.png';
const dt = new Date();
const time = dt.toLocaleTimeString() + ', ' + dt.toLocaleDateString();
if (status === null && cameOnline === true) {
cameOnline = false;
endtime = new Date().getTime();
const diff = parseInt( (endtime - starttime) / 1000 );
const duration = `${ parseInt(diff / 60) } min and ${ diff % 60 } sec`;
console.log(`${name}: ${time}, Went Offline`);
console.log(`Duration: ${duration}`);
const startDate = new Date(starttime).toLocaleTimeString();
const endDate = new Date(endtime).toLocaleTimeString();
// add to CSV
csvContent += `${name},${startDate},${endDate},${duration}\n`;
// send notification
if (checkPermission()) {
let notif = new Notification(`${name} went offline`, {
icon: dp_url,
body: `from ${startDate} to ${endDate}\n Duration: ${duration}`
});
}
}
else if ((status === "online" || status === "typing…")
&& (cameOnline === false)) {
cameOnline = true;
starttime = new Date().getTime();
const startDate = new Date(starttime).toLocaleTimeString();
const endDate = new Date(endtime).toLocaleTimeString();
console.log(`${name}: ${time}, Came online`);
// send notification
if (checkPermission()) {
let notif = new Notification(`${name} came online`, {
icon: dp_url,
body: `Time: ${startDate}, ${endDate}\n`
});
}
}
}
catch(err) {
return;
}
}
function stalk() {
try {
if (!buttonAdded) {
putCSVLink();
putStopStalkButton();
buttonAdded = true;
checkPermission();
}
}
catch (e){
}
running = setInterval(f, 1000);
alert("Stalking!");
console.log("Stalking!");
alert("Provide permission if you want to get notifications when a user is online/offline.\
\n\nYou can turn notifications off later if you don't want to recieve them");
return running;
}
function getCSV(data) {
data = encodeURI(data);
let link = document.createElement("a");
link.setAttribute("href", data);
link.setAttribute("download", "stalk_data.csv");
document.body.appendChild(link);
link.click();
}
function resetCSV() {
csvContent = "data:text/csv;charset=utf-8,Name,From,To,Duration\n";
}
function getStalkData() {
getCSV(csvContent);
}
function putCSVLink() {
var button = document.createElement("button");
button.textContent = "GetCSV";
button.style.padding = "4px";
button.style.margin = "3px";
button.style.border = "1px solid black";
button.onclick = getStalkData;
var sideBar = document.querySelector("#side > header");
sideBar.appendChild(button);
// create reset csv data button
var button2 = document.createElement("button");
button2.textContent = "ResetCSV";
button2.style.padding = "4px";
button2.style.margin = "3px";
button2.style.border = "1px solid black";
button2.onclick = resetCSV;
var sideBar = document.querySelector("#side > header");
sideBar.appendChild(button2);
}
function toggleStalk() {
if (running === -1) {
stalk();
this.textContent = "Stop";
} else {
clearInterval(running);
running = -1;
alert("Stopped Stalking");
console.log("Stopped Stalking");
this.textContent = "Stalk";
}
}
function putStopStalkButton() {
var button = document.createElement("button");
button.style.padding = "4px";
button.style.margin = "2px";
button.style.border = "1px solid black";
button.textContent = "Stop";
button.onclick = toggleStalk;
var sideBar = document.querySelector("#side > header");
sideBar.appendChild(button);
}
stalk();
function checkPermission() {
if (!("Notification" in window)) {
return false;
}
else if (Notification.permission === "granted") {
return true;
}
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function(permission) {
if (permission === "granted") {
return true;
}
});
}
}