Skip to content

Commit b1799f0

Browse files
committed
add amazon questions when the company tag is clicked
1 parent ea499d9 commit b1799f0

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

src/background/background.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
5959
}
6060
});
6161

62+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
63+
if (request.action === "openCompanyPage") {
64+
chrome.tabs.create({
65+
url: chrome.runtime.getURL("src/popup/company.html"),
66+
active: true
67+
}, function (tab) {
68+
chrome.tabs.onUpdated.addListener(function listener(tabId, changedProps) {
69+
// When the tab is done loading
70+
if (tabId == tab.id && changedProps.status == "complete") {
71+
chrome.tabs.sendMessage(tabId, request);
72+
chrome.tabs.onUpdated.removeListener(listener);
73+
}
74+
});
75+
});
76+
}
77+
});
78+
79+
6280
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
6381
// If descriptions tab is opened or updated, update the description
6482
let urlPattern = /^https:\/\/leetcode\.com\/problems\/.*\/(description\/)?/;

src/content-script/update-description-tab.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,21 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
9797
chrome.storage.local.get(['leetcodeProblems'], (result) => {
9898
const problem = result.leetcodeProblems.questions.find((problem: problem) => problem.title === problemTitle);
9999
if (problem.companies && problem.companies.length > 0) {
100-
// slice the array to get only the first five companies
101100
const topCompanies = problem.companies.slice(0, 5);
102-
103101
// create a button for each company
104102
topCompanies.forEach((company: { name: string; score: any; }) => {
105103
const button = document.createElement('button');
104+
// opens the company page when the button is clicked
105+
button.onclick = () => {
106+
chrome.runtime.sendMessage({
107+
// passes the company name and score to the background script
108+
action: 'openCompanyPage', company: company
109+
})
110+
}
111+
106112
button.style.display = 'flex';
107-
button.style.alignItems = 'center'; // align items vertically in the center
108-
button.style.justifyContent = 'center'; // align items horizontally in the center
113+
button.style.alignItems = 'center';
114+
button.style.justifyContent = 'center';
109115

110116
const icon = document.createElement('img');
111117
icon.src = `https://logo.clearbit.com/${company.name.toLowerCase().replace(/\s/g, '')}.com`; // replace spaces with nothing

src/popup/company.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>LeetCode Solutions</title>
6+
</head>
7+
8+
<body>
9+
<h1 id="test">Company tags</h1>
10+
<table id="solutionTable">
11+
<tr>
12+
<th>Problem Number</th>
13+
<th>Problem Title</th>
14+
<th>Score</th>
15+
</tr>
16+
</table>
17+
<script src="../../dist/popup/company.js"></script>
18+
</body>
19+
20+
</html>

src/popup/company.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
3+
function main() {
4+
chrome.storage.local.get("leetcodeProblems", function (data) {
5+
const companyName = "Amazon";
6+
let solutions = [];
7+
8+
console.log(data);
9+
10+
data.leetcodeProblems.questions.forEach(question => {
11+
if (!question.companies) return;
12+
question.companies.forEach(company => {
13+
if (company.name === companyName) {
14+
solutions.push({
15+
id: question.frontend_id,
16+
title: question.title,
17+
score: company.score,
18+
url: `https://leetcode.com/problems/${question.title.replace(/\s/g, '-')}/`
19+
});
20+
}
21+
});
22+
});
23+
24+
const table = document.getElementById("solutionTable");
25+
solutions.forEach(solution => {
26+
const row = table.insertRow(-1);
27+
row.insertCell(0).innerText = solution.id;
28+
const titleCell = row.insertCell(1);
29+
titleCell.innerHTML = `<a href="${solution.url}" target="_blank">${solution.title}</a>`;
30+
row.insertCell(2).innerText = solution.score;
31+
});
32+
});
33+
34+
document.getElementById("test")?.textContent = "Hello World!";
35+
}
36+
37+
/* Run the script */
38+
main();

0 commit comments

Comments
 (0)