Lower Youtube preview volume

Youtube previews are too goddamn loud and the volume is not settable, so I fixed it.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

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

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

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

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

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Lower Youtube preview volume
// @namespace    https://greasyfork.org/en/scripts/577264-lower-youtube-preview-volume
// @version      2026-05-09
// @description  Youtube previews are too goddamn loud and the volume is not settable, so I fixed it.
// @author       Vicbo
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
    'use strict';

    const isHomeScreen = () => {
        return location.hostname === "www.youtube.com" && location.pathname === "/";
    }

    const isPreviewVideo = videoElement => {
        return !!videoElement.closest("ytd-video-preview");
    };

    const DEFAULT_PREVIEW_VOLUME = 0.25;

    const getPreviewVolume = () => {
        return GM_getValue(
            "previewVolume",
            DEFAULT_PREVIEW_VOLUME
        );
    }

    const configureVolume = async () => {
        const current = getPreviewVolume();

        const input = prompt(
            "Youtube preview volume (0.0 - 1.0)",
            current
        );

        if (input === null) return;

        let value = parseFloat(input);

        if (isNaN(value) || value < 0 || value > 1) {
            value = DEFAULT_PREVIEW_VOLUME;
        }

        await GM_setValue("previewVolume", value);
    }

    GM_registerMenuCommand(
        "Set preview volume",
        configureVolume
    );

    const volumeDescriptor = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "volume");

    Object.defineProperty(HTMLMediaElement.prototype, "volume",
        {
            get() {
                return volumeDescriptor.get.call(this);
            },

            set(value) {
                if (isHomeScreen() && isPreviewVideo(this)) {
                    value = getPreviewVolume();
                }

                return volumeDescriptor.set.call(this, value);
            }
        }
    );
})();