Skip to content

Instantly share code, notes, and snippets.

@nmalayev
Created February 28, 2022 18:16
Show Gist options
  • Save nmalayev/0093e1bf480aaf718ee9915e58f7d2a8 to your computer and use it in GitHub Desktop.
Save nmalayev/0093e1bf480aaf718ee9915e58f7d2a8 to your computer and use it in GitHub Desktop.
YouTube Music Likes to Playlist
// Define the sleep method and relevant paths.
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const sleepTime = 600;
const count = 0;
// Update playlistName to desired playlist
const playlistName = "#####";
(async () => {
// Capture the list of liked songs.
const likedSongs = document
.querySelector("#contents")
.children.item(0)
.children.item(0).children;
// Loop over each item in the liked songs list.
for (let song of likedSongs) {
try {
// Click the three-dot menu and give it a moment to appear.
const threeDotMenu = song.children.item(5).children.item(1);
threeDotMenu.click();
await sleep(sleepTime);
// Click "add to playlist" and give it a moment to process.
const addToPlaylist = Array.from(
document.querySelectorAll("a[id=navigation-endpoint]")
).find((element) => element.innerText === "Add to playlist");
addToPlaylist.click();
await sleep(sleepTime);
// Select desired playlist.
const playlist = document.querySelector(`[title="${playlistName}"]`);
playlist.click();
await sleep(sleepTime);
} catch (e) {
console.log(song.outerText);
}
// Debug; only process the first n songs.
// count += 1;
// if (count === 3) {
// break;
// }
}
})();
@KazKaze
Copy link

KazKaze commented Jul 16, 2022

Took forever (I should have tried to reduce the sleepTime) but worked like a charm. Thanks a lot ! Tried several but this is the one that worked !

@nmalayev
Copy link
Author

Took forever (I should have tried to reduce the sleepTime) but worked like a charm. Thanks a lot ! Tried several but this is the one that worked !

Just saw your comment! Glad to hear it was helpful! And yes, it does take a while. Unfortunately this is super synchronous and browser based, so it depends on slow user actions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment