Stable Volume Emulator

Mimics stable volume control for YouTube.

اعتبارا من 15-09-2024. شاهد أحدث إصدار.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         Stable Volume Emulator
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Mimics stable volume control for YouTube.
// @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 = 0.5;
    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 smoothVolume = (video.volume + volume) / 2;
        video.volume = smoothVolume;
        volumeHistory.push(smoothVolume);
        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) {
            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 init() {
        loadSettings();
        const video = document.querySelector('video');
        handleVideo(video);
        observeDOMChanges();
    }
    init();
})();