|
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 | | - |
73 | 1 | chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { |
74 | 2 | if (changeInfo.status === 'complete' && tab.url) { |
75 | 3 | const url = tab.url; |
76 | 4 | let problemUrl = /^https:\/\/leetcode\.com\/problems\/.*\/?/; |
77 | 5 | if (url.match(problemUrl)) { |
78 | | - chrome.storage.local.get(['currentLeetCodeProblemTitle','descriptionTabUpdated','solutionsTabUpdated'], (result) => { |
| 6 | + chrome.storage.local.get(['currentLeetCodeProblemTitle'], (result) => { |
79 | 7 | 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 |
82 | 10 | if (tab.title !== lastTitle) { |
83 | 11 | chrome.storage.local.set({ |
84 | 12 | 'currentLeetCodeProblemTitle': tab.title, |
85 | 13 | 'descriptionTabUpdated': false, |
86 | 14 | 'solutionsTabUpdated': false |
87 | 15 | }); |
88 | | - // If the title has changed, we reset both flags |
89 | | - descriptionTabUpdated = false; |
90 | | - solutionsTabUpdated = false; |
91 | | - } |
92 | 16 |
|
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 | + } |
103 | 46 | } |
104 | 47 | }); |
105 | 48 | } |
|
0 commit comments