forked from Araxeus/Youtube-Volume-Scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageAccess.js
138 lines (111 loc) · 3.62 KB
/
pageAccess.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
137
138
const $ = document.querySelector.bind(document);
const oneMonth = 2592e6;
const api = $('#movie_player');
const isMusic = window.location.href.includes('music.youtube');
let steps = 1;
// listen for 'steps' change
window.addEventListener('message', event => {
if (event.data.type === 'Youtube-Volume-Scroll' && typeof event.data.steps === 'number') {
steps = event.data.steps;
}
}, false);
let volumeCookie;
try {
volumeCookie = window.localStorage.getItem('Youtube-Volume-Scroll');
} catch {
printIncognitoError();
}
let hudFadeTimeout;
// incognito setup
if (volumeCookie) {
volumeCookie = JSON.parse(volumeCookie);
if (volumeCookie.incognito === true && volumeCookie.savedVolume !== api.getVolume()) {
api.setVolume(volumeCookie.savedVolume);
if (!isMusic) saveNativeVolume(volumeCookie.savedVolume);
}
}
// set last active time to now every 15min (blocks "are you there?" popup)
setInterval(() => window._lact = Date.now(), 9e5);
(isMusic ?
$('#main-panel') :
$('.html5-video-player')
).onwheel = event => {
if (event.shiftKey) {
event.preventDefault();
// Event.deltaY < 0 means wheel-up
changeVolume(event.deltaY < 0, 1);
}
};
function changeVolume(toIncrease, modifier) {
const newVolume = Math.round(toIncrease ?
Math.min(api.getVolume() + (steps * modifier), 100) :
Math.max(api.getVolume() - (steps * modifier), 0));
// Have to manually mute/unmute on youtube.com
if (!isMusic && newVolume > 0 && api.isMuted()) {
//$('.ytp-mute-button').click();
api.unMute();
}
api.setVolume(newVolume);
showVolume(newVolume);
if (!isMusic) saveNativeVolume(newVolume);
window.postMessage({ type: 'Youtube-Volume-Scroll', newVolume: newVolume }, '*');
}
injectVolumeHud();
function getVolumeHud() {
let volumeHud = $('#volumeHud');
if (volumeHud === null) {
injectVolumeHud();
volumeHud = $('#volumeHud');
}
if (volumeHud === null) {
console.err('Cannot Create Youtube-Volume-Scroll HUD');
return null;
}
return volumeHud;
}
function injectVolumeHud() {
if ($('#volumeHud')) return;
if (!isMusic) {
$('.ytp-cards-button-icon').style.display = 'none';
$('.ytp-chrome-top-buttons').style.display = 'none';
}
$(isMusic ?
'#song-video' :
'.html5-video-container'
).insertAdjacentHTML('afterend', `<span id='volumeHud' ${isMusic ? "class='music'" : ''}></span>`)
}
function showVolume(volume) {
let volumeHud = getVolumeHud();
if (volumeHud === null) return;
volumeHud.textContent = volume + '%';
volumeHud.style.opacity = 1;
if (hudFadeTimeout) clearTimeout(hudFadeTimeout);
hudFadeTimeout = setTimeout(() => {
volumeHud.style.opacity = 0;
hudFadeTimeout = null;
}, 1.5e3);
}
// save the volume to a native cookies used by youtube.com
function saveNativeVolume(newVolume) {
const data = JSON.stringify({
volume: newVolume,
muted: newVolume <= 0
})
const timeNow = Date.now();
try {
window.localStorage.setItem('yt-player-volume', JSON.stringify({
data: data,
expiration: timeNow + oneMonth,
creation: timeNow
}));
window.sessionStorage.setItem('yt-player-volume', JSON.stringify({
data: data,
creation: timeNow
}));
} catch {
printIncognitoError();
}
}
function printIncognitoError() {
console.error("Youtube-Volume-Scroll could not save volume to cookies, if you are in incognito mode see https://i.stack.imgur.com/mEidB.png");
}