Skip to content

Instantly share code, notes, and snippets.

@joeywang4
Forked from TheOnlyWayUp/README.md
Last active March 1, 2024 02:13
Show Gist options
  • Select an option

  • Save joeywang4/fb0c60df23088a5279a9eff9bc64bbf7 to your computer and use it in GitHub Desktop.

Select an option

Save joeywang4/fb0c60df23088a5279a9eff9bc64bbf7 to your computer and use it in GitHub Desktop.
Like all songs in a Youtube Music Playlist

This script likes all the songs in a Youtube Music Playlist at once.

How to use:

  • Visit a Playlist's page on ytm (URL looks like: https://music.youtube.com/playlist?list=...
  • Press ctrl + shift + j. This opens the Developer Console.
  • Copy the script in this gist (That's in script.js)
  • Paste the code into the Developer Console on the ytm Tab, hit enter.
  • Great, you're done!

Star ⭐ this gist if it was useful. Follows to my GitHub Profile are appreciated.

Follow Badge


TheOnlyWayUp © 2024

const interval = 1; // wait time between chunks in seconds
const chunkSize = 1; // number of songs to like at once
// HTML elements selectors
const contentsId = "contents";
const playlistName = "ytmusic-playlist-shelf-renderer";
const qryStr = "yt-button-shape[id='button-shape-like']";
const ytmLog = msg => console.log(`[YTM Liker] ${msg}`);
const getSongs = (notLiked = true) => {
const contents = document.getElementById(contentsId);
const playlist = contents.getElementsByTagName(playlistName)[0];
const els = playlist.querySelectorAll(
notLiked ? qryStr + "[aria-pressed='false']" : qryStr
);
return Array.from(els).map(el => el.getElementsByTagName("button")[0]);
}
const loadAllSongs = async () => {
let lastCount = 0;
while (true) {
const songs = getSongs(false);
const count = songs.length;
ytmLog(`Loaded ${count} songs.`);
if (count === lastCount) {
break;
}
lastCount = count;
songs[count - 1].scrollIntoView();
await sleep(1000);
}
}
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const likeAll = async () => {
await loadAllSongs();
let songs = getSongs();
const total = songs.length;
ytmLog(`Liking ${total} songs...`);
for (let i = 0; i < total; i += chunkSize) {
// scroll to the first song
songs[i].scrollIntoView();
// click like buttons in chunks
ytmLog(`Progress: ${i}/${total}.`);
songs.slice(i, i + chunkSize).forEach(likeBtn => likeBtn.click());
// wait for the next chunk
await sleep(interval * 1000);
}
ytmLog("All songs liked.");
}
likeAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment