-
Notifications
You must be signed in to change notification settings - Fork 2
/
youtube-volume.user.js
136 lines (123 loc) · 3.57 KB
/
youtube-volume.user.js
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
// ==UserScript==
// @name Youtube Scroll Volume
// @namespace https://greasyfork.org/users/649
// @version 1.1.12
// @description Use the scroll wheel to adjust volume of youtube videos
// @author Adrien Pyke
// @match *://www.youtube.com/*
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @require https://cdn.jsdelivr.net/gh/kufii/My-UserScripts@22210afba13acf7303fc91590b8265faf3c7eda7/libs/gm_config.js
// @require https://cdn.jsdelivr.net/gh/fuzetsu/userscripts@ec863aa92cea78a20431f92e80ac0e93262136df/wait-for-elements/wait-for-elements.js
// @require https://cdn.jsdelivr.net/gh/kufii/quick-query.js@2993f91ae90f3b2aff4af7e9ce0b08504f5c8060/dist/window/qq.js
// ==/UserScript==
(() => {
'use strict';
const { q } = window.QuickQuery;
const Util = {
bound: (num, min, max) => Math.max(Math.min(num, max), min)
};
const Config = GM_config([
{ key: 'reverse', label: 'Reverse Scroll', default: false, type: 'bool' },
{
key: 'horizontal',
label: 'Use Horizontal Scroll',
default: false,
type: 'bool'
},
{
key: 'step',
label: 'Change By',
default: 5,
type: 'number',
min: 1,
max: 100
},
{ key: 'hud', label: 'Display HUD', default: true, type: 'bool' },
{
key: 'requireShift',
label: 'Only handle scroll if holding "Shift" key',
default: false,
type: 'bool'
}
]);
GM_registerMenuCommand('Youtube Scroll Volume Settings', Config.setup);
let config = Config.load();
Config.onsave = newConf => (config = newConf);
GM_addStyle(/* css */ `
.YSV_hud {
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
opacity: 0;
transition: opacity 500ms ease 0s;
z-index: 10;
pointer-events: none;
}
.YSV_bar {
background-color: #444;
border: 2px solid white;
width: 80%;
max-width: 600px;
margin-bottom: 10%;
}
.YSV_progress {
transition: width 100ms ease-out 0s;
background-color: #888;
height: 20px;
}
.YSV_text {
position: absolute;
text-align: center;
line-height: 20px;
width: 80%;
max-width: 600px;
color: white;
}
`);
const createHud = () => {
const hud = document.createElement('div');
hud.classList.add('YSV_hud');
hud.innerHTML =
'<div class="YSV_bar"><div class="YSV_text"></div><div class="YSV_progress"></div></div>';
return hud;
};
waitForElems({
sel: 'ytd-player',
onmatch(node) {
let id;
const hud = createHud();
const progress = q(hud).q('.YSV_progress');
const text = q(hud).q('.YSV_text');
node.appendChild(hud);
const showHud = volume => {
clearTimeout(id);
progress.style.width = `${volume}%`;
text.innerHTML = `${volume}%`;
hud.style.opacity = 1;
id = setTimeout(() => (hud.style.opacity = 0), 800);
};
node.onwheel = e => {
if (config.requireShift && !e.shiftKey) return;
const player = node.getPlayer();
const dir =
((config.horizontal ? -e.deltaX : e.deltaY) > 0 ? -1 : 1) *
(config.reverse ? -1 : 1);
const vol = Util.bound(player.getVolume() + config.step * dir, 0, 100);
if (vol > 0 && player.isMuted()) player.unMute();
player.setVolume(vol);
if (config.hud) showHud(vol);
e.preventDefault();
e.stopImmediatePropagation();
};
}
});
})();