-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathYouTubeVolumeMouseController.user.js
More file actions
217 lines (184 loc) · 6.82 KB
/
YouTubeVolumeMouseController.user.js
File metadata and controls
217 lines (184 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// ==UserScript==
// @name YouTube Volume Mouse Controller
// @namespace wddd
// @version 1.5.0
// @author wddd
// @license MIT
// @description Control YouTube video volume by mouse wheel.
// @homepage https://github.com/wdwind/YouTubeVolumeMouseController
// @downloadURL https://github.com/wdwind/YouTubeVolumeMouseController/raw/master/YouTubeVolumeMouseController.user.js
// @match *://www.youtube.com/*
// ==/UserScript==
var ytb = {
inIframe: function() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
},
isEmbed: function() {
return this.inIframe || window.location.href.includes('youtube.com/embed');
},
getPlayerElement: function() {
if (!this.isEmbed()) {
var ytd_player = document.getElementsByTagName("ytd-player");
for (var player of ytd_player) {
if (player.getPlayer() && player.className.indexOf('preview') == -1) {
return player;
}
}
} else {
return this.getPlayer();
}
},
getVideo: function() {
return this.getPlayerElement()?.getElementsByTagName("video")[0];
},
getPlayer: function() {
if (!this.isEmbed()) {
return this.getPlayerElement()?.getPlayer();
} else {
return document.getElementsByClassName("html5-video-player")[0];
}
},
ready: function() {
return this.getPlayer() && this.getVideo();
}
}
function run() {
if (!ytb.getPlayer()) {
// eslint-disable-next-line no-console
console.log("Player not found (yet).");
return;
}
var timer = 0;
// detect available wheel event
var support = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers support "wheel"
document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least "mousewheel"
"DOMMouseScroll"; // let"s assume that remaining browsers are older Firefox
ytb.getVideo().addEventListener(support, function (event) {
var volume = ytb.getPlayer().getVolume();
var volumeDelta = 5;
var deltaY = support == "mousewheel" ? event.wheelDelta : (event.deltaY || event.detail);
// Optimize volume change for touchpad
if (Math.abs(deltaY) < 5) {
volumeDelta = Math.max(Math.floor(Math.abs(deltaY)), 1);
}
volume += (deltaY > 0 ? -volumeDelta : volumeDelta);
// Limit the volume between 0 and 100
volume = Math.max(0, Math.min(100, volume));
if (volume > 0) {
ytb.getPlayer().unMute(true);
}
ytb.getPlayer().setVolume(volume, true);
timer = showSlider(timer, volume);
// Prevent the page to scroll
event.preventDefault();
event.stopImmediatePropagation();
return false;
});
}
function showSlider(timer, volume) {
if (timer) {
clearTimeout(timer);
}
var sliderBar = appendSlideBar();
sliderBar.style.display = "block";
timer = setTimeout(function () {
sliderBar.style.display = "none";
}, 1000);
sliderBar.innerText = "Volume: " + volume;
return timer;
}
function appendSlideBar() {
var sliderBar = document.getElementById("sliderBar");
if (!sliderBar) {
var sliderBarElement = document.createElement("div");
sliderBarElement.id = "sliderBar";
ytb.getPlayerElement().getElementsByClassName("html5-video-container")[0].appendChild(sliderBarElement);
sliderBar = document.getElementById("sliderBar");
addCss(sliderBar, {
"width": "100%",
"height": "20px",
"position": "relative",
"z-index": "9999",
"text-align": "center",
"color": "#fff",
"font-size": "initial",
"opacity": "0.9",
"background-color": "rgba(0,0,0,0.2)",
});
}
if (!ytb.isEmbed()) {
addCss(sliderBar, {"top": getSliderBarTopProp() + "px"});
}
return sliderBar;
}
function addCss(element, css) {
for (var cssAttr in css) {
element.style[cssAttr] = css[cssAttr];
}
}
function getSliderBarTopProp() {
var fullScreenTitleHeight = 0;
var fullScreenTitle = document.getElementsByClassName("ytp-title")[0];
if (fullScreenTitle && fullScreenTitle.offsetParent) {
fullScreenTitleHeight = fullScreenTitle.offsetHeight;
}
var videoTop = ytb.getVideo().getBoundingClientRect().top;
var headerBoundingBox =
(document.getElementById("masthead-positioner") || document.getElementById("masthead-container")).getBoundingClientRect();
var headerTop = headerBoundingBox.top;
var headerHeight = headerBoundingBox.height;
var overlap = (headerHeight + headerTop > 0) ? Math.max(0, headerHeight - videoTop) : 0;
return Math.max(fullScreenTitleHeight, overlap);
}
/**
* YouTube use Javascript to navigate between pages. So the script will not work:
* 1. If the script only includes/matches the sub pages (like the video page www.youtube.com/watch?v=...)
* 2. And the user navigates to the sub page from a page which is not included/matched by the script
*
* In the above scenario, the script will not be executed.
*
* To run the script in all cases,
* 1. Include/match the whole YouTube host
* 2. Detect Javascript events, and run the script appropriately
*
* Details:
* * https://stackoverflow.com/questions/32275387/recall-tampermonkey-script-when-page-location-changes/32277150#32277150
* * https://stackoverflow.com/questions/34077641/how-to-detect-page-navigation-on-youtube-and-modify-html-before-page-is-rendered
* * https://github.com/1c7/Youtube-Auto-Subtitle-Download/blob/master/Youtube-Subtitle-Downloader/Tampermonkey.js#L122-L152
*/
// trigger when navigating to new material design page
window.addEventListener("yt-navigate-finish", function () {
if (window.location.href.includes("/watch?v=")) {
run();
}
});
// trigger when navigating to the old page
window.addEventListener("spfdone", function () {
if (window.location.href.includes("/watch?v=")) {
run();
}
});
// trigger when directly loading the page
window.addEventListener("DOMContentLoaded", function () {
if (window.location.href.includes("/watch?v=")) {
run();
}
});
/**
* Use MutationObserver to cover all edge cases.
* https://stackoverflow.com/a/39803618
*
* This is to handle the use case where navigation happens but <video> has not been loaded yet.
* (In YouTube the contents are loaded asynchronously.)
*/
var observer = new MutationObserver(function() {
if (ytb.ready()) {
observer.disconnect();
run();
}
});
observer.observe(document.body, /* config */ {childList: true, subtree: true});