Skip to content

Instantly share code, notes, and snippets.

@eternalruler
Last active August 29, 2015 13:56
Show Gist options
  • Save eternalruler/9129143 to your computer and use it in GitHub Desktop.
Save eternalruler/9129143 to your computer and use it in GitHub Desktop.
Play Random Song on Grooveshark
/*
Add a random song to your Grooveshark playlist
Clears queue (not very fast) then adds the first song using a random songID that is valid
*/
(function() {
//clear current queue
function removeSong(cb) {
w = window.Grooveshark.setSongStatusCallback();
if (w && w.status != "none") {
window.Grooveshark.removeCurrentSongFromQueue()
setTimeout(function() {
removeSong(cb);
}, 125);
} else {
cb();
}
}
removeSong(function() {
function next() { //try to load a new songID
var songID = Math.floor(Math.random() * 30000000); //generate a new songID
var o = [songID]; //create array of one songID
window.Grooveshark.addSongsByID(o, null); //add song
var c = 0, //retry count
l = 8, //retry limit
t = 250; //retry timeout
function wait() { //wait for song to have loaded
c++; //increment retry count
if (c < l) { //if not over retry limit
var w = window.Grooveshark.setSongStatusCallback(); //get callback for song status
if (w && w.song !== null && w.song.songID) { //if callback valid and songID matches
console.log(w.song.songID, '==', songID, (w.song.songID == songID));
if (w.song.songID == songID) {
console.log('songID good');
window.Grooveshark.play();
return;
} else {
setTimeout(function() {
console.log('wait');
wait(); //check again
}, t);
}
} else {
setTimeout(function() {
console.log('wait');
wait(); //check again
}, t);
}
} else {
console.log('songID bad');
next(); //too many fails, try new songID
}
}
wait(); //check if song loaded (first time calling wait())
}
next(); //add song (first time calling next())
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment