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 यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==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);
            }
        }
    );
})();