|
| 1 | +// define solutions and companyName outside of the functions so they can be accessed globally |
| 2 | +let solutions = [] as { id: number, title: string, score: number, url: string }[]; |
| 3 | +let companyName = "Amazon"; |
| 4 | + |
| 5 | +function main() { |
| 6 | + |
| 7 | + |
| 8 | + chrome.storage.local.get("clickedCompany", function (data) { |
| 9 | + companyName = data.clickedCompany; |
| 10 | + document.getElementById("title")!.textContent = companyName; |
| 11 | + document.title = companyName + "'s favorite problems" |
| 12 | + }); |
| 13 | + |
| 14 | + addCompanyProblems("Score"); |
| 15 | + |
| 16 | + // attach click listeners to table headers for sorting |
| 17 | + document.getElementById('#')!.addEventListener('click', () => sortBy('#')); |
| 18 | + document.getElementById('Title')!.addEventListener('click', () => sortBy('Title')); |
| 19 | + document.getElementById('Score')!.addEventListener('click', () => sortBy('Score')); |
| 20 | +} |
| 21 | + |
| 22 | +// Adds the company problems by sorting method |
| 23 | +function addCompanyProblems(sortMethod: string) { |
| 24 | + chrome.storage.local.get("leetcodeProblems", function (data) { |
| 25 | + data.leetcodeProblems.questions.forEach(question => { |
| 26 | + if (!question.companies) return; |
| 27 | + question.companies.forEach(company => { |
| 28 | + if (company.name === companyName) { |
| 29 | + solutions.push({ |
| 30 | + id: question.frontend_id, |
| 31 | + title: question.title, |
| 32 | + score: company.score, |
| 33 | + url: `https://leetcode.com/problems/${question.title.replace(/\s/g, '-')}/` |
| 34 | + }); |
| 35 | + } |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + const table = document.getElementById("solutionTable"); |
| 40 | + |
| 41 | + if (sortMethod === "Score") { |
| 42 | + solutions.sort((a, b) => b.score - a.score); |
| 43 | + } |
| 44 | + |
| 45 | + solutions.forEach(solution => { |
| 46 | + const row = table!.insertRow(-1); |
| 47 | + row.insertCell(0).innerText = solution.id; |
| 48 | + const titleCell = row.insertCell(1); |
| 49 | + titleCell.innerHTML = `<a href="${solution.url}" target="_blank">${solution.title}</a>`; |
| 50 | + row.insertCell(2).innerText = solution.score; |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +function sortBy(column: string) { |
| 56 | + if (column === 'Score') { |
| 57 | + solutions.sort((a, b) => b.score - a.score); |
| 58 | + } |
| 59 | + else if (column === 'Title') { |
| 60 | + solutions.sort((a, b) => a.title.localeCompare(b.title)); |
| 61 | + } |
| 62 | + else { |
| 63 | + solutions.sort((a, b) => a.id - b.id); |
| 64 | + } |
| 65 | + |
| 66 | + // after sorting, you might want to re-render your table |
| 67 | + const table = document.getElementById("solutionTable"); |
| 68 | + |
| 69 | + // remove all existing rows |
| 70 | + while (table.rows.length > 1) { |
| 71 | + table.deleteRow(1); |
| 72 | + } |
| 73 | + |
| 74 | + // add sorted rows |
| 75 | + solutions.forEach(solution => { |
| 76 | + const row = table.insertRow(-1); |
| 77 | + row.insertCell(0).innerText = solution.id; |
| 78 | + const titleCell = row.insertCell(1); |
| 79 | + titleCell.innerHTML = `<a href="${solution.url}" target="_blank">${solution.title}</a>`; |
| 80 | + row.insertCell(2).innerText = solution.score; |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +/* Run the script */ |
| 85 | +main(); |
0 commit comments