Created
February 28, 2022 18:16
-
-
Save nmalayev/0093e1bf480aaf718ee9915e58f7d2a8 to your computer and use it in GitHub Desktop.
YouTube Music Likes to Playlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
// } | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.