HTML5 视频音频默认音量

避免被一些默认 100% 音量的视频/音频吓一跳(或社死)!且支持各网站分别记住音量...

Instalează acest script?
Script sugerat de autor

Poate îți va plăcea și自动无缝翻页.

Instalează acest script
// ==UserScript==
// @name         HTML5 视频音频默认音量
// @name:zh-CN   HTML5 视频音频默认音量
// @name:zh-TW   HTML5 視訊音訊預設音量
// @name:en      HTML5 Video Audio Default Volume
// @version      1.0.3
// @author       X.I.U
// @description  避免被一些默认 100% 音量的视频/音频吓一跳(或社死)!且支持各网站分别记住音量...
// @description:zh-CN  避免被一些默认 100% 音量的视频/音频吓一跳(或社死)!且支持各网站分别记住音量...
// @description:zh-TW  避免被一些預設 100% 音量的視訊/音訊嚇一跳(或社死)!且支援各網站分別記住音量...
// @description:en  Avoid being startled by some video/audio with default 100% volume! And support each website to remember the volume separately...
// @match        *://*/*
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAALfElEQVRYhX2Xe3Bd1XXGf3vvc859X+nq6nUlW5Yly7JlI2xsYzAwtnk4ATpAxkNTHm0mnaTT/gHTTvrIBDLTpp1JUoZppqHQls5AKTR2INOWJJQSXF4x2BhsJCRZ8kuWZckPSVf3/Trn7N3RVTFpQrNn1l97n7O/vda31reWMKMPcmUJA9U8vrwHGdqCHn4HPzePaIxhVSoYbYRXrn7BeMVbCUduF6kVUXHwvQP+6amDaqDnoIompmQytaBnTmB8H5lowrjgFss48SBeI/hUEEZeudLi1ywhJEIJdL6Q8rzal/1a5SGC4XZrYBvWwEZobMdLdH6RH+z/Io1taEeh52fe8tOZbysl/ouWFvANYP7fSz4DgAEBBIL4xiS8ubmnVcTZK68aRK29Dtm8dgnZJydRW+/E2nrnp19nz+7U77+60zt0qMz07J/KxuQTwrIw4rMBCDP6wC+FIIcO34eudDdXf/7jD52Opi772lugY3AZr++hp06gz48j+waRqTWYmVHcS+chEMFeuw1hBzBzY7g/fQE9fmqBYPzBQKrpVa/R4OkCAnXlSvXnX9sIll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ct13axWvJNwadiiAli4iEP8VAJ89HQtR129TLiG1nos0Nt8dSOi12qi9lRq3utVqT/lirql24hW3vLBQi3d3XUqu73+PZOonBNR/WnbYSMeGTO5/Xf6ZtwDwPwtFRezQVs+sAAAAAElFTkSuQmCC
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_openInTab
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_notification
// @license      GPL-3.0 License
// @run-at       document-start
// @namespace    https://github.com/XIU2/UserScript
// ==/UserScript==

(function() {
    'use strict';
    var menu_ID = [];
    registerMenuCommand(); // 注册脚本菜单
    function registerMenuCommand() {
        if (self != top) return
        // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
        if (menu_ID.length > 0){for (let i=0;i<menu_ID.length;i++){GM_unregisterMenuCommand(menu_ID[i]);}}
        menu_ID[0] = GM_registerMenuCommand('#️⃣ 修改全局默认音量 [ ' + GM_getValue('menu_defaultVolume', 30) + '% ]', function(){customDefaultVolume()});
        let nowVolume = ' (跟随全局)'
        if (localStorage.getItem('html5_xiu_currentVolume')) nowVolume = ' [ ' + parseInt(localStorage.getItem('html5_xiu_currentVolume')) + '% ]'
        menu_ID[1] = GM_registerMenuCommand('🔁 忘记当前网站音量' + nowVolume, function(){resetCurrentVolume()});
        menu_ID[2] = GM_registerMenuCommand('💬 反馈 & 建议', function () {GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true}); GM_openInTab('https://greasyfork.org/zh-CN/scripts/438400/feedback', {active: true,insert: true,setParent: true});});
    }

    insPage();
    currentPage();


    // 网页本身的 Video Audio 标签
    function currentPage() {
        document.querySelectorAll('video, audio').forEach(function(_this){
            //console.log('网页本身:', _this)
            isFirstEvent(_this)
        })
    }


    // 后续动态插入的 Video Audio 标签(插入事件)
    function insPage() {
        const callback = (mutationsList, observer) => {
            for (const mutation of mutationsList) {
                for (const target of mutation.addedNodes) {
                    if (target.nodeType != 1) break
                    //console.log('所有插入:', target)
                    if (target.tagName === 'VIDEO' || target.tagName === 'AUDIO') {
                        //console.log('后续插入1:', target)
                        isFirstEvent(target)
                    } else if (target.tagName === 'DIV') {
                        target.querySelectorAll('video, audio').forEach(function(_this){
                            //console.log('后续插入2:', _this)
                            isFirstEvent(_this)
                        })
                    }
                }
            }
        };
        const observer = new MutationObserver(callback);
        observer.observe(document, { childList: true, subtree: true });
    }


    // 音量变化事件(记住音量)
    function volumeChangeEvent(event) {
        if (event.target.muted) { // 判断是否静音
            localStorage.setItem('html5_xiu_currentVolume', 0)
            registerMenuCommand(); // 修改脚本菜单
        } else if (localStorage.getItem('html5_xiu_currentVolume') || ((event.target.volume * 100) !== GM_getValue('menu_defaultVolume', 30))) {
            localStorage.setItem('html5_xiu_currentVolume', event.target.volume * 100)
            registerMenuCommand(); // 修改脚本菜单
        }
    }


    // 判断该视频/音频元素是否已监听事件
    function isFirstEvent(target) {
        if (!target.controls) return; // 如果视频/音频已经有了自己的控件(即没有使用 HTML5 默认的控件),则退出
        modifyVolume(target);
        // 如果没有该属性,则代表是还未监听事件
        if (target.dataset.html5VolumeXiu != 'true') {
            target.dataset.html5VolumeXiu = 'true'
            target.addEventListener('volumechange', volumeChangeEvent);
        }
    }


    // 修改视频音量
    function modifyVolume(_this) {
        let nowVolume = parseFloat(localStorage.getItem('html5_xiu_currentVolume')); // 先看看 localStorage 有没有(即用户是否手动调整过音量)
        if (!nowVolume && nowVolume !== 0) nowVolume = GM_getValue('menu_defaultVolume', 30); // 如果 localStorage 没有,那就从脚本配置中获取
        if (!((typeof nowVolume === 'number') && nowVolume <= 100)) nowVolume = 30; // 如果获取到的音量数值不是数字,或大于 100,则重置为 30
        //console.log(_this, _this.volume, nowVolume, nowVolume / 100)
        _this.volume = nowVolume / 100; // 设置音量为 0.00~1.00 范围
        //console.log(_this.volume)
    }


    // 修改全局默认音量
    function customDefaultVolume() {
        let newValue = parseFloat(prompt('修改全局默认音量,不影响各网站记住的音量,当前网页需刷新后生效~\n范围:0~100 (即 0%~100%,不需要加 % 百分号)\n默认:30', GM_getValue('menu_defaultVolume', 30)));
        if (!Number.isNaN(newValue) && newValue >= 0 && newValue <= 100) {GM_setValue('menu_defaultVolume', newValue);}
        registerMenuCommand(); // 重新注册菜单(刷新菜单上的音量值)
    }


    // 忘记当前网站音量
    function resetCurrentVolume() {
        if (localStorage.getItem('html5_xiu_currentVolume')) localStorage.removeItem('html5_xiu_currentVolume') // 清理 localStorage
        currentPage(); // 重置当前网页的音量
    }
})();