|
| 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