Skip to content

Instantly share code, notes, and snippets.

@Thomasvdam
Last active October 9, 2023 18:45
Show Gist options
  • Save Thomasvdam/3208bd212ce71dd0bf66db52ac6e9ac8 to your computer and use it in GitHub Desktop.
Save Thomasvdam/3208bd212ce71dd0bf66db52ac6e9ac8 to your computer and use it in GitHub Desktop.
Copy SoundCloud playlist items from one to another.
// Configuration
const WAIT_OPEN_PLAYLIST_POPUP = 1000;
const WAIT_AFTER_ADD_PLAYLIST = 1500;
const TARGET_PLAYLIST_NAME = 'To Do';
// Should the new playlist be in reverse order?
const REVERSE = false;
// Should liked items be skipped?
const SKIP_LIKED = true;
// Implementation
const delta = REVERSE ? -1 : 1;
const nextIndex = (index) => index + delta;
const listItems = Array.from(document.querySelectorAll('.sc-button-group.sc-button-group-small'));
const addItemToPlaylist = (index) => {
const node = listItems[index];
if (!node) {
console.log('Done with all currently loaded items.');
return;
}
if (SKIP_LIKED) {
const hasLikedButton = node.querySelector('.sc-button-like.sc-button-selected');
if (hasLikedButton) {
console.log('Already liked index: ', index);
setTimeout(() => {
addItemToPlaylist(nextIndex(index));
}, 0);
return;
}
}
node.querySelector('.sc-button-more').click();
document.querySelector('button.sc-button-addtoset').click();
setTimeout(() => {
const playlistRow = Array.from(document.querySelectorAll('.addToPlaylistList__item')).find(node => node.querySelector(`[title="${TARGET_PLAYLIST_NAME}"]`));
const playListButton = playlistRow.querySelector('button.addToPlaylistButton:not(.sc-button-selected)');
if (!playListButton) {
console.log('Already added index: ', index);
setTimeout(() => {
addItemToPlaylist(nextIndex(index));
}, 0);
return;
}
playListButton.click();
console.log('Added index: ', index);
setTimeout(() => {
addItemToPlaylist(nextIndex(index));
}, WAIT_AFTER_ADD_PLAYLIST);
}, WAIT_OPEN_PLAYLIST_POPUP);
};
if (REVERSE) {
addItemToPlaylist(listItems.length - 1);
} else {
addItemToPlaylist(0);
}
@samhangster
Copy link

@Thomasvdam
I got it. I had changed the delays to a lower value thinking it would speed it up or something and it wasn't allowing time for it to even read the playlist name. Thank you for your help!

@JupiterJaeden
Copy link

You are an actual god

@hamhimstudio
Copy link

thank you from the bottom of my heart

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