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 c8f3eaf

Browse files
committed
fix company tags not adding sometimes
1 parent a66d66f commit c8f3eaf

File tree

1 file changed

+99
-14
lines changed

1 file changed

+99
-14
lines changed

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

Lines changed: 99 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,72 @@ function showCompanyTags(problemTitle: string) {
154154
return;
155155
}
156156

157-
if (companyTagContainer) {
158-
while (companyTagContainer.firstChild) {
159-
companyTagContainer.firstChild.remove();
160-
}
161-
} else {
162-
companyTagContainer = document.createElement('div');
163-
companyTagContainer.id = 'companyTagContainer';
164-
companyTagContainer.style.display = 'flex';
165-
companyTagContainer.style.flexDirection = 'row';
166-
companyTagContainer.style.marginBottom = '20px';
167-
companyTagContainer.style.gap = '5px';
157+
// Try to find the description element with retries
158+
const maxRetries = 10;
159+
const baseDelay = 300;
160+
let retryCount = 0;
168161

162+
const tryInsertCompanyTags = () => {
169163
const description = document.getElementsByClassName('elfjS')[0];
170-
if (description) {
164+
165+
if (!description && retryCount < maxRetries) {
166+
// Use exponential backoff for retry delay
167+
const delay = baseDelay * Math.pow(1.5, retryCount);
168+
retryCount++;
169+
console.log(`Attempt ${retryCount}: Waiting for description element to load... Retrying in ${delay}ms`);
170+
setTimeout(tryInsertCompanyTags, delay);
171+
return;
172+
}
173+
174+
if (!description) {
175+
console.log('Failed to find description element after all retries');
176+
177+
// If still not found, set up a MutationObserver to watch for DOM changes
178+
const observer = new MutationObserver((mutations, obs) => {
179+
const description = document.getElementsByClassName('elfjS')[0];
180+
if (description) {
181+
obs.disconnect(); // Stop observing once we find the element
182+
insertCompanyTags(description);
183+
}
184+
});
185+
186+
// Start observing the document with the configured parameters
187+
observer.observe(document.body, {
188+
childList: true,
189+
subtree: true
190+
});
191+
192+
return;
193+
}
194+
195+
insertCompanyTags(description);
196+
};
197+
198+
const insertCompanyTags = (description: Element) => {
199+
// Check if container already exists
200+
if (companyTagContainer) {
201+
// Clear existing tags
202+
while (companyTagContainer.firstChild) {
203+
companyTagContainer.firstChild.remove();
204+
}
205+
} else {
206+
// Create new container
207+
companyTagContainer = document.createElement('div');
208+
companyTagContainer.id = 'companyTagContainer';
209+
companyTagContainer.style.display = 'flex';
210+
companyTagContainer.style.flexDirection = 'row';
211+
companyTagContainer.style.marginBottom = '20px';
212+
companyTagContainer.style.gap = '5px';
213+
171214
description.insertBefore(companyTagContainer, description.firstChild);
172215
}
173-
}
174216

175-
loadCompanyTags(problemTitle, companyTagContainer);
217+
// Load and inject company tags
218+
loadCompanyTags(problemTitle, companyTagContainer);
219+
};
220+
221+
// Start the retry process
222+
tryInsertCompanyTags();
176223
});
177224
}
178225

@@ -319,3 +366,41 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
319366
return true;
320367
});
321368

369+
// Self-initialization function that runs when the content script loads
370+
function initializeContentScript() {
371+
// Wait for the DOM to be fully loaded
372+
if (document.readyState === 'loading') {
373+
document.addEventListener('DOMContentLoaded', onDOMReady);
374+
} else {
375+
onDOMReady();
376+
}
377+
378+
function onDOMReady() {
379+
// Check if we're on a LeetCode problem page
380+
const isProblemPage = /^https:\/\/leetcode\.com\/problems\/.*\/?/.test(window.location.href);
381+
382+
if (isProblemPage) {
383+
console.log('LeetCode problem page detected, initializing content script...');
384+
385+
// Extract the problem title from the page title
386+
const pageTitle = document.title;
387+
const problemTitle = pageTitle.split('-')[0].trim();
388+
389+
// Detect theme on first load of a problem page
390+
detectAndSyncTheme();
391+
showExamples();
392+
showCompanyTags(problemTitle);
393+
showDifficulty();
394+
showRating(problemTitle);
395+
396+
// Add theme change listener after creating company tags
397+
setupDescriptionThemeListener();
398+
399+
console.log('Content script initialized for problem:', problemTitle);
400+
}
401+
}
402+
}
403+
404+
// Run the initialization
405+
initializeContentScript();
406+

0 commit comments

Comments
(0)

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