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 e4cc30f

Browse files
committed
fix: dynamically opening problem not updatint company tags, video, code on page
1 parent 1086f58 commit e4cc30f

File tree

3 files changed

+47
-90
lines changed

3 files changed

+47
-90
lines changed

‎manifest.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"permissions": [
1616
"storage",
17-
"tabs"
17+
"tabs",
18+
"webNavigation"
1819
],
1920
"host_permissions": [
2021
"https://api.leetcodeapp.com/*"

‎src/background/background.ts‎

Lines changed: 32 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,48 @@
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-
731
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
742
if (changeInfo.status === 'complete' && tab.url) {
753
const url = tab.url;
764
let problemUrl = /^https:\/\/leetcode\.com\/problems\/.*\/?/;
775
if (url.match(problemUrl)) {
78-
chrome.storage.local.get(['currentLeetCodeProblemTitle','descriptionTabUpdated','solutionsTabUpdated'], (result) => {
6+
chrome.storage.local.get(['currentLeetCodeProblemTitle'], (result) => {
797
let lastTitle = result.currentLeetCodeProblemTitle || '';
80-
letdescriptionTabUpdated=result.descriptionTabUpdated||false;
81-
letsolutionsTabUpdated=result.solutionsTabUpdated||false;
8+
9+
// If the title has changed, we need to update both tabs
8210
if (tab.title !== lastTitle) {
8311
chrome.storage.local.set({
8412
'currentLeetCodeProblemTitle': tab.title,
8513
'descriptionTabUpdated': false,
8614
'solutionsTabUpdated': false
8715
});
88-
// If the title has changed, we reset both flags
89-
descriptionTabUpdated = false;
90-
solutionsTabUpdated = false;
91-
}
9216

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' });
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+
}
10346
}
10447
});
10548
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,19 @@ chrome.runtime.onMessage.addListener((request) => {
543543
return;
544544
}
545545

546+
// If forceUpdate is true or this is not a video update, remove existing containers
547+
if (request.forceUpdate) {
548+
const existingContainers = [
549+
'.nav-container',
550+
'.video-container',
551+
'.code-container',
552+
'.language-buttons-container'
553+
].forEach(selector => {
554+
const element = document.querySelector(selector);
555+
if (element) element.remove();
556+
});
557+
}
558+
546559
// Only create nav container if it doesn't exist or if this is not a video update
547560
let existingNavContainer = document.querySelector('.nav-container');
548561
if (!existingNavContainer) {

0 commit comments

Comments
(0)

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