Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3f47d90

Browse files
committed
fix company tags not opening top problems page
1 parent 4957083 commit 3f47d90

File tree

2 files changed

+92
-36
lines changed

2 files changed

+92
-36
lines changed

‎src/background/background.ts‎

Lines changed: 90 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,107 @@
1+
// Helper function to get or create user ID
2+
function getRandomToken(): string {
3+
const randomPool = new Uint8Array(32);
4+
crypto.getRandomValues(randomPool);
5+
return Array.from(randomPool)
6+
.map(b => b.toString(16).padStart(2, '0'))
7+
.join('');
8+
}
9+
10+
// Load problem data & default settings on install
11+
chrome.runtime.onInstalled.addListener(() => {
12+
// Generate and store user ID if it doesn't exist
13+
chrome.storage.sync.get('userId', function (items) {
14+
if (!items.userId) {
15+
const userId = getRandomToken();
16+
chrome.storage.sync.set({ userId: userId });
17+
}
18+
});
19+
20+
// Load JSON file of problem data into storage
21+
const leetcodeProblems = chrome.runtime.getURL('src/assets/data/problem_data.json');
22+
fetch(leetcodeProblems)
23+
.then((response) => response.json())
24+
.then((data) => {
25+
chrome.storage.local.set({ leetcodeProblems: data });
26+
})
27+
.catch((error) => {
28+
console.error(error);
29+
});
30+
31+
// Load problems by company JSON file into storage
32+
const companyProblems = chrome.runtime.getURL('src/assets/data/problems_by_company.json');
33+
fetch(companyProblems)
34+
.then((response) => response.json())
35+
.then((data) => {
36+
chrome.storage.local.set({ companyProblems: data });
37+
})
38+
.catch((error) => {
39+
console.error(error);
40+
});
41+
42+
// Load default settings
43+
chrome.storage.local.set({ fontSize: 12 }); // Default to small display size
44+
chrome.storage.local.set({ showExamples: true });
45+
chrome.storage.local.set({ showDifficulty: true });
46+
chrome.storage.local.set({ showRating: true });
47+
chrome.storage.local.set({ showCompanyTags: true });
48+
// Set default theme to auto mode and default to dark
49+
chrome.storage.local.set({ isDarkTheme: true }); // Default to dark theme
50+
chrome.storage.local.set({ themeMode: 'auto' });
51+
});
52+
53+
chrome.runtime.onMessage.addListener((request) => {
54+
if (request.action === 'openCompanyPage') {
55+
chrome.storage.local.set({ clickedCompany: request.company });
56+
chrome.tabs.create({
57+
url: chrome.runtime.getURL('src/problems-by-company/company.html'),
58+
active: true,
59+
}, function (tab) {
60+
// Remove the listener once the tab is loaded
61+
const listener = function (tabId: number, changedProps: any) {
62+
if (tabId == tab.id && changedProps.status == 'complete') {
63+
chrome.tabs.sendMessage(tabId, request);
64+
chrome.tabs.onUpdated.removeListener(listener);
65+
}
66+
};
67+
// Attach the listener
68+
chrome.tabs.onUpdated.addListener(listener);
69+
});
70+
}
71+
});
72+
173
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
274
if (changeInfo.status === 'complete' && tab.url) {
375
const url = tab.url;
476
let problemUrl = /^https:\/\/leetcode\.com\/problems\/.*\/?/;
577
if (url.match(problemUrl)) {
6-
chrome.storage.local.get(['currentLeetCodeProblemTitle'], (result) => {
78+
chrome.storage.local.get(['currentLeetCodeProblemTitle','descriptionTabUpdated','solutionsTabUpdated'], (result) => {
779
let lastTitle = result.currentLeetCodeProblemTitle || '';
8-
9-
// If the title has changed, we need to update both tabs
80+
letdescriptionTabUpdated=result.descriptionTabUpdated||false;
81+
letsolutionsTabUpdated=result.solutionsTabUpdated||false;
1082
if (tab.title !== lastTitle) {
1183
chrome.storage.local.set({
1284
'currentLeetCodeProblemTitle': tab.title,
1385
'descriptionTabUpdated': false,
1486
'solutionsTabUpdated': false
1587
});
88+
// If the title has changed, we reset both flags
89+
descriptionTabUpdated = false;
90+
solutionsTabUpdated = false;
91+
}
1692

17-
// Force update both tabs when problem changes
18-
chrome.tabs.sendMessage(tabId, {
19-
action: 'updateDescription',
20-
title: tab.title || 'title',
21-
forceUpdate: true
22-
});
23-
chrome.tabs.sendMessage(tabId, {
24-
action: 'updateSolutions',
25-
title: tab.title || 'title',
26-
forceUpdate: true
27-
});
28-
} else {
29-
// If we're on the same problem but URL changed, update appropriate tab
30-
let descriptionUrl = /^https:\/\/leetcode\.com\/problems\/.*\/(description\/)?/;
31-
let solutionsUrl = /^https:\/\/leetcode\.com\/problems\/.*\/solutions\/?/;
32-
33-
if (url.match(descriptionUrl)) {
34-
chrome.tabs.sendMessage(tabId, {
35-
action: 'updateDescription',
36-
title: tab.title || 'title'
37-
});
38-
}
39-
40-
if (url.match(solutionsUrl)) {
41-
chrome.tabs.sendMessage(tabId, {
42-
action: 'updateSolutions',
43-
title: tab.title || 'title'
44-
});
45-
}
93+
let descriptionUrl = /^https:\/\/leetcode\.com\/problems\/.*\/(description\/)?/;
94+
if (!descriptionTabUpdated && url.match(descriptionUrl)) {
95+
chrome.storage.local.set({ 'descriptionTabUpdated': true });
96+
chrome.tabs.sendMessage(tabId, { action: 'updateDescription', title: tab.title || 'title' });
97+
}
98+
99+
let solutionsUrl = /^https:\/\/leetcode\.com\/problems\/.*\/solutions\/?/;
100+
if (url.match(solutionsUrl)) {
101+
chrome.storage.local.set({ 'solutionsTabUpdated': true });
102+
chrome.tabs.sendMessage(tabId, { action: 'updateSolutions', title: tab.title || 'title' });
46103
}
47104
});
48105
}
49106
}
50-
});
51-
107+
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
269269
button.classList.add('company-tag');
270270
button.onclick = () => {
271271
chrome.runtime.sendMessage({
272-
action: 'openCompanyPage',
273-
company: company.name,
272+
action: 'openCompanyPage', company: company.name,
274273
});
275274
};
276275

@@ -283,6 +282,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
283282
button.style.letterSpacing = '.5px';
284283
button.style.transition = 'all 0.2s ease';
285284
button.style.cursor = 'pointer';
285+
286286

287287
chrome.storage.local.get(['isDarkTheme'], (result) => {
288288
const isDark = result.isDarkTheme;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /