Youtube Video Max Volume Patch

Override the html5 video volume to 1 when player's volume slider is set to 100%.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube Video Max Volume Patch
// @namespace    http://github.com/jaytohe/
// @version      0.2
// @description  Override the html5 video volume to 1 when player's volume slider is set to 100%.
// @author       jaytohe
// @license      MIT
// @match        https://www.youtube.com/*
// @run-at       document-idle
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const curr_volume = () => document.querySelector('div[class^="ytp-volume-panel"]').getAttribute('aria-valuenow');
    const get_video = () => document.querySelector('video[src]'); //video[src] is used to prevent accidental firing on bogus video in homepage

    const forceMaxVol = (event) => {
        if (curr_volume() === '100') {
            event.target.volume = 1;
        }

    }
    const initHijack = (video) => {
        if (video !== null && curr_volume() !== null) {
            console.log('Patching max volume...');

            if (curr_volume() === '100') //in case video autoplays
                video.volume = 1;

            if (video.onplay === null)
                video.onplay = forceMaxVol;

            if(video.onvolumechange === null)
                video.onvolumechange = forceMaxVol;
        }
    }
    const wait_for_video = setInterval(function() {
        if (get_video() !== null) {
            console.log('[YOTUBE MAX VOLUME PATCH] -- FOUND VIDEO.');
            setTimeout(()=>initHijack(get_video()), 1000);
            clearInterval(wait_for_video);
        }
    }, 500);
})();