Last active
September 2, 2020 23:45
-
-
Save thalesmg/e3aef4819dbaf4803944e274d38c9b9a to your computer and use it in GitHub Desktop.
Auto Random Play VGMRips
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
// -*- mode: js2 -*- | |
// ==UserScript== | |
// @name VGMRips Auto Random | |
// @namespace https://userscripts-mirror.org/users/529924 | |
// @include https://vgmrips.net/* | |
// @require https://vgmrips.net/packs/js/jquery-1.11.0.min.js | |
// @author Thales M. G. | |
// @version 1.0.1 | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
var song = null, songDuration = null; | |
function sec2time(s) { | |
var r = ''; | |
r += Math.floor(s / 60.0); | |
r += ':'; | |
var m = Math.floor(s) % 60; | |
if(m < 10) m = '0' + m; | |
r += m; | |
return r; | |
} | |
var volume = .8; | |
function getCookie(cookiename) { | |
// Get name followed by anything except a semicolon | |
var cookiestring = RegExp(cookiename + "=[^;]+").exec(document.cookie); | |
// Return everything after the equal sign, or an empty string if the cookie name not found | |
return decodeURIComponent(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./,"") : ""); | |
} | |
function isWebView() { | |
if(navigator && navigator.userAgent && navigator.userAgent.match(/WebView/)) | |
return true; | |
if(window.location && window.location.href && window.location.href.match(/browser=webview/)) | |
return true; | |
return false; | |
} | |
function clearListeners(path) { | |
var elems = $(path); | |
elems.each((idx) => { | |
var old = elems[idx]; | |
var clone = old.cloneNode(true); | |
old.parentNode.replaceChild(clone, old); | |
}); | |
} | |
jQuery(document).ready(function() { | |
if(isWebView()) { | |
$('table.playlist tr').each(function(i, el) { | |
$(el).find('a.beginPlay').attr('href', $(el).data('vgmurl')); | |
}); | |
return; | |
} | |
clearListeners('#playBtn'); | |
$('#playBtn').click(function() { | |
var gi = $(this).find('.icon'); | |
if(gi.hasClass('icon-pause')) { | |
gi.removeClass('icon-pause').addClass('icon-play'); | |
if(song) song.pause(); | |
} else if(gi.hasClass('icon-play')) { | |
gi.removeClass('icon-play').addClass('icon-pause'); | |
if(song) song.play(); | |
else playRow(); | |
} | |
}); | |
clearListeners('#player .rew'); | |
$('#player .rew').click(function(e) { | |
e.preventDefault(); | |
playPrev(); | |
}); | |
clearListeners('#player .fwd'); | |
$('#player .fwd').click(function(e) { | |
e.preventDefault(); | |
playNext(); | |
}); | |
function beginPlay() { | |
playRow(); | |
} | |
function seekPercent(pct) { | |
if(pct < 0) pct = 0; | |
if(pct > 100) pct = 100; | |
if(song && songDuration) { | |
song.currentTime = songDuration * pct / 100.0; | |
} | |
} | |
var $progress = $('#position'); | |
var pos = $progress.offset(); | |
function seekFunc(ev) { | |
seekPercent((ev.clientX - pos.left) / $progress.width() * 100); | |
ev.preventDefault(); | |
ev.stopPropagation(); | |
} | |
$progress.mousedown(function(ev) { | |
seekFunc(ev); | |
$(document).on('mousemove', seekFunc); | |
$(document).on('mouseup', function(ev) { | |
$(document).off('mousemove', seekFunc); | |
}); | |
}); | |
function progressLoading() { | |
$progress | |
.removeClass('progress-success') | |
.addClass('progress-striped') | |
.addClass('active'); | |
$('#player .loading') | |
.css('width', '100%'); | |
$('#player .playing') | |
.css('width', '0'); | |
} | |
function updateProgress(playing, loaded) { | |
playing = Math.round(playing / songDuration * 100); | |
if(loaded > 100) loaded = 100; | |
var loadingWidth = Math.round(loaded - playing); | |
if(loadingWidth < 0) loadingWidth = 0; | |
$progress | |
.removeClass('progress-striped') | |
.removeClass('progress-success'); | |
$('#player .progress .playing') | |
.css('width', playing + '%'); | |
$('#player .progress .loading') | |
.css('width', loadingWidth + '%'); | |
} | |
var curLoaded = 0; | |
function progressProgress(buf) { | |
curLoaded = buf.length > 0 ? (buf.end(0) / songDuration * 100) : 0; | |
if(song && songDuration) updateProgress(song.currentTime, curLoaded); | |
} | |
function progressUpdate() { | |
if(song && songDuration) updateProgress(song.currentTime, curLoaded); | |
} | |
function playRow(elm) { | |
if(typeof elm == 'undefined') { | |
elm = $('.playlist tr.active'); | |
if(elm.length == 0) elm = $('.playlist tbody tr:first'); | |
} | |
//window.location.hash = elm.data('songurl'); | |
$('.playlist tbody tr').removeClass('active'); | |
elm.addClass('active'); | |
if(song) { | |
song.pause(); | |
delete song; | |
} | |
song = new Audio(elm.data('audiourl')); | |
songDuration = elm.data('duration'); | |
$('#player .title').text(elm.find('.title a').text()); | |
$('#player .time').text('Loading...'); | |
progressLoading(); | |
$(document).off('mousemove', seekFunc); | |
song.addEventListener('progress', function(ev) { | |
progressProgress(this.buffered); | |
}); | |
song.addEventListener('loadeddata', function(ev) { | |
progressProgress(this.buffered); | |
}); | |
song.preload = 'auto'; | |
song.volume = volume; | |
song.play(); | |
$('#playBtn').addClass('disabled').find('.icon').removeClass('icon-play').removeClass('icon-pause').addClass('icon-spinner'); | |
song.addEventListener('timeupdate',function () { | |
$('#player .time').text(sec2time(song.currentTime) + ' / ' + sec2time(songDuration)); | |
progressUpdate(); | |
}); | |
song.addEventListener('ended', function() { | |
playNext(); | |
}); | |
song.addEventListener('canplay', function() { | |
$('#playBtn').removeClass('disabled').find('.icon').removeClass('icon-spinner').removeClass('icon-play').addClass('icon-pause'); | |
}); | |
song.addEventListener('error', function(er) { | |
$('#playBtn').css('background', 'red').text('Error'); | |
}); | |
} | |
function playNext() { | |
var next = $('.playlist tbody tr.active').next('tr[data-songurl]'); | |
if(next.length) { | |
playRow(next); | |
} else { | |
window.location = "https://vgmrips.net/packs/random#autotocar"; | |
}; | |
} | |
function playPrev() { | |
var next = $('.playlist tbody tr.active').prev(); | |
if(next.length) playRow(next); | |
} | |
clearListeners('.playlist tbody a.beginPlay'); | |
$('.playlist tbody a.beginPlay').click(function(e) { playRow($(this).closest('tr')); e.preventDefault(); }); | |
var c = getCookie('vgmripsPlayerVolume'); | |
if(typeof c != 'undefined' && !c.isNan) { | |
if(c > 1.0) c = 1.0; | |
if(c < 0.0) c = 0.0; | |
volume = c; | |
}; | |
clearListeners('#volumeBar'); | |
clearListeners('#volumeBar .progress-bar'); | |
var $volumeBar = $('#volumeBar'); | |
function setVolume(vol) { | |
if(vol < 0) vol = 0; | |
if(vol > 1) vol = 1; | |
volume = vol; | |
if(song) song.volume = volume; | |
document.cookie = 'vgmripsPlayerVolume=' + volume; | |
$('#volumeBar .progress-bar').css('width', volume * $volumeBar.width() + 'px'); | |
} | |
function volumeBar(ev) { | |
var pos = $volumeBar.offset(); | |
setVolume((ev.clientX - pos.left) / $volumeBar.width()); | |
ev.preventDefault(); | |
ev.stopPropagation(); | |
} | |
$volumeBar.on('mousedown', function(ev) { | |
volumeBar(ev); | |
$(document).on('mousemove', volumeBar); | |
$(document).on('mouseup', function() { | |
$volumeBar.removeClass('open'); | |
$(document).off('mousemove', volumeBar); | |
}); | |
}); | |
$volumeBar.find('.progress-bar').css('width', volume * $volumeBar.width() + 'px'); | |
$('#player .title').text($('.playlist tr:first .title a').text()); | |
if(window.location.hash == '#autotocar') { | |
playRow(); | |
} else if(window.location.hash) { | |
var $r = $('#song-'+(window.location.hash.replace(/^#/, ''))); | |
if($r.length > 0) playRow($r); | |
} | |
$('.fav a').click(function(ev) { | |
var $a = $(this), $tr = $a.closest('tr'), data = { pack_url: typeof packUrl == 'undefined' ? $tr.data('packurl') : packUrl, song_order: $a.data('order') }; | |
if($a.data('favorited') != '0') { | |
data.remove = true; | |
} | |
$.getJSON(baseUrl+'/fav.json', data, function(favd) { | |
$a.data('favorited', favd ? '1' : '0'); | |
if(favd) { | |
$a.find('span').removeClass('icon-heart2').addClass('icon-heart'); | |
} else { | |
$a.find('span').removeClass('icon-heart').addClass('icon-heart2'); | |
} | |
}); | |
ev.preventDefault(); | |
ev.stopPropagation(); | |
}); | |
clearListeners('.playlistAdd'); | |
$('.playlistAdd').click(function(ev) { | |
var $tr = $(this).closest('tr'); | |
var data = { | |
playlist_id: $(this).data('playlist'), | |
pack_url: typeof packUrl == 'undefined' ? $(this).data('packurl') : packUrl, | |
song_order: $tr.data('order') | |
}; | |
console.log(data); | |
$.getJSON(baseUrl+'/pl.json', data, function(dat) { | |
alert('Added to playlist. Total songs: '+dat); | |
}); | |
ev.preventDefault(); | |
ev.stopPropagation(); | |
}); | |
var randomLink = document.querySelector("a[href='https://vgmrips.net/packs/random']"); | |
if (randomLink) { | |
randomLink.href += "#autotocar"; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment