hybridsong / Saga Musix Audio Player

// ==UserScript==
// @name         Saga Musix Audio Player
// @namespace    http://catchip.org/
// @author       hybridsong
// @icon         https://secure.gravatar.com/avatar/187e65a2ca69335f7483a4f19812050a
// @version      0.1.2
// @updateURL    https://openuserjs.org/meta/hybridsong/Saga_Musix_Audio_Player.meta.js
// @match        http://sagamusix.de/en/music/
// @require      http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.3.min.js
/* jshint esnext: true */
// ==/UserScript==

/*
 *  Saga Musix Audio Player
 *  by hybridsong
 *
 *  HomePage:
 *      http://catchip.org/
 *  
 *  License:
 *      MIT License
 */

(function($) {
    'use strict';
	
	let tracks = $("#content > .track");
	let current = $(tracks[0]);
	
	let next = () => {
		current = current.next()[0];
		if (current)
			current = $(current);
		else
			return false;
		return true;
	};
	
	let play = () => {
		let container = $("<div/>", {
			class: "player_container"
		});
		
		let tune = current.children("a[id^='preview']").attr("href");
		tune = decodeURI(tune.match(/javascript:previewtune\('(.+)',\d+\)/)[1]);
		
		let src  = basepath + 'music_files/ogg/saga_musix_-_' + tune + '.ogg';
		
		let player = $("<audio/>", {
			preload: "auto",
			controls: true,
			autoplay: true,
			src: src
		});
		player[0].addEventListener("ended", () => {
			stop();
			if (next()) {
				play();
				window.scrollTo(0, current.offset().top-150);
			}
		});
		player.css("width", "100%");
		player.css("margin", "5px 0");
		
		container.append(player);
		current.prepend(container);
	};
	
	let stop = () => {
		current.children(".player_container").remove();
	};
	
	let hover_tracks = () => {
		tracks.one("mouseenter", function() {
			let self = $(this);
			let go_ths = $("<a/>", {href: "javascript:"});

			go_ths.append("listen online");
			go_ths.click(() => {
				stop();
				current = self;
				play();
			});

			let listen_online = self.children("a[id^='preview']");
			listen_online.html(null);
			listen_online.after(go_ths);
		});
	};
	
	$(document).ready(hover_tracks);
})(jQuery);