Guest User

Untitled

a guest
Jun 26th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         blasux mp3 queue&play
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.2
  5. // @description  embeds html5 player
  6. // @author       anon34522
  7. // @match        https://lexs.blasux.ru/music/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function(window){
  12.     // create player
  13.     var player = window.document.createElement('audio');
  14.     player.controls = true;
  15.     window.document.body.insertBefore(player, window.document.body.firstChild);
  16.  
  17.     // queue&play all
  18.     var anchors = [...window.document.querySelectorAll('a[href$="mp3"]')];
  19.     var queue = anchors.map(function(el){ return el.href; });
  20.     player.src = queue.shift();
  21.  
  22.     // render "jump to"-buttons
  23.     anchors.map(function(el, idx){
  24.         var btn = window.document.createElement('button');
  25.         btn.innerHTML ='→';
  26.         btn.onclick = function() {
  27.             // remove all buttons
  28.             anchors.map(function(el){
  29.                 var b = el.parentElement.querySelector('button');
  30.                 el.parentElement.removeChild(b);
  31.             });
  32.             for (var i=0; i < idx; i++) {
  33.                 player.src = queue.shift();
  34.             };
  35.             player.play();
  36.         };
  37.         el.parentNode.insertBefore(btn, el);
  38.     });
  39.  
  40.     // highlight playing now
  41.     player.addEventListener("play", function() {
  42.         anchors
  43.         .filter(function(el){
  44.             return el.href == player.src;
  45.         })
  46.         .map(function(el){
  47.             el.parentElement.style="background:#efc;";
  48.         });
  49.     }, false);
  50.  
  51.     player.addEventListener("ended", function() {
  52.         // remove highlight
  53.         anchors
  54.         .filter(function(el){
  55.             return el.href == player.src;
  56.         })
  57.         .map(function(el){
  58.             el.parentElement.style="";
  59.         });
  60.  
  61.         this.src = queue.shift();
  62.         this.play();
  63.     }, false);
  64. })(window)
Advertisement
Add Comment
Please, Sign In to add comment