Youtube Scroll Volume

Use the scroll wheel to adjust volume of youtube videos

当前为 2019-01-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Scroll Volume
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.1.2
  5. // @description Use the scroll wheel to adjust volume of youtube videos
  6. // @author Adrien Pyke
  7. // @match *://www.youtube.com/*
  8. // @grant GM_addStyle
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_registerMenuCommand
  12. // @require https://gitcdn.link/repo/kufii/My-UserScripts/fa4555701cf5a22eae44f06d9848df6966788fa8/libs/gm_config.js
  13. // @require https://gitcdn.link/repo/fuzetsu/userscripts/b38eabf72c20fa3cf7da84ecd2cefe0d4a2116be/wait-for-elements/wait-for-elements.js
  14. // @require https://gitcdn.link/repo/kufii/quick-query.js/2993f91ae90f3b2aff4af7e9ce0b08504f5c8060/dist/window/qq.js
  15. // ==/UserScript==
  16.  
  17. (() => {
  18. 'use strict';
  19.  
  20. const { q } = window.QuickQuery;
  21.  
  22. const Util = {
  23. bound: (num, min, max) => Math.max(Math.min(num, max), min)
  24. };
  25.  
  26. const Config = GM_config([
  27. { key: 'reverse', label: 'Reverse Scroll', default: false, type: 'bool' },
  28. { key: 'step', label: 'Change By', default: 5, type: 'number', min: 1, max: 100 }
  29. ]);
  30. GM_registerMenuCommand('Youtube Scroll Volume Settings', Config.setup);
  31.  
  32. let config = Config.load();
  33. Config.onsave = newConf => config = newConf;
  34.  
  35. GM_addStyle(`
  36. .YSV_hud {
  37. display: flex;
  38. justify-content: center;
  39. align-items: center;
  40. position: absolute;
  41. top: 0;
  42. bottom: 0;
  43. left: 0;
  44. right: 0;
  45. opacity: 0;
  46. transition: opacity 500ms ease 0s;
  47. z-index: 10;
  48. pointer-events: none;
  49. }
  50. .YSV_bar {
  51. background-color: #888;
  52. border: 2px solid white;
  53. width: 80%;
  54. max-width: 800px;
  55. }
  56. .YSV_progress {
  57. transition: width 250ms ease-out 0s;
  58. background-color: #444;
  59. height: 35px;
  60. }
  61. `);
  62.  
  63. const createHud = () => {
  64. const hud = document.createElement('div');
  65. hud.classList.add('YSV_hud');
  66. hud.innerHTML = '<div class="YSV_bar"><div class="YSV_progress"></div></div>';
  67. return hud;
  68. };
  69.  
  70. waitForElems({
  71. sel: 'ytd-player',
  72. onmatch(node) {
  73. let id;
  74.  
  75. const hud = createHud();
  76. const progress = q(hud).q('.YSV_progress');
  77. node.appendChild(hud);
  78.  
  79. node.onwheel = e => {
  80. const player = node.getPlayer();
  81. const dir = (e.deltaY > 0 ? -1 : 1) * (config.reverse ? -1 : 1);
  82.  
  83. const vol = Util.bound(player.getVolume() + (config.step * dir), 0, 100);
  84. player.setVolume(vol);
  85. if (vol > 0) player.unMute();
  86.  
  87. clearTimeout(id);
  88. progress.style.width = `${vol}%`;
  89. hud.style.opacity = 1;
  90. id = setTimeout(() => hud.style.opacity = 0, 800);
  91.  
  92. e.preventDefault();
  93. e.stopImmediatePropagation();
  94. };
  95. }
  96. });
  97. })();