Skip to content

Commit

Permalink
Fix description functionality
Browse files Browse the repository at this point in the history
- Update event name from 'ynt-description-data' to 'nmt-description-data' to match extension renaming
- Change timing management: remove timeout and use direct event handling
- Switch from sequential await to promise chain with then()
  • Loading branch information
YouG-o committed Feb 19, 2025
1 parent 1ea6f30 commit ae5bcb0
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/content/descriptionTranslation/descriptionIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ async function handleDescriptionTranslation(): Promise<void> {

const description = await new Promise<string | null>((resolve) => {
const handleDescription = (event: CustomEvent) => {
window.removeEventListener('nmt-description-data', handleDescription as EventListener);
window.removeEventListener('ynt-description-data', handleDescription as EventListener);
resolve(event.detail?.description || null);
};

window.addEventListener('nmt-description-data', handleDescription as EventListener);
window.addEventListener('ynt-description-data', handleDescription as EventListener);

const script = document.createElement('script');
script.src = browser.runtime.getURL('dist/content/descriptionTranslation/descriptionScript.js');
Expand Down Expand Up @@ -152,11 +152,9 @@ function setupDescriptionObserver() {
if (mutation.type === 'attributes' && mutation.attributeName === 'video-id') {
descriptionLog('Video ID changed!');
descriptionCache.clearCurrentDescription(); // Clear cache on video change
// Wait a bit for YouTube to update its data
await new Promise(resolve => setTimeout(resolve, 1000));
// Then wait for description element
await waitForElement('#description-inline-expander');
handleDescriptionTranslation();
waitForElement('#description-inline-expander').then(() => {
refreshDescription();
});
}
}
});
Expand All @@ -181,10 +179,10 @@ function setupDescriptionObserver() {
} else {
const description = await new Promise<string | null>((resolve) => {
const handleDescription = (event: CustomEvent) => {
window.removeEventListener('nmt-description-data', handleDescription as EventListener);
window.removeEventListener('ynt-description-data', handleDescription as EventListener);
resolve(event.detail?.description || null);
};
window.addEventListener('nmt-description-data', handleDescription as EventListener);
window.addEventListener('ynt-description-data', handleDescription as EventListener);
const script = document.createElement('script');
script.src = browser.runtime.getURL('dist/content/descriptionTranslation/descriptionScript.js');
document.documentElement.appendChild(script);
Expand Down

0 comments on commit ae5bcb0

Please sign in to comment.