Volume Booster

Control volume of playing media on any webpage

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Volume Booster
// @namespace    https://greasyfork.org/zh-CN/users/196399-xyabc120
// @version      2024-09-27
// @description  Control volume of playing media on any webpage
// @author       mr.zhao
// @match        *://*.bilibili.com/*
// @icon         https://www.bilibili.com/favicon.ico?v=1
// @grant        GM_getValue
// @grant        GM_setValue
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const storageKeyName = "VOLUMN_BOOSTER_GAIN_INDEX";
    let currentGainIndex = GM_getValue(storageKeyName, 0);
    const gainValues = [1.0, 2.0, 3.0, 4.0, 5.0];

    // 创建 AudioContext 和 GainNode
    const audioContext = new (window.AudioContext || window.webkitAudioContext)();
    const gainNode = audioContext.createGain();
    gainNode.gain.value = gainValues[currentGainIndex];

    let connectedVideo = null;
    // 尝试连接现有视频元素
    const connectMedia = () => {
        const videoElement = document.querySelector('video');
        if (videoElement && videoElement !== connectedVideo) {
            if (connectedVideo) {
                connectedVideo.srcObject = null;
            }
            const track = audioContext.createMediaElementSource(videoElement);
            track.connect(gainNode);
            gainNode.connect(audioContext.destination);
            connectedVideo = videoElement;
        }
    };

    // 监听 DOM 变化
    const observer = new MutationObserver(connectMedia);
    observer.observe(document.body, { childList: true, subtree: true });

    // 可选:创建一个按钮来调整音量
    const button = document.createElement('button');
    button.textContent = `Volume Boost ${gainValues[currentGainIndex]}x`;
    button.style.position = 'fixed';
    button.style.top = '70px';
    button.style.right = '10px';
    button.style.zIndex = '9999';
    button.style.padding = "1px 7px";
    button.title = "左键点击增加音量倍数,右键重置";
    document.body.appendChild(button);

    const updateGain = (gainIndex) => {
        currentGainIndex = gainIndex;
        gainNode.gain.value = gainValues[currentGainIndex];
        button.textContent = `Volume Boost ${gainValues[currentGainIndex]}x`;
        GM_setValue(storageKeyName, gainIndex);
    };

    button.addEventListener('click', () => updateGain(++currentGainIndex % gainValues.length));
    button.addEventListener('contextmenu', () => {
        event.preventDefault();
        updateGain(0);
    });

})();