Greasy Fork 支持简体中文。

YouTube Volume Mouse Controller

Control YouTube volume by mouse.

目前為 2018-12-29 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name YouTube Volume Mouse Controller
  3. // @namespace wddd
  4. // @version 1.1.0
  5. // @author wddd
  6. // @description Control YouTube volume by mouse.
  7. // @homepage https://github.com/wdwind/YouTubeVolumeMouseController
  8. // @match *://www.youtube.com/*
  9. // @require https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js
  10. // @grant GM_addStyle
  11. // @noframes
  12. // ==/UserScript==
  13.  
  14. function run() {
  15. "use strict";
  16.  
  17. var player = $("video");
  18. var timer = 0;
  19.  
  20. // detect available wheel event
  21. var support = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers support "wheel"
  22. document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least "mousewheel"
  23. "DOMMouseScroll"; // let"s assume that remaining browsers are older Firefox
  24.  
  25. player.bind(support, function(event) {
  26. var originalEvent = event.originalEvent;
  27. var volume = player[0].volume;
  28. var volumeDelta = 0.05;
  29. var deltaY = 0;
  30.  
  31. if (support == "mousewheel") {
  32. deltaY = originalEvent.wheelDelta;
  33. } else {
  34. deltaY = originalEvent.deltaY || originalEvent.detail;
  35. }
  36.  
  37. volume += (deltaY > 0 ? -volumeDelta : volumeDelta);
  38. player[0].volume = Math.max(0, Math.min(1, volume));
  39.  
  40. $(".ytp-volume-panel").attr("aria-valuenow", (player[0].volume * 100).toFixed(0));
  41. $(".ytp-volume-slider-handle").css("left", ((player[0].volume * 100) * 0.4) + "px");
  42.  
  43. showSlider();
  44.  
  45. // Prevent the page to scroll
  46. return false;
  47. });
  48.  
  49. function showSlider() {
  50. if (timer) {
  51. clearTimeout(timer);
  52. }
  53.  
  54. var sliderBar = $("div#sliderBar");
  55. if (!sliderBar[0]) {
  56. $("body").append("<div id=\"sliderBar\"></div>");
  57. GM_addStyle([
  58. "#sliderBar {width: 100%;",
  59. "height: 20px;",
  60. "position: fixed;",
  61. "top: 63px;",
  62. "z-index: 9999;",
  63. "text-align: center;",
  64. "color: #fff;",
  65. "font-size: initial;",
  66. "opacity: 0.9;",
  67. "background-color: rgba(0,0,0,0.2);}",
  68. ].join(" "));
  69. sliderBar = $("div#sliderBar");
  70. }
  71.  
  72. sliderBar.fadeIn(100);
  73. timer = setTimeout(function() {
  74. sliderBar.fadeOut(700);
  75. }, 1000);
  76.  
  77. sliderBar.html("Volume: " + (player[0].volume * 100).toFixed(0));
  78. }
  79. }
  80.  
  81. /**
  82. * YouTube use Javascript to navigate between pages. So the script will not work:
  83. * 1. If the script only includes/matches the sub pages (like the video page www.youtube.com/watch?v=...)
  84. * 2. And the user navigates to the sub page from a page which is not included/matched by the script
  85. *
  86. * In the above scenario, the script will not be executed.
  87. *
  88. * To run the script in all cases,
  89. * 1. Include/match the whole YouTube host
  90. * 2. Detect Javascript events, and run the script appropriately
  91. *
  92. * Details:
  93. * * https://stackoverflow.com/questions/32275387/recall-tampermonkey-script-when-page-location-changes/32277150#32277150
  94. * * https://stackoverflow.com/questions/34077641/how-to-detect-page-navigation-on-youtube-and-modify-html-before-page-is-rendered
  95. * * https://github.com/1c7/Youtube-Auto-Subtitle-Download/blob/master/Youtube-Subtitle-Downloader/Tampermonkey.js#L122-L152
  96. */
  97.  
  98. // trigger when loading new material design page
  99. var body = document.getElementsByTagName("body")[0];
  100. body.addEventListener("yt-navigate-finish", function() {
  101. if (window.location.href.includes("/watch?v=")) {
  102. run();
  103. }
  104. });
  105.  
  106. // trigger when loading old page
  107. window.addEventListener("spfdone", function() {
  108. if (window.location.href.includes("/watch?v=")) {
  109. run();
  110. }
  111. });