Skip to content

Instantly share code, notes, and snippets.

@ckalegi
Created October 4, 2020 22:10
Show Gist options
  • Save ckalegi/f6a23b046af9bdb887a334df4c226a4b to your computer and use it in GitHub Desktop.
Save ckalegi/f6a23b046af9bdb887a334df4c226a4b to your computer and use it in GitHub Desktop.
LDS Player Controls
/* LDS Player Controls
* @ckalegi
*
* Preview tracks quicker by seeking through and marking for download with keyboard commands
*
* USE:
* Open folder containing tracks
* Up/Down arrows - previous/next song
* Left/Right arrows - Seek forward/back 10 seconds
* Space - Play/pause
* Shift - Toggle 'download' state for track
*/
// Limits functions to website
if (document.location.origin == 'https://www.livedjservice.com'){
function nextTrack() {
// If nothing's been loaded
if (!document.querySelector('.track_record.playing')){
// Play the first one
var firstTrack = document.querySelectorAll('.track_record')[0];
firstTrack.childNodes[3].click();
} else {
// Play the next one
var nextTrack = document.querySelector('.track_record.playing').nextElementSibling;
nextTrack.childNodes[3].click();
}
}
function prevTrack() {
var firstTrackId = document.querySelectorAll('.track_record')[0].parentElement.children[2].id;
var hasPlay = !!document.querySelector('.track_record.playing');
// if nothing's played yet
if (!hasPlay){
return false
}
// if you're at the top
else if (document.querySelector('.track_record.playing').id == firstTrackId) {
return false
}
// all good
else {
document.querySelector('.track_record.playing').previousElementSibling.childNodes[3].click();
}
}
function seek(direction) {
var seekTo;
var total = soundManager.sounds.ldsPlayer.duration;
var current = soundManager.sounds.ldsPlayer.position;
if (direction == 'prev'){ seekTo = parseInt(current)-10000; }
if (direction == 'fwd') { seekTo = parseInt(current)+10000; }
if (seekTo < total && seekTo > 0){
soundManager.sounds.ldsPlayer.setPosition(seekTo)
} else {
console.log("can't skip beyond the start or end")
}
}
function addDl(){
document.querySelector('.track_record.playing > div.action_btns > div.download_btn').click();
}
function togglePlay(e){
// prevent play/pause when typing in search box
if (e.target == document.body){
e.preventDefault();
document.getElementById('playtoggle').click()
}
}
function registerBind(e) {
let key = e.which;
if (key == 40) { nextTrack() }
if (key == 39) { seek('fwd') }
if (key == 38) { prevTrack() }
if (key == 37) { seek('prev') }
if (key == 16) { addDl() }
if (key == 32) { togglePlay(e) }
}
window.addEventListener('keydown', registerBind);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment