-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
156 lines (146 loc) · 5 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
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
// Function to handle DOM changes
const protocols= {
"parseUrlAndContents": function parseUrlAndContents(url) {
const problemId = getProblemId(url);
if(problemId==="") {
let problemUrl = getProblemUrlFromTable();
let verdict = getVerdictFromTable();
console.log("verdict: ", verdict);
sendToServer(
{
type: "handleVerdict",
link: problemUrl,
verdict: verdict,
}
);
return;
}
let tagBoxes = document.querySelectorAll('span.tag-box');
let tags = [];
let difficulty = 800;
tagBoxes.forEach(span => {
// Check if the <span> has a title attribute with the value "Difficulty"
let textContent = span.textContent.trim();
if (span.getAttribute('title') === 'Difficulty') {
difficulty = textContent;
}
else {
tags.push(textContent);
}
});
if(problemId === "") {
return;
}
sendToServer({
type: "parseUrlAndContents",
id: problemId,
tags: tags,
difficulty: difficulty,
platform: "codeforces",
link: url,
});
},
"verdict-accepted": function () {
const problemId = getProblemId(getProblemUrlFromTable());
sendToServer( {
type: 'verdict-accepted',
id: problemId,
status: 'AC',
link: problemUrl,
});
},
};
protocols.parseUrlAndContents(window.location.href, document);
function getProblemUrlFromTable() {
var table = document.querySelector('.status-frame-datatable');
if (table) {
var row = table.querySelectorAll('tr')[1];
if (row) {
var cells = row.querySelectorAll('td');
if (cells.length >= 4) {
var fourthColumnCell = cells[3];
console.log(fourthColumnCell);
var problemUrl = fourthColumnCell.querySelector('a').href;
console.log(problemUrl);
return problemUrl;
} else {
console.error('The first row does not have 4 columns.');
}
} else {
console.error('No rows found in the table.');
}
} else {
console.error('No table found.');
}
return "";
}
function getVerdictFromTable() {
var table = document.querySelector('.status-frame-datatable');
if (table) {
var row = table.querySelectorAll('tr')[1];
if (row) {
var cells = row.querySelectorAll('td');
if (cells.length >= 6) {
var col = cells[5];
console.log(col);
var verdict = col.innerText;
return verdict;
} else {
console.error('The first row does not have 6 columns.');
}
} else {
console.error('No rows found in the table.');
}
} else {
console.error('No table found.');
}
return "";
}
function handleDomChanges(mutationsList) {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
console.log(mutation.addedNodes);
if (mutation.addedNodes.length>0 && mutation.addedNodes[0].className in protocols) {
protocols[mutation.addedNodes[0].className]();
}
}
}
}
// Function to send data to the server
function sendToServer(data) {
console.log('data to server: ', data);
fetch('http://localhost:3000/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(response => response.json())
.then(data => console.log('Success:', data))
.catch((error) => console.error('Error:', error));
}
// Create an observer instance linked to the callback function
const observer = new MutationObserver(handleDomChanges);
// Configuration of the observer
const config = {
attributes: true, // Observe changes to attributes
childList: true, // Observe changes to children
subtree: true // Observe changes to all descendants
};
// Start observing the document body
observer.observe(document.body, config);
console.log('MutationObserver is now observing DOM changes.');
//********************************************************************************************************************** */
function getProblemId(url) {
console.log("f: getProblemId", "url: ", url);
let id = "";
if(!url.includes('problem/')) return "";
if (url.includes("problemset")) {
return url.split("problem/")[1];
}
else{
let items = url.split('/');
let n = items.length;
return items[n-3]+"/"+items[n-1];
}
}