Advertisement
AHOHNMYC

"P" for play/pause & stopper

Sep 22nd, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Archive.org comfort audiobook player
  3. // @namespace    archive.org
  4. // @version      0.0.2.1
  5. // @author       AHOHNMYC
  6. // @match        https://archive.org/embed/*
  7. // @match        https://archive.org/details/*
  8. // @grant        none
  9. // @noframes
  10. // ==/UserScript==
  11.  
  12. /* Добавляет кнопку для перехода в Embedded-mode, в который вживлены:
  13.  * * Пробел для Паузы/Прололжения
  14.  * * Поддержка медиа-кнопок (работают только когда вкладка активна)
  15.  * * Ползунок для остановки через определённое количество времени (например, для того чтобы засыпать под книгу :з)
  16.  */
  17.  
  18. let defaultStopValue = 60*30;
  19.  
  20. if (/^\/embed/.test(location.pathname)) {
  21.     initStopController();
  22. } else {
  23.     addEmbedButton();
  24. }
  25.  
  26. function addEmbedButton() {
  27.     let newButton = document.querySelector('.action-buttons').appendChild(document.createElement('div'));
  28.     newButton.classList.add('topinblock');
  29.     newButton = newButton.appendChild(document.createElement('a'));
  30.     newButton.classList.add('button');
  31.     newButton.dataOriginalTitle = 'Embed';
  32.     newButton.textContent = 'Embed';
  33.     newButton.href = '/embed' + location.pathname.match(/^\/details(\/[^\/]+)/)[1] + '?playlist=true';
  34. }
  35.  
  36. function initStopController() {
  37.     document.addEventListener('keydown', ({key:k})=>{
  38.         if (k=='MediaPlayPause' || k==' ') document.querySelector('.jwplay button').click();
  39.         if (k=='MediaTrackPrevious') document.querySelector('.jwprev button').click();
  40.         if (k=='MediaTrackNext') document.querySelector('.jwnext button').click();
  41.     });
  42.     function addEl(el, newEl) { return el.appendChild(document.createElement(newEl)); }
  43.  
  44.     let mainEl = addEl(document.body, 'div');
  45.     let button = addEl(mainEl, 'button');
  46.     addEl(mainEl, 'br');
  47.     let timer = addEl(mainEl, 'input');
  48.  
  49.     let timerC = {
  50.         state: false,
  51.         start: ()=>{
  52.             t = setInterval(()=>{
  53.                 timer.value--;
  54.                 timerC.updateTablo();
  55.                 if (timer.valueAsNumber) return;
  56.                 let el = document.querySelector('.jwplay');
  57.                 if ( el && el.classList.contains('jwtoggle') ) el.querySelector('button').click();
  58.                 timerC.toggle();
  59.             }, 1000);
  60.         },
  61.         stop: ()=>{ clearInterval(t); },
  62.         toggle: ()=>{
  63.             timerC[(timerC.state?'stop':'start')]();
  64.             timerC.state = !timerC.state;
  65.             timerC.updateTablo();
  66.         },
  67.         updateTablo: ()=>{
  68.             let m = Math.trunc(timer.value/60),
  69.                 s = timer.value%60;
  70.             button.textContent = (timerC.state?'Остановка через ':'') + (m<10?'0':'') + m + ':' + (s<10?'0':'') + s;
  71.         }
  72.     };
  73.  
  74.  
  75.     mainEl.style = 'position: fixed; top:0; left:0';
  76.  
  77.     timer.type = 'range';
  78.     timer.min = 0;
  79.     timer.max = 60*90;
  80.     timer.value = defaultStopValue;
  81.  
  82.     timer.style.width = '900px';
  83.     timer.style.display = 'none';
  84.     mainEl.addEventListener('mouseover', ()=>{timer.style.display = '';});
  85.     mainEl.addEventListener('mouseout', ()=>{timer.style.display = 'none';});
  86.  
  87.     ['change', 'mousemove'].forEach(trigger=>{ timer.addEventListener(trigger, timerC.updateTablo); });
  88.  
  89.     button.addEventListener('click', timerC.toggle);
  90.  
  91.     timerC.updateTablo();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement