Skip to content

Commit

Permalink
Wait for player and metadata elements before updating page title
Browse files Browse the repository at this point in the history
- Prevent YouTube from re-translating page title
- Wait for essential elements to be fully loaded
- Add proper loading checks for both ytd-player and ytd-watch-metadata
  • Loading branch information
YouG-o committed Feb 9, 2025
1 parent ac5095d commit d5ee8fc
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/content/titleTranslation/mainTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ function updateMainTitleElement(element: HTMLElement, title: string, videoId: st
.filter(node => node.nodeType === Node.TEXT_NODE);

if (textNodes.length > 1) {
mainTitleLog('Multiple text nodes detected, cleaning up');
isUpdating = true;
element.innerText = title;
mainTitleLog('YouTube added an other Title, cleaning it up');
isUpdating = false;
mainTitleLog('Multiple text nodes detected, cleaning up');
}
}
});
Expand All @@ -83,8 +82,25 @@ function updateMainTitleElement(element: HTMLElement, title: string, videoId: st
}

function updatePageTitle(mainTitle: string): void {
//mainTitleLog('Updating page title with:', mainTitle);
document.title = `${mainTitle} - YouTube`;
// Wait for essential elements to be loaded
const waitForLoad = async () => {
const player = document.querySelector('ytd-player');
const metadata = document.querySelector('ytd-watch-metadata');

// Check if elements are ready (both have loading=null)
if (player?.getAttribute('loading') === null &&
metadata?.getAttribute('loading') === null) {
document.title = `${mainTitle} - YouTube`;
mainTitleLog(`Essential elements loaded, updating page title : ${document.title}`);
return;
}

// Try again in 100ms
setTimeout(waitForLoad, 100);
};

// Start waiting for load
waitForLoad();
}


Expand Down

0 comments on commit d5ee8fc

Please sign in to comment.