Stable Volume Emulator

Emulates stable volume on videos without it.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Stable Volume Emulator
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Emulates stable volume on videos without it.
// @author       InariOkami
// @match        *://www.youtube.com/*
// @grant        none
// @icon         https://cdn.pixabay.com/photo/2021/10/02/23/20/logo-6676544_1280.png
// ==/UserScript==
(function() {
    'use strict';
    const volumeStorageKey = 'youtube_stable_volume';
    const volumeNormalizationKey = 'youtube_volume_normalization';
    let storedVolume = 1.0;
    let volumeHistory = [];

    function loadSettings() {
        const savedVolume = localStorage.getItem(volumeStorageKey);
        if (savedVolume !== null) {
            storedVolume = parseFloat(savedVolume);
        }
        const savedHistory = localStorage.getItem(volumeNormalizationKey);
        if (savedHistory !== null) {
            volumeHistory = JSON.parse(savedHistory);
        }
    }

    function saveVolume(volume) {
        localStorage.setItem(volumeStorageKey, volume);
    }

    function saveVolumeHistory() {
        localStorage.setItem(volumeNormalizationKey, JSON.stringify(volumeHistory));
    }

    function calculateAverageVolume() {
        if (volumeHistory.length === 0) return storedVolume;
        const total = volumeHistory.reduce((acc, val) => acc + val, 0);
        return total / volumeHistory.length;
    }

    function applyVolume(video, volume) {
        const boostedVolume = Math.min(volume, 1.0);
        video.volume = boostedVolume;
        volumeHistory.push(boostedVolume);
        if (volumeHistory.length > 10) volumeHistory.shift();
        saveVolumeHistory();

        video.addEventListener('volumechange', () => {
            saveVolume(video.volume);
            volumeHistory.push(video.volume);
            if (volumeHistory.length > 10) volumeHistory.shift();
            saveVolumeHistory();
        });
    }

    function handleVideo(video) {
        if (video) {
            video.volume = Math.min(storedVolume, 1.0);

            // Emulate stable volume if not available
            if (!video.hasAttribute('stable-volume')) {
                const averageVolume = calculateAverageVolume();
                applyVolume(video, averageVolume);
            }
        }
    }

    function observeDOMChanges() {
        const observer = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeName === 'VIDEO') {
                        handleVideo(node);
                    }
                });
            });
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

    function checkStableVolumeAvailability(callback) {
        const request = new XMLHttpRequest();
        request.open('GET', 'https://update.greasyfork.org/scripts/508598/Stable%20Volume%20Emulator.meta.js', true);
        request.onreadystatechange = function() {
            if (request.readyState === XMLHttpRequest.DONE) {
                if (request.status === 200) {
                    const responseText = request.responseText;
                    if (responseText.includes('Stable Volume')) {
                        callback(true);
                    } else {
                        callback(false);
                    }
                } else {
                    callback(false);
                }
            }
        };
        request.send();
    }

    function init() {
        checkStableVolumeAvailability(isAvailable => {
            if (!isAvailable) {
                loadSettings();
                const video = document.querySelector('video');
                handleVideo(video);
                observeDOMChanges();
            } else {
                const videos = document.querySelectorAll('video');
                videos.forEach(video => video.volume = Math.min(storedVolume, 1.0));
            }
        });
    }

    init();
})();