Skip to content

Commit

Permalink
Inject content script at document_start (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored Jun 30, 2024
1 parent 807b993 commit c4ac3ca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Changelog

## v3.1.7
## v3.1.8-beta

<!--Releasenotes start-->
- Fixed a bug where the shuffle button would sometimes not be added to the page if it was opened directly from a new tab.
- Fixed a bug where the playlist created by the extension would sometimes not be renamed correctly.
<!--Releasenotes end-->

## v3.1.7

- The extensions's news page can now be updated with breaking changes or other important information without the need to update the extension itself.
- Fixed some dynamic content on the News page.
- Added a hint in the popup if no channel has yet been visited.
- Removed some unneeded scripts from the extension's pages.
<!--Releasenotes end-->

## v3.1.6

Expand Down
4 changes: 2 additions & 2 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ async function startDOMObserver(event) {
}

async function channelDetectedAction(pageType, channelId, channelName, eventVersion) {
// It might be that we got here after shuffling, in which case we want to check if there is a 'Untitled List' that we can rename
// We do this before anything else to prevent the previous text from showing shortly
// It might be that we got to this page after shuffling, in which case we want to check if there is a 'Untitled List' that we can rename
// We do this before anything else to prevent the previous text from showing
if (pageType === "video") {
tryRenameUntitledList();
}
Expand Down
22 changes: 16 additions & 6 deletions src/domManipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,26 @@ export function buildShuffleButton(pageType, channelId, eventVersion, clickHandl
});
}

// With the way that YouTube handles navigation, the playlist title somehow won't get updated correctly when navigating if we change it here at any point
// So we need to change it back if the user moves to a different playlist
export function tryRenameUntitledList() {
let mainPlaylistElement = document.querySelector('ytd-playlist-panel-renderer#playlist.style-scope.ytd-watch-flexy').querySelector('yt-formatted-string.title.style-scope.ytd-playlist-panel-renderer');
let collapsedPlaylistElement = document.querySelector('ytd-playlist-panel-renderer#playlist.style-scope.ytd-watch-flexy').querySelector('yt-formatted-string.byline-title.style-scope.ytd-playlist-panel-renderer');
export function tryRenameUntitledList(attempt = 1) {
let mainPlaylistElement = document.querySelector('ytd-playlist-panel-renderer#playlist.style-scope.ytd-watch-flexy')?.querySelector('yt-formatted-string.title.style-scope.ytd-playlist-panel-renderer');
let collapsedPlaylistElement = document.querySelector('ytd-playlist-panel-renderer#playlist.style-scope.ytd-watch-flexy')?.querySelector('yt-formatted-string.byline-title.style-scope.ytd-playlist-panel-renderer');

// Retry this a few times, in case the element has not yet loaded in
if (!mainPlaylistElement || !collapsedPlaylistElement) {
if (attempt <= 10) {
setTimeout(() => tryRenameUntitledList(attempt + 1), 200);
return;
} else {
return;
}
}

for (let playlistType of [mainPlaylistElement, collapsedPlaylistElement]) {
if (playlistType && window.location.href.includes("&list=TL") && playlistType.title == "Untitled List") {
playlistType.innerText = "Random YouTube Video - Playlist";
playlistType.title = "This playlist is unlisted, temporary and cannot be saved. Until it is removed by YouTube (which will happen automatically), you can revisit it using the link in the URL bar.";
playlistType.title = "This playlist is unlisted, temporary and cannot be saved. You can visit it using the link in the URL bar until it is removed by YouTube, which will happen automatically.";
// With the way that YouTube handles navigation, the playlist title somehow won't get updated correctly when navigating if we change it here at any point
// So we need to change it back if the user moves to a different playlist
} else if (playlistType) {
playlistType.innerText = playlistType.title;
}
Expand Down
4 changes: 2 additions & 2 deletions static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Random YouTube Video",
"description": "Customize, shuffle and play random videos from any YouTube channel.",
"version": "3.1.7",
"version_name": "3.1.7",
"version_name": "3.1.8-beta",
"manifest_version": 3,
"content_scripts": [
{
Expand All @@ -12,7 +12,7 @@
"matches": [
"*://*.youtube.com/*"
],
"run_at": "document_end"
"run_at": "document_start"
}
],
"permissions": [
Expand Down

0 comments on commit c4ac3ca

Please sign in to comment.