Skip to content

Commit c237a7a

Browse files
authored
Merge pull request #3 from Jaimepas77/addCategories
Añadir información de las categorías a las que pertenece cada problema.
2 parents b66dc5e + 33c20ee commit c237a7a

File tree

4 files changed

+194
-9
lines changed

4 files changed

+194
-9
lines changed

manifest.json

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"name": "Acepta el Reto for u!",
44
"description": "Chrome extension to highlight solved problems on aceptaelreto.com",
5-
"version": "1.0.4",
5+
"version": "1.0.5",
66
"action": {
77
"default_popup": "popup/hello.html",
88
"default_icon": {
@@ -16,19 +16,28 @@
1616
"content_scripts": [
1717
{
1818
"js": [
19-
"scripts/content.js",
20-
"scripts/getters.js"
19+
"scripts/getters.js",
20+
"scripts/content.js"
2121
],
2222
"matches": [
23-
"*://aceptaelreto.com/problems/*"
23+
"*://aceptaelreto.com/problems/*"
2424
]
2525
},
2626
{
2727
"js": [
28-
"scripts/hyperlink.js"
28+
"scripts/getters.js",
29+
"scripts/problem.js"
2930
],
3031
"matches": [
31-
"*://aceptaelreto.com/24en23/*/clasificacion.php"
32+
"*://aceptaelreto.com/problem/statement.php*"
33+
]
34+
},
35+
{
36+
"js": [
37+
"scripts/hyperlink.js"
38+
],
39+
"matches": [
40+
"*://aceptaelreto.com/24en23/*/clasificacion.php"
3241
]
3342
}
3443
],

scripts/getters.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function isTried(problemId, userID) {
4040
}
4141

4242
async function getUserID(username) {
43-
console.log("New username: " + username);
43+
// console.log("New username: " + username);
4444
const baseSearchUrl = "https://aceptaelreto.com/bin/search.php?search_query=${username}&commit=searchUser&search_currentPage=%2Fuser%2Fprofile.php";
4545
url = baseSearchUrl.replace("${username}", username);
4646

@@ -70,8 +70,50 @@ async function getLastError(problemId, userID) {
7070
return submissions.submission[0].result;
7171
}
7272

73+
async function getProblemCategories(problemId) {
74+
let category_problems_url = "https://aceptaelreto.com/ws/cat/${categoryId}/problems";
75+
let contained_categories = [];
76+
77+
// Scan the categories (start from 2, stop when null returned)
78+
let categoryId = 2;
79+
let problem_list = null;
80+
81+
let not_found_counter = 0;
82+
while(not_found_counter < 10) {
83+
// Get the problems in the category
84+
request = await fetch(category_problems_url.replace("${categoryId}", categoryId));
85+
if (request.status === 404) {
86+
// console.log("Category not found: " + categoryId);
87+
not_found_counter++;
88+
categoryId++;
89+
continue;
90+
}
91+
problem_list = await request.json();
92+
// console.log("Problems: " + problem_list.problem.length);
93+
94+
// Add the category to the list if the preblemId is in the list of problems
95+
if (problem_list.problem.some(elem => elem.num === problemId)) {
96+
contained_categories.push(categoryId);
97+
}
98+
99+
// Increment the category ID
100+
categoryId++;
101+
}
102+
// console.log("Contained categories: " + contained_categories);
103+
return contained_categories;
104+
}
105+
106+
async function getCategoryData(categoryId) {
107+
let category_name_url = "https://aceptaelreto.com/ws/cat/${categoryId}";
108+
category_name_url = category_name_url.replace("${categoryId}", categoryId);
109+
const request = await fetch(category_name_url);
110+
const category_data = await request.json();
111+
// console.log("Category data: " + category_data.name);
112+
return category_data;
113+
}
114+
73115
try {
74-
module.exports = { isAC, isTried, getUserID, getLastError };
116+
module.exports = { isAC, isTried, getUserID, getLastError, getProblemCategories, getCategoryData };
75117
}
76118
catch (e) {
77119
// Do nothing, this is for testing purposes

scripts/getters.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { isAC, isTried, getUserID, getLastError } = require('./getters');
1+
const { isAC, isTried, getUserID, getLastError, getProblemCategories, getCategoryData } = require('./getters');
22

33
test('isAC: problem 200 of jjjjjjjp022', async () => {
44
const ret = await isAC(200, 17715);
@@ -29,3 +29,14 @@ test('getLastError: problem 331 of elferni', async () => {
2929
const ret = await getLastError(331, 8);
3030
expect(ret).toBe('WA');
3131
});
32+
33+
test('getProblemCategories: problem 100', async () => {
34+
const ret = await getProblemCategories(100);
35+
expect(ret.length).toBe(5);
36+
}, 100000);
37+
38+
test('getCategoryData: category 6', async () => {
39+
const ret = await getCategoryData(6);
40+
expect(ret.name).toBe('Bucles simples');
41+
expect(ret.desc).toBe('for\'s, while\'s o do-while\'s sin anidamiento.');
42+
});

scripts/problem.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
async function updateCategories() {
2+
let problemId = parseInt(await getProblemId());
3+
4+
// Load cached categories
5+
let problem_categories = await new Promise((resolve) => {
6+
chrome.storage.local.get("problemCategories", function (data) {
7+
resolve(data.problemCategories);
8+
});
9+
});
10+
// console.log("Categories from storage: ", problem_categories);
11+
12+
// Update categories
13+
let categories = [];
14+
let new_categories = getProblemCategories(problemId);
15+
if (problem_categories !== undefined && problem_categories[problemId] !== undefined) { // If categories are found in storage
16+
// console.log("Categories from storage: ", problem_categories);
17+
categories = problem_categories[problemId];
18+
19+
insertCategories(categories);
20+
}
21+
else { // If categories are not found in storage
22+
// console.log("No categories found in storage");
23+
// If no categories are found, create a new Map
24+
if (problem_categories === undefined) {
25+
problem_categories = new Map();
26+
}
27+
categories = await new_categories;
28+
// console.log("Categories from API: ", categories);
29+
problem_categories[problemId] = categories;
30+
31+
insertCategories(categories);
32+
}
33+
34+
if (await new_categories.length !== categories.length) {
35+
// Update categories in storage
36+
problem_categories[problemId] = await new_categories;
37+
// console.log("Updating categories in storage: ", problem_categories);
38+
chrome.storage.local.set({ problemCategories: problem_categories });
39+
}
40+
}
41+
42+
async function insertCategories(categories) {
43+
let categories_data = [];
44+
for (let i = 0; i < categories.length; i++) {
45+
let cat = await getCategoryData(categories[i]);
46+
categories_data.push(cat);
47+
}
48+
49+
const categoriesDiv = document.getElementById("content").children[0].children[0];
50+
51+
// Create container
52+
const categoriesContainer = document.createElement("div");
53+
categoriesContainer.id = "categoriesDiv";
54+
categoriesContainer.className = "card-body";
55+
categoriesContainer.style = `
56+
margin-top: 10px;
57+
margin-bottom: 10px;
58+
padding: 2px;
59+
background-color:rgb(163, 163, 163);
60+
border-radius: 5px;
61+
`;
62+
63+
// Accordion + Tags HTML
64+
categoriesContainer.innerHTML = `
65+
<div>
66+
<button id="accordionToggle" style="
67+
width: 100%;
68+
text-align: left;
69+
padding: 10px;
70+
font-size: 16px;
71+
background-color:rgb(0, 97, 136);
72+
color: white;
73+
border: none;
74+
border-radius: 4px;
75+
cursor: pointer;
76+
">
77+
Categorías ▼
78+
</button>
79+
<div id="accordionContent" style="
80+
display: none;
81+
padding: 10px;
82+
border: 1px solid #ddd;
83+
border-top: none;
84+
background-color: rgb(245, 245, 245);
85+
border-radius: 0 0 4px 4px;
86+
margin-top: -4px;
87+
">
88+
${categories_data.map(cat => `<span
89+
style="
90+
display: inline-block;
91+
background-color:rgb(198, 233, 240);
92+
color: #333;
93+
padding: 5px 10px;
94+
border-radius: 12px;
95+
font-size: 13px;
96+
margin: 3px;
97+
">
98+
<a href="https://aceptaelreto.com/problems/categories.php?cat=${cat.id}" target="_blank"
99+
style="color: rgb(0, 0, 0); underline: black;"\
100+
>${cat.name}</a>
101+
</span>`).join('')}
102+
</div>
103+
</div>
104+
`;
105+
106+
// Insert into the page
107+
categoriesDiv.insertBefore(categoriesContainer, categoriesDiv.children[1]);
108+
109+
// Toggle logic
110+
document.getElementById("accordionToggle").addEventListener("click", () => {
111+
const content = document.getElementById("accordionContent");
112+
content.style.display = content.style.display === "block" ? "none" : "block";
113+
});
114+
115+
}
116+
117+
async function getProblemId() {
118+
const url = window.location.href;
119+
const problemId = url.split("=")[1];
120+
return problemId;
121+
}
122+
123+
updateCategories();

0 commit comments

Comments
 (0)