Repository navigation
Expand file tree
/
Copy pathYouTubePlayablesVolume.user.js
More file actions
351 lines (330 loc) · 16.1 KB
/
Copy pathYouTubePlayablesVolume.user.js
File metadata and controls
351 lines (330 loc) · 16.1 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// ==UserScript==
// @name YouTube Playables Volume
// @namespace ModLabs
// @version 1.0.0-GitHub
// @description Adds a hover volume slider to YouTube games, including WebAudio sound.
// @author ModLabs
// @license Apache License 2.0
// @match https://www.youtube.com/*
// @match https://m.youtube.com/*
// @match https://youtube.com/*
// @match https://*.playables.usercontent.goog/*
// @run-at document-start
// @inject-into page
// @grant none
// @updateURL https://raw.githubusercontent.com/ModLabsCC/Scripts/main/YouTubePlayablesVolume.user.js
// @downloadURL https://raw.githubusercontent.com/ModLabsCC/Scripts/main/YouTubePlayablesVolume.user.js
// ==/UserScript==
(function () {
'use strict';
const installed = Symbol.for('ModLabs.YouTubePlayablesVolume');
if (window[installed]) return;
window[installed] = true;
const channel = 'modlabs.youtube-playables-volume';
const youtubeOrigins = new Set(['https://www.youtube.com', 'https://m.youtube.com', 'https://youtube.com']);
function isGameUrl(value) {
try {
const url = new URL(value);
return url.protocol === 'https:' && url.hostname.endsWith('.playables.usercontent.goog');
} catch { return false; }
}
function createAudioControl() {
let volume = 0;
const buses = new Map();
const levels = new WeakMap();
const routedMedia = new WeakSet();
const seenMedia = new WeakSet();
const mediaRefs = new Set();
const mediaPrototype = window.HTMLMediaElement?.prototype;
const descriptor = mediaPrototype && Object.getOwnPropertyDescriptor(mediaPrototype, 'volume');
function rememberMedia(element) {
if (!levels.has(element)) levels.set(element, descriptor.get.call(element));
if (!seenMedia.has(element)) {
seenMedia.add(element);
mediaRefs.add(new WeakRef(element));
}
const effectiveVolume = levels.get(element) * (routedMedia.has(element) ? 1 : volume);
if (descriptor.get.call(element) !== effectiveVolume) descriptor.set.call(element, effectiveVolume);
}
// Games may use unattached Audio() elements, not just <audio> in the DOM.
if (descriptor?.get && descriptor?.set && descriptor.configurable) {
Object.defineProperty(mediaPrototype, 'volume', {
...descriptor,
get() {
rememberMedia(this);
return levels.get(this);
},
set(value) {
const level = Number(value);
if (!Number.isFinite(level) || level < 0 || level > 1) {
return descriptor.set.call(this, value);
}
levels.set(this, level);
rememberMedia(this);
}
});
const play = mediaPrototype.play;
mediaPrototype.play = function (...args) {
rememberMedia(this);
return play.apply(this, args);
};
if (window.Audio) window.Audio = new Proxy(window.Audio, {
construct(target, args, newTarget) {
const element = Reflect.construct(target, args, newTarget);
rememberMedia(element);
return element;
},
apply(target, receiver, args) {
const element = Reflect.apply(target, receiver, args);
rememberMedia(element);
return element;
}
});
document.addEventListener('play', event => {
if (event.target instanceof HTMLMediaElement) rememberMedia(event.target);
}, true);
const trackMedia = root => {
if (root instanceof HTMLMediaElement) rememberMedia(root);
root.querySelectorAll?.('audio, video').forEach(rememberMedia);
};
new MutationObserver(records => {
for (const record of records) for (const node of record.addedNodes) trackMedia(node);
}).observe(document, { childList: true, subtree: true });
trackMedia(document);
}
const nodePrototype = window.AudioNode?.prototype;
if (nodePrototype) {
const connect = nodePrototype.connect;
const disconnect = nodePrototype.disconnect;
function busFor(context) {
if (!buses.has(context)) {
const bus = context.createGain();
bus.gain.value = volume;
connect.call(bus, context.destination);
buses.set(context, bus);
context.addEventListener('statechange', () => {
if (context.state === 'closed') buses.delete(context);
});
}
return buses.get(context);
}
// Unity, Howler and other engines can keep their own gain/mute controls.
nodePrototype.connect = function (destination, ...args) {
if (destination === this.context.destination) {
connect.call(this, busFor(this.context), ...args);
return destination;
}
return connect.call(this, destination, ...args);
};
nodePrototype.disconnect = function (...args) {
if (args[0] === this.context.destination && buses.has(this.context)) {
args[0] = buses.get(this.context);
}
return disconnect.apply(this, args);
};
// Media routed through WebAudio must be attenuated only once.
const prototypes = new Set([window.AudioContext?.prototype, window.webkitAudioContext?.prototype]);
for (const prototype of prototypes) {
if (!prototype?.createMediaElementSource || !descriptor) continue;
const createSource = prototype.createMediaElementSource;
prototype.createMediaElementSource = function (element, ...args) {
const source = createSource.call(this, element, ...args);
routedMedia.add(element);
rememberMedia(element);
return source;
};
}
}
return value => {
if (value === volume) return;
volume = value;
for (const [context, bus] of buses) {
if (context.state === 'closed') { buses.delete(context); continue; }
const now = context.currentTime;
bus.gain.cancelScheduledValues(now);
if (volume === 0) bus.gain.setValueAtTime(0, now);
else bus.gain.setTargetAtTime(volume, now, 0.015);
}
for (const ref of mediaRefs) {
const element = ref.deref();
if (element) rememberMedia(element);
else mediaRefs.delete(ref);
}
};
}
function startGameFrame() {
if (window.parent === window) return;
const originHint = new URLSearchParams(location.hash.slice(1)).get('origin');
let parentOrigin = youtubeOrigins.has(originHint) ? originHint : null;
try {
const referrerOrigin = new URL(document.referrer).origin;
if (youtubeOrigins.has(referrerOrigin)) parentOrigin = referrerOrigin;
} catch { /* The origin in YouTube's iframe URL also works without a referrer. */ }
if (!parentOrigin) return;
const setVolume = createAudioControl();
const ready = () => window.parent.postMessage({ channel, type: 'ready' }, parentOrigin);
let retry;
window.addEventListener('message', event => {
if (event.source !== window.parent || !youtubeOrigins.has(event.origin) || event.data?.channel !== channel) return;
if (event.data.type === 'query') { ready(); return; }
const volume = event.data.volume;
if (event.data.type !== 'volume' || typeof volume !== 'number' || !Number.isFinite(volume) || volume < 0 || volume > 1) return;
setVolume(volume);
clearInterval(retry);
});
ready();
retry = setInterval(ready, 1000);
}
function startYouTubeControls() {
const storageKey = 'modlabs.youtube-playables-volume';
const frames = new Map();
let controller;
let volume = 1;
let lastVolume = 1;
function loadVolume() {
try {
const saved = JSON.parse(localStorage.getItem(storageKey));
if (typeof saved?.volume === 'number' && saved.volume >= 0 && saved.volume <= 1) volume = saved.volume;
if (typeof saved?.lastVolume === 'number' && saved.lastVolume > 0 && saved.lastVolume <= 1) lastVolume = saved.lastVolume;
} catch { /* Storage may be disabled; the slider still works for this page. */ }
}
function saveVolume() {
try { localStorage.setItem(storageKey, JSON.stringify({ volume, lastVolume })); } catch {}
}
loadVolume();
function muted() { return controller?.button.getAttribute('aria-pressed') !== 'true'; }
function send(frame, message) {
frame.contentWindow?.postMessage({ channel, ...message }, new URL(frame.src).origin);
}
function update() {
const effectiveVolume = controller && !muted() ? volume : 0;
for (const [frame, state] of frames) {
if (state.ready) send(frame, { type: 'volume', volume: effectiveVolume });
}
if (!controller) return;
const percent = Math.round(effectiveVolume * 100);
const ready = [...frames.values()].some(state => state.ready);
controller.input.disabled = !ready;
controller.input.value = String(percent);
controller.input.setAttribute('aria-valuetext', `${percent}%`);
controller.input.style.setProperty('--ytg-volume-fill', `${percent}%`);
if (controller.output.textContent !== `${percent}%`) controller.output.textContent = `${percent}%`;
controller.panel.title = ready ? '' : 'Reload the game page to activate the volume slider.';
}
window.addEventListener('message', event => {
if (event.data?.channel !== channel || event.data.type !== 'ready') return;
for (const frame of document.querySelectorAll('iframe')) {
if (!isGameUrl(frame.src) || event.source !== frame.contentWindow || event.origin !== new URL(frame.src).origin) continue;
trackFrame(frame);
frames.get(frame).ready = true;
update();
}
});
function trackFrame(frame) {
if (frames.get(frame)?.src === frame.src) return;
frames.get(frame)?.remove();
const loaded = () => {
frames.get(frame).ready = false;
send(frame, { type: 'query' });
update();
};
frame.addEventListener('load', loaded);
frames.set(frame, { src: frame.src, ready: false, remove: () => frame.removeEventListener('load', loaded) });
send(frame, { type: 'query' });
}
function unmount() {
if (!controller) return;
controller.anchor.classList.remove('ytg-volume-anchor');
delete controller.anchor.dataset.ytgDismissed;
controller.anchor.removeEventListener('pointerenter', controller.reveal);
controller.anchor.removeEventListener('focusin', controller.reveal);
controller.panel.remove();
controller = null;
}
function mount(button) {
const anchor = button.closest('toggle-button-view-model') || button.parentElement;
const panel = document.createElement('div');
panel.className = 'ytg-volume-popover';
const inner = document.createElement('div');
inner.className = 'ytg-volume-panel';
const input = document.createElement('input');
input.type = 'range';
input.min = '0'; input.max = '100'; input.step = '1';
input.setAttribute('aria-label', document.documentElement.lang.startsWith('de') ? 'Spiellautstärke' : 'Game volume');
const output = document.createElement('span');
output.setAttribute('aria-hidden', 'true');
inner.append(input, output);
panel.append(inner);
const reveal = () => { delete anchor.dataset.ytgDismissed; };
controller = { button, anchor, panel, input, output, reveal, wasMuted: button.getAttribute('aria-pressed') !== 'true' };
anchor.classList.add('ytg-volume-anchor');
anchor.addEventListener('pointerenter', reveal);
anchor.addEventListener('focusin', reveal);
anchor.append(panel);
for (const type of ['pointerdown', 'click', 'keydown', 'keyup']) panel.addEventListener(type, event => event.stopPropagation());
panel.addEventListener('keydown', event => {
if (event.key !== 'Escape') return;
event.preventDefault();
button.focus();
anchor.dataset.ytgDismissed = 'true';
});
input.addEventListener('input', () => {
volume = Number(input.value) / 100;
if (volume > 0) lastVolume = volume;
saveVolume();
// Apply attenuation before unmuting, avoiding a burst at full volume.
update();
if (muted() !== (volume === 0)) button.click();
controller.wasMuted = muted();
update();
});
}
const style = document.createElement('style');
style.textContent = `
.ytg-volume-anchor { position: relative; display: inline-flex; }
.ytg-volume-popover { position: absolute; bottom: 100%; right: -8px; padding-bottom: 8px; z-index: 5000; opacity: 0; visibility: hidden; pointer-events: none; transform: translateY(4px); transition: opacity .15s, transform .15s, visibility .15s; }
.ytg-volume-anchor:hover .ytg-volume-popover, .ytg-volume-anchor:focus-within .ytg-volume-popover { opacity: 1; visibility: visible; pointer-events: auto; transform: none; }
.ytg-volume-anchor[data-ytg-dismissed] .ytg-volume-popover { opacity: 0; visibility: hidden; pointer-events: none; }
.ytg-volume-panel { display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-radius: 22px; background: var(--yt-spec-menu-background, #282828); color: var(--yt-spec-text-primary, #fff); box-shadow: 0 4px 18px #0005; font: 12px/1.4 Roboto, Arial, sans-serif; }
.ytg-volume-panel span { min-width: 32px; text-align: right; font-variant-numeric: tabular-nums; }
.ytg-volume-panel input { appearance: none; width: 100px; height: 24px; margin: 0; padding: 0; background: transparent; color: inherit; cursor: pointer; }
.ytg-volume-panel input::-webkit-slider-runnable-track { height: 3px; border-radius: 3px; background: linear-gradient(to right, currentColor var(--ytg-volume-fill), #8888 var(--ytg-volume-fill)); }
.ytg-volume-panel input::-webkit-slider-thumb { appearance: none; width: 12px; height: 12px; margin-top: -4.5px; border-radius: 50%; background: currentColor; }
.ytg-volume-panel input::-moz-range-track { height: 3px; border-radius: 3px; background: #8888; }
.ytg-volume-panel input::-moz-range-progress { height: 3px; background: currentColor; }
.ytg-volume-panel input::-moz-range-thumb { width: 12px; height: 12px; border: 0; border-radius: 50%; background: currentColor; }
.ytg-volume-panel input:focus-visible { outline: 2px solid var(--yt-spec-call-to-action, #3ea6ff); outline-offset: 3px; border-radius: 4px; }
.ytg-volume-panel input:disabled { opacity: .45; cursor: wait; }
@media (prefers-reduced-motion: reduce) { .ytg-volume-popover { transition: none; transform: none; } }
`;
function sync() {
if (!style.isConnected) (document.head || document.documentElement)?.append(style);
const active = /^\/playables\/[^/]+\/?$/.test(location.pathname);
const button = active && document.querySelector('.ytMiniAppTopBarViewModelEndButtons toggle-button-view-model button[aria-pressed]');
if (controller?.wasMuted && button?.getAttribute('aria-pressed') === 'true' && volume === 0) {
volume = lastVolume; saveVolume();
}
if (controller && (controller.button !== button || !controller.panel.isConnected)) unmount();
if (button && !controller) mount(button);
for (const [frame, state] of frames) {
if (active && frame.isConnected && isGameUrl(frame.src)) continue;
state.remove(); frames.delete(frame);
}
if (active) for (const frame of document.querySelectorAll('iframe')) if (isGameUrl(frame.src)) trackFrame(frame);
if (controller) controller.wasMuted = muted();
update();
}
let scheduled = false;
new MutationObserver(() => {
if (scheduled) return;
scheduled = true;
requestAnimationFrame(() => { scheduled = false; sync(); });
}).observe(document, { subtree: true, childList: true, attributes: true, attributeFilter: ['aria-pressed', 'src'] });
for (const type of ['yt-navigate-finish', 'yt-page-data-updated', 'popstate']) window.addEventListener(type, sync);
window.addEventListener('storage', event => { if (event.key === storageKey) { loadVolume(); sync(); } });
setInterval(sync, 1000);
sync();
}
if (isGameUrl(location.href)) startGameFrame();
else if (window.parent === window) startYouTubeControls();
})();