Last active
October 11, 2024 23:45
-
-
Save Thomasvdam/3208bd212ce71dd0bf66db52ac6e9ac8 to your computer and use it in GitHub Desktop.
Copy SoundCloud playlist items from one to another.
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
// 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); | |
} |
@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!
You are an actual god
thank you from the bottom of my heart
With these few changes got it to work.
const WAIT_OPEN_PLAYLIST_POPUP = 1500; // Increased the wait time
const WAIT_AFTER_ADD_PLAYLIST = 2000; // Increased the wait time
const TARGET_PLAYLIST_NAME = 'my playlist'; // Changed name to an already saved playlist
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@samhangster The error is thrown on line 46, but actually originates on line 45:
It seems that the script is unable to locate a node in the DOM tree that has the correct title associated with it. This is most likely a typo in the
TARGET_PLAYLIST_NAME
variable, or it's possible that you have a lot of playlists and your target playlist is not on the first 'page' that is loaded. That would require some way to work around that using the filter I think.On second read of your comment, I noticed that you said you clicked something yourself:
This might throw the script off as it was designed to run without intervention. It's all a bit rickety due to all the timeouts, so tiny adjustments can easily break the entire thing.