-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
85 lines (69 loc) · 2.91 KB
/
content.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
while (document == null || document.head == null);
var s = document.createElement("script");
// Send message directly without first sending a chrome.runtime.sendMessage to the background
function sendToBackground(data) {
chrome.storage.local.get(["token"], function (token) {
chrome.storage.local.get(["url"], function (url) {
fetchUrl = url["url"] + "?auth=" + token["token"];
console.debug("[Teams Client] Send to: ", fetchUrl);
fetch(fetchUrl, { // Send the auth via query parameter to prevent cors issues
method: "POST",
mode: "no-cors",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: data
}).then(response => console.debug("[Teams Client] Posted: ", JSON.stringify(data)));
});
});
}
function hookWebSocket() {
window.RealWebSocket = window.WebSocket;
window.WebSocket = function () {
var socket = new window.RealWebSocket(...arguments);
socket.addEventListener("message", (e) => {
// We need to send a message to our extension since in an injected script, we do not have access to chrome.*
var extensionID = "fekcnofjiglkhlhiiodbpbedplghdkfp";
chrome.runtime.sendMessage(extensionID, { action: "getTokenUrl" },
function (response) {
fetchUrl = response.url + "?auth=" + response.token;
console.debug("[Teams Client] Send to: ", fetchUrl);
fetch(fetchUrl, { // Send the auth via query parameter to prevent cors issues
method: "POST",
mode: "no-cors",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: e.data
}).then(response => console.debug("[Teams Client] Posted: ", JSON.stringify(response)));
});
});
return socket;
}
}
s.innerHTML = `
${hookWebSocket.toString()}
hookWebSocket();
`;
document.head.prepend(s);
setInterval(function () {
let targetNodes = document.querySelectorAll('.activity-badge');
if (targetNodes.length <= 0) {
return
}
let sum = 0;
targetNodes.forEach(function (node) {
// If the node also has the 'activity-badge-container' class, skip it
if (node.classList.contains('activity-badge-container')) {
return;
}
let value = parseInt(node.textContent.trim(), 10);
if (!isNaN(value)) { // make sure the content was a number
sum += value;
}
});
let activityObject = { activity: { count: sum } };
sendToBackground(JSON.stringify(activityObject));
}, 5000); // 5000 milliseconds = 5 seconds