Last active
October 16, 2024 07:58
-
-
Save damp11113/2de703595074a9e1dec9132e8bebef71 to your computer and use it in GitHub Desktop.
YouTube No Normalization! (Userscript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name YouTube No Normalization! | |
// @namespace http://tampermonkey.net/ | |
// @version 1.1 | |
// @description Disable auto normalization in youtube | |
// @author damp11113 | |
// @match https://www.youtube.com/* | |
// @match https://youtube.com/* | |
// @grant none | |
// ==/UserScript== | |
var alwaysEnable = true; | |
var isenable = false; | |
var default_volume; | |
(function() { | |
'use strict'; | |
var video; | |
function baseElement() { | |
return document.querySelector('#content'); | |
} | |
function createFullVolumeButton() { | |
var el = document.createElement('div'); | |
el.classList.add('ytp-menuitem'); | |
el.setAttribute('role', 'menuitemcheckbox'); | |
el.setAttribute('aria-checked', 'false'); | |
el.setAttribute('tabindex', '0'); | |
var iconDiv = document.createElement('div'); | |
iconDiv.classList.add('ytp-menuitem-icon'); | |
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); | |
svg.setAttribute('height', '24'); | |
svg.setAttribute('viewBox', '0 0 24 24'); | |
svg.setAttribute('width', '24'); | |
var path = document.createElementNS("http://www.w3.org/2000/svg", "path"); | |
path.setAttribute('d', 'M14,3.23V5.29C16.89,6.15 19,8.83 19,12C19,15.17 16.89,17.84 14,18.7V20.77C18,19.86 21,16.28 21,12C21,7.72 18,4.14 14,3.23M16.5,12C16.5,10.23 15.5,8.71 14,7.97V16C15.5,15.29 16.5,13.76 16.5,12M3,9V15H7L12,20V4L7,9H3Z'); | |
path.setAttribute('fill', 'white'); | |
svg.appendChild(path); | |
iconDiv.appendChild(svg); | |
el.appendChild(iconDiv); | |
var labelDiv = document.createElement('div'); | |
labelDiv.classList.add('ytp-menuitem-label'); | |
labelDiv.innerText = 'No normalization'; // Set button label | |
el.appendChild(labelDiv); | |
var contentDiv = document.createElement('div'); | |
contentDiv.classList.add('ytp-menuitem-content'); | |
var toggleDiv = document.createElement('div'); | |
toggleDiv.classList.add('ytp-menuitem-toggle-checkbox'); | |
contentDiv.appendChild(toggleDiv); | |
el.appendChild(contentDiv); | |
// Set onclick event to adjust volume | |
isenable = (el.getAttribute('aria-checked') === 'true') | |
console.log(el.getAttribute('aria-checked')) | |
if (isenable) { | |
default_volume = baseElement().querySelector('video').volume; | |
video = baseElement().querySelector('video'); | |
video.volume = 1; | |
el.setAttribute('aria-checked', 'true'); // Update aria-checked to true when clicked | |
console.log('Volume set to 100%'); | |
isenable = true; | |
} | |
el.onclick = function() { | |
if (!isenable) { | |
default_volume = baseElement().querySelector('video').volume; | |
video = baseElement().querySelector('video'); | |
video.volume = 1; | |
el.setAttribute('aria-checked', 'true'); // Update aria-checked to true when clicked | |
console.log('Volume set to 100%'); | |
isenable = true; | |
} else { | |
video = baseElement().querySelector('video'); | |
video.volume = default_volume; | |
el.setAttribute('aria-checked', 'false'); // Update aria-checked to false when clicked | |
console.log('Volume set to default'); | |
isenable = false; | |
} | |
}; | |
return el; | |
} | |
function round(num, sig) { | |
var mult = Math.pow(10, sig); | |
return Math.round(num * mult) / mult; | |
} | |
var fullVolumeButtonTaskId = setInterval(function() { | |
if (baseElement().querySelector('video') === null) { | |
console.log('video element not found'); | |
return; | |
} | |
if (baseElement().querySelector('.ytp-menuitem[aria-checked="true"]') !== null) { | |
console.log('full volume button already found'); | |
clearInterval(fullVolumeButtonTaskId); | |
return; | |
} | |
var volumeSlider = baseElement().querySelector('.ytp-volume-slider-handle'); | |
if (!volumeSlider) { | |
console.log('volumeSlider not found'); | |
return; | |
} | |
var video = baseElement().querySelector('video'); | |
var volumeSliderLeftStr = volumeSlider.style.left; | |
var volumeSliderLeft = volumeSliderLeftStr.substr(0, volumeSliderLeftStr.length - 2); | |
var volumeSliderValue = parseFloat(volumeSliderLeft) * 2.5; | |
console.log('Checking slider ' + round(volumeSliderValue / 100, 2).toString() + ' against value ' + round(video.volume, 2).toString()); | |
if (alwaysEnable || volumeSliderValue / 100 > video.volume) { | |
var ytpPanelMenu = document.querySelector('.ytp-panel-menu'); // Select the ytp-panel-menu | |
if (ytpPanelMenu) { | |
ytpPanelMenu.prepend(createFullVolumeButton()); // Append button to the panel menu | |
clearInterval(fullVolumeButtonTaskId); // Clear interval after adding the button | |
} else { | |
console.log('ytp-panel-menu not found'); // Log if the panel menu is not found | |
} | |
} else { | |
console.log('volume slider did not meet criteria for Full Volume button'); | |
} | |
}, 500); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment