Skip to content

Instantly share code, notes, and snippets.

@BrianLincoln
Last active July 15, 2024 17:56
Show Gist options
  • Save BrianLincoln/982bd121c4414d89ff3785af93f65c7a to your computer and use it in GitHub Desktop.
Save BrianLincoln/982bd121c4414d89ff3785af93f65c7a to your computer and use it in GitHub Desktop.
YouTube Music Likes to Playlist

Copy Likes to a playlist in YouTube Music

This is a very hacky solution to copy Liked songs to a playlist since YTM still doesn't have the functionality. I'm using this to copy songs out of YTM to another service, then unsubscribing. Thus, I won't be maintaining it (or ever using it again). It will only work while the YTM interface is the same as it is today (3/6/21) and will break once they make updates.

Steps to use:

  1. Create a new playlist
  2. Go to your Likes page (in chrome, on a desktop or laptop). Scroll to the bottom so all songs are loaded
  3. Open Chrome's dev tools (F12 on windows), go to the console
  4. Paste the script below. Edit the first line, replace "YOUR_PLAYLIST_NAME" with your playlist's name
  5. Press enter

If it's working, you will see it log out the song titles and (1 of x).

let TARGET_PLAYLIST = "YOUR_PLAYLIST_NAME";

let songElements = document.querySelectorAll(
  "#contents > .ytmusic-playlist-shelf-renderer"
);
let addToPlaylistXpath = "//yt-formatted-string[text()='Add to playlist']";
let playlistXpath = `//yt-formatted-string[text()='${TARGET_PLAYLIST}']`;

function isHidden(element) {
  return element.offsetParent === null;
}

function waitForElementToDisplay(xpath, callback) {
  const timeout = 5000
  const checkFrequency = 30

  var startTimeInMs = Date.now();
  (function loopSearch() {
    const element = document.evaluate(
      xpath,
      document,
      null,
      XPathResult.FIRST_ORDERED_NODE_TYPE,
      null
    ).singleNodeValue;

    if (element && !isHidden(element)) {
      callback(element);
      return;
    } else {
      setTimeout(function () {
        if (timeout && Date.now() - startTimeInMs > timeout) return;
        loopSearch();
      }, checkFrequency);
    }
  })();
}

function copySong(songElement, index) {
  const dotsElement = songElement.querySelector("#button");
  const songTitle = songElement.querySelector(".yt-simple-endpoint").textContent;

  console.log(`Copying: ${songTitle} (${index} of ${songElements.length})`);

  // click dot action menu
  dotsElement.click();

  waitForElementToDisplay(addToPlaylistXpath, (addToPlaylistElement) => {
    // click "Add to playlist"
    addToPlaylistElement.click();

    waitForElementToDisplay(playlistXpath, (playlistElement) => {
      // click target playlist
      playlistElement.click();

      document.body.click();

      // call function again
      if (index <= songElements.length) {
        const nextIndex = index + 1;
        copySong(songElements[nextIndex], nextIndex);
      }
    });
  });
}

copySong(songElements[0], 0)
@Tom-Whi
Copy link

Tom-Whi commented Aug 20, 2023

I guess this doesn't work anymore? I keep getting an undefined and it sits at 1 of x

This work Liked music to playlist

I have too many tracks in my Liked Music so your "nice but manual" method won't work for me, but thanks for the hint. I'll keep looking for a scripted way to automate the full list.

@DwiBlackList
Copy link

@Tom-Whi @Mrkas121 (sorry for tag)

Hey i found treasure

go to https://chrome.google.com/webstore/detail/multiselect-for-youtube/gpgbiinpmelaihndlegbgfkmnpofgfei/related
Download , install , and eneble

image

go to the playlist , scroll to bottom , select all videos , three dots , save to playlist
and done

@Tom-Whi
Copy link

Tom-Whi commented Sep 25, 2023

Hey i found treasure

You are an absolute treasure...!!! Thank you so much for sharing. That worked so much easier than I was expecting :-)

@dangerousdan
Copy link

You can just select the checkbox next to the first song, scroll to the bottom, then shift click on the last. Then add to playlist. No plugin or reverse engineering needed.

It seems as though you have to click on a playlist from the "All playlists" list though. If you try to add to a playlist from the "Recent" list, then it doesn't work

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