Skip to content

Commit cb4051c

Browse files
committed
fix show/hide examples, difficulty, rating not toggling instantly
1 parent 36c514e commit cb4051c

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

src/background/background.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ chrome.runtime.onInstalled.addListener(() => {
5151
});
5252

5353
chrome.runtime.onMessage.addListener((request) => {
54+
// Direct path for settings updates
55+
if (request.action === 'settingsUpdate') {
56+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
57+
const tab = tabs[0];
58+
if (tab?.id && tab.url?.includes('leetcode.com/problems/')) {
59+
chrome.tabs.sendMessage(tab.id, {
60+
action: 'updateDescription',
61+
title: tab.title || 'title',
62+
isSettingsUpdate: true
63+
});
64+
}
65+
});
66+
return;
67+
}
68+
69+
// Existing message handlers
5470
if (request.action === 'openCompanyPage') {
5571
chrome.storage.local.set({ clickedCompany: request.company });
5672
chrome.tabs.create({

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,16 @@ function setupDescriptionThemeListener() {
371371

372372
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
373373
if (request.action === 'updateDescription') {
374+
// For settings updates, bypass the state checks
375+
if (request.isSettingsUpdate) {
376+
console.log('Updating description tab due to settings change...');
377+
updatePageContent();
378+
return true;
379+
}
380+
374381
// Only detect theme on first load, problem change, or refresh
375382
if (!request.isProblemChange && !request.isRefresh) {
376-
return;
383+
return true;
377384
}
378385

379386
console.log('Updating description tab...');

src/popup/settings.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,45 @@ document.addEventListener('DOMContentLoaded', () => {
115115
if (showRatingIcon) showRatingIcon.textContent = result.showRating ? '✅' : '❌';
116116
});
117117

118+
// Helper function to send messages safely to content script
119+
function safelySendMessage(message: any) {
120+
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
121+
const tabId = tabs[0]?.id;
122+
if (!tabs || !tabs[0] || typeof tabId === 'undefined') {
123+
return;
124+
}
125+
126+
try {
127+
// Send message to background script for settings updates
128+
if (message.action === 'updateDescription') {
129+
chrome.runtime.sendMessage({ action: 'settingsUpdate' });
130+
return;
131+
}
132+
133+
// For other messages, send directly to content script
134+
chrome.tabs.sendMessage(tabId, message, response => {
135+
if (chrome.runtime.lastError) {
136+
console.error('Error sending message:', chrome.runtime.lastError.message);
137+
// Attempt to inject the content script if it's not already injected
138+
chrome.scripting.executeScript({
139+
target: { tabId },
140+
files: ['dist/content-script/update-description-tab.js']
141+
}).then(() => {
142+
// Try sending the message again after injecting the script
143+
setTimeout(() => {
144+
chrome.tabs.sendMessage(tabId, message);
145+
}, 100);
146+
}).catch(err => {
147+
console.error('Error injecting content script:', err);
148+
});
149+
}
150+
});
151+
} catch (error) {
152+
console.error('Error sending message:', error);
153+
}
154+
});
155+
}
156+
118157
// Set up toggle event handlers
119158
const showCompanyTagsBtn = document.getElementById('show-company-tags-btn');
120159
showCompanyTagsBtn && showCompanyTagsBtn.addEventListener('click', function () {
@@ -123,9 +162,7 @@ document.addEventListener('DOMContentLoaded', () => {
123162
chrome.storage.local.set({ showCompanyTags: showCompanyTags }, () => {
124163
const showCompanyTagsIcon = document.getElementById('show-company-tags-icon');
125164
showCompanyTagsIcon && (showCompanyTagsIcon.textContent = showCompanyTags ? '✅' : '❌');
126-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
127-
chrome.tabs.sendMessage(tabs[0].id || 0, { action: 'updateDescription', title: tabs[0].title || 'title' });
128-
});
165+
safelySendMessage({ action: 'updateDescription', title: document.title || 'title' });
129166
});
130167
});
131168
});
@@ -137,10 +174,7 @@ document.addEventListener('DOMContentLoaded', () => {
137174
chrome.storage.local.set({ showExamples: showExamples }, () => {
138175
const showExamplesIcon = document.getElementById('show-examples-icon');
139176
showExamplesIcon && (showExamplesIcon.textContent = showExamples ? '✅' : '❌');
140-
});
141-
// Manually trigger the update description after toggling
142-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
143-
chrome.tabs.sendMessage(tabs[0].id || 0, { action: 'updateDescription', title: tabs[0].title || 'title' });
177+
safelySendMessage({ action: 'updateDescription', title: document.title || 'title' });
144178
});
145179
});
146180
});
@@ -152,10 +186,7 @@ document.addEventListener('DOMContentLoaded', () => {
152186
chrome.storage.local.set({ showDifficulty: showDifficulty }, () => {
153187
const showDifficultyIcon = document.getElementById('show-difficulty-icon');
154188
if (showDifficultyIcon) showDifficultyIcon.textContent = showDifficulty ? '✅' : '❌';
155-
});
156-
// Manually trigger the update description after toggling
157-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
158-
chrome.tabs.sendMessage(tabs[0].id || 0, { action: 'updateDescription', title: tabs[0].title || 'title' });
189+
safelySendMessage({ action: 'updateDescription', title: document.title || 'title' });
159190
});
160191
});
161192
});
@@ -167,10 +198,7 @@ document.addEventListener('DOMContentLoaded', () => {
167198
chrome.storage.local.set({ showRating: showRating }, () => {
168199
const showRatingIcon = document.getElementById('show-rating-icon');
169200
if (showRatingIcon) showRatingIcon.textContent = showRating ? '✅' : '❌';
170-
});
171-
// Manually trigger the update description after toggling
172-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
173-
chrome.tabs.sendMessage(tabs[0].id || 0, { action: 'updateDescription', title: tabs[0].title || 'title' });
201+
safelySendMessage({ action: 'updateDescription', title: document.title || 'title' });
174202
});
175203
});
176204
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy