Skip to content

Instantly share code, notes, and snippets.

@marcsello
Created August 22, 2024 13:29
Show Gist options
  • Save marcsello/0217645e80036e2684e8b408545a0166 to your computer and use it in GitHub Desktop.
Save marcsello/0217645e80036e2684e8b408545a0166 to your computer and use it in GitHub Desktop.
Adds an extra input box to youtube to set the real volume of the video
// ==UserScript==
// @name Extra volume changer for YouTube
// @namespace http://tampermonkey.net/
// @version 0.2
// @description try to take over the world!
// @author Marcsello
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
const volumeChangerID = "volume_changer_Mah7phop"
function pullVolumeFromVideo(i, video) {
i.value = video.volume * 100.0
}
function createVolumeChanger(video) {
let i = document.createElement('input')
i.id = volumeChangerID
i.type = 'number'
i.style.width = '50px'
i.style.margin = '0 1em'
i.max = 100
i.min = 0
i.step = 0.25
// box -> video
i.onchange = e => {
const vol = e.target.value / 100.0
video.volume = vol
}
// video -> box
const volUpdateFn = e => {
pullVolumeFromVideo(i, e.target);
}
video.onvolumechange = volUpdateFn;
video.onchange = volUpdateFn;
video.onchange = volUpdateFn;
video.onplay = volUpdateFn;
video.oncanplay = volUpdateFn;
video.ondurationchange = volUpdateFn;
video.ondurationchange = volUpdateFn;
pullVolumeFromVideo(i, video); // set initial value
// add it next to the subscribe button
return i
}
function checkVolumeChangerPresence() {
const elm = document.getElementById(volumeChangerID);
if (elm === null) {
return false;
}
const sb = document.getElementById('subscribe-button')
if (sb === null) {
return false;
}
if (sb.parentElement === null) {
return false;
}
let found = false
sb.parentElement.childNodes.forEach(e => {
if (e === elm) {
found = true;
}
});
return found;
}
const initialInitWaiterInterval = setInterval(function () {
// check url
const path = window.location.pathname
if (!path.startsWith("/watch")) { // not a video
return // don't add it (yet)
}
// check if the video is loaded (youtube is re-using the video tag)
const videos = document.getElementsByTagName('video');
if (videos.length !== 1) {
return // don't add it (yet)
}
// check if the subscribe button is there
const sb = document.getElementById('subscribe-button')
if (sb === null) { // subscribe button not there
return // don't add it (yet)
}
clearInterval(initialInitWaiterInterval); // <- player is kindof ready
// start monitoring and adding volume changer in case it's removed
setInterval(function() {
if (!checkVolumeChangerPresence()) {
const videos = document.getElementsByTagName('video');
if (videos.length !== 1) {
return; // no video, ignore
}
const video = videos[0];
let i = document.getElementById(volumeChangerID);
if (i === null) {
i = createVolumeChanger(video);
} else {
pullVolumeFromVideo(i, video);
}
const sb = document.getElementById('subscribe-button')
sb.parentElement.appendChild(i)
console.log("Extra volume changer added!")
}
}, 1500)
}, 500)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment