-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathforeground.js
213 lines (205 loc) · 8.07 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
if (!window.LCPredictorInjected) {
window.LCPredictorInjected = true;
let predictionsTimer;
let isListenerActive = false;
const setEventListener = () => {
try {
const tbody = document.querySelector("tbody");
if (!tbody) {
setTimeout(setEventListener, 100);
return;
}
const trs = tbody.querySelectorAll("tr");
if (!trs) {
setTimeout(setEventListener, 100);
return;
}
if (trs.length <= 1) {
// listen only if there is more than one row
isListenerActive = false;
return;
}
const tds = trs[1].querySelectorAll("td");
if (tds.length <= 1) {
isListenerActive = false;
return;
}
if (isListenerActive) return;
isListenerActive = true;
tds[1].addEventListener("DOMCharacterDataModified", async () => {
window.clearTimeout(predictionsTimer);
predictionsTimer = setTimeout(fetchPredictions, 500);
});
} catch (err) {
console.error(err);
}
};
const isContestRankingPage = (url) => {
return /^https:\/\/leetcode.com\/contest\/.*\/ranking/.test(url);
};
let rowsChanged = new Map();
let deltaTHInserted = false;
const fetchPredictions = async () => {
const thead = document.querySelector("thead");
if (!thead) {
predictionsTimer = setTimeout(fetchPredictions, 500);
return;
}
const tbody = document.querySelector("tbody");
if (!tbody) {
predictionsTimer = setTimeout(fetchPredictions, 500);
return;
}
const rows = tbody.querySelectorAll("tr");
if (!rows) {
predictionsTimer = setTimeout(fetchPredictions, 500);
return;
}
let contestId;
try {
contestId = document
.querySelector(".ranking-title-wrapper")
.querySelector("span")
.querySelector("a")
.innerHTML.toLowerCase()
.replace(/\s/g, "-");
} catch {
predictionsTimer = setTimeout(fetchPredictions, 500);
return;
}
const handlesMap = new Map();
const handles = [...rows].map((row, index) => {
try {
const tds = row.querySelectorAll("td");
if (tds.length >= 2) {
let handle, url;
try {
handle = tds[1].querySelector("a").innerText.trim();
url = tds[1].querySelector("a").getAttribute("href");
} catch {
handle = tds[1].querySelector("span").innerText.trim();
url = ""; // TODO: get data_region in this case
}
const data_region = /^https:\/\/leetcode.cn/.test(url)
? "CN"
: "US";
handlesMap.set(
(data_region + "/" + handle).toLowerCase(),
index
);
return handle;
}
} catch (err) {
console.debug(err);
}
});
chrome.runtime.sendMessage(
{
message: "get_predictions",
data: {
contestId,
handles,
},
},
(response) => {
if (!response) {
return;
}
try {
if (response.status === "OK") {
if (!deltaTHInserted && response.meta.total_count) {
const th = document.createElement("th");
th.innerText = "Δ";
thead.querySelector("tr").appendChild(th);
deltaTHInserted = true;
}
const rowsUpdated = new Map();
for (item of response.items) {
try {
const id = (
item.data_region +
"/" +
item._id
).toLowerCase();
if (handlesMap.has(id)) {
const rowIndex = handlesMap.get(id);
const row = rows[rowIndex];
let td;
if (rowsChanged.has(rowIndex)) {
td = row.lastChild;
} else {
td = document.createElement("td");
}
if (item.delta == null) {
td.innerText = "?";
td.style.color = "gray";
} else {
const delta =
Math.round(item.delta * 100) / 100;
td.innerText =
delta > 0 ? "+" + delta : delta;
if (delta > 0) {
td.style.color = "green";
} else {
td.style.color = "gray";
}
// td.style.fontWeight = "bold";
}
if (!rowsChanged.has(rowIndex)) {
row.appendChild(td);
}
rowsUpdated.set(rowIndex, true);
} else {
console.log(
`handle not found in the results: ${id}`
);
}
} catch (err) {
console.warn(err);
}
}
for (rowIndex of rowsChanged.keys()) {
if (
!rowsUpdated.has(rowIndex) &&
rowIndex < rows.length
) {
try {
const row = rows[rowIndex];
row.lastChild.innerText = "";
} catch {}
}
if (rowIndex >= rows.length) {
rowsChanged.delete(rowIndex);
}
}
for (rowIndex of rowsUpdated.keys()) {
rowsChanged.set(rowIndex, true);
}
}
} catch (err) {
console.warn(err);
}
}
);
};
// listen to url changes
chrome.runtime.onMessage.addListener(function (
request,
sender,
sendResponse
) {
if (request.message === "url_updated") {
if (!isListenerActive && isContestRankingPage(request.url)) {
window.clearTimeout(predictionsTimer);
predictionsTimer = setTimeout(fetchPredictions, 500);
}
if (!isContestRankingPage(request.url)) {
isListenerActive = false;
rowsChanged.clear();
deltaTHInserted = false;
} else {
setEventListener();
}
}
});
}