Mouse Amplifier

Amplify volume of Twitch streams on a per-channel basis

目前為 2023-02-13 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Mouse Amplifier
// @description Amplify volume of Twitch streams on a per-channel basis
// @author      Xspeed
// @namespace   xspeed.net
// @license     MIT
// @version     5
// @icon        https://cdn.discordapp.com/icons/399202924695388160/a_131293bc9991ade1ec366efb27b05adf.webp

// @match       *://*.twitch.tv/*
// @grant       GM.getValue
// @grant       GM.setValue
// @run-at      document-idle
// @noframes
// ==/UserScript==

const predefinedDefaults = {
    ironmouse: 2.33
};

let lastName;
let targetVid;
let gainNode;
let inputElem;

const audioCtx = new AudioContext();

function log(txt) {
    console.log('[' + GM.info.script.name + '] ' + txt);
}

// gain = 20 - 20 * sqrt(1 - (input / 64) ^ 2)
function input2gain(i) {
    return (20 - 20 * Math.sqrt(1 - (i / 64) * (i / 64))).toFixed(2);
}

// input = 64 * sqrt(1 - (1 - gain / 20) ^ 2)
function gain2input(g) {
    return (64 * Math.sqrt(1 - (1 - g / 20) * (1 - g / 20))).toFixed(0);
}

async function setup() {
    const path = document.location.pathname.substring(1).split('/');
    const mobile = document.location.hostname.split('.')[0] == 'm';

    if (path.length == 1 || path[0] == 'videos' || path[1] == 'clip') {
        let chnlElem;
        let chnlName;

        if (mobile) {
            const child = document.querySelector('a.tw-link>figure.tw-avatar>img.tw-image');
            if (child) chnlElem = child.parentElement.parentElement;
            if (chnlElem) {
                const arr = chnlElem.href.split('/');
                chnlName = arr[arr.length - 2];
            }
        }
        else {
            chnlElem = document.querySelector('div>a>h1.tw-title');
            if (chnlElem) chnlName = chnlElem.innerText;
        }

        if (!chnlName || chnlName.length == 0) {
            // Not a channel page, offline or not loaded yet
            return;
        }

        let gainData = JSON.parse(await GM.getValue('gainData', '{}')) ?? {};
        let gain = gainData[chnlName];

        if (gain == null || isNaN(gain)) {
            gain = predefinedDefaults[chnlName] ?? 1;
            gainData[chnlName] = gain;
        }

        if (chnlName != lastName) {
            log('Detected channel ' + chnlName + ", initial gain " + gain);
            lastName = chnlName;
        }

        function setGain() {
            gain = input2gain(inputElem.value);
            gainNode.gain.value = gain;
            inputElem.parentElement.childNodes[1].innerText = 'Gain ' + gain + 'x';
        }

        if (gainNode == null) {
            const source = audioCtx.createMediaElementSource(targetVid);
            gainNode = audioCtx.createGain();

            source.connect(gainNode);
            gainNode.connect(audioCtx.destination);
        }

        if (inputElem == null || !document.body.contains(inputElem)) {
            const container = document.createElement('div');

            inputElem = document.createElement('input');
            inputElem.type = 'range';
            inputElem.min = 2;
            inputElem.max = 64;
            inputElem.step = 1;
            inputElem.value = gain2input(gain);
            inputElem.style.verticalAlign = 'middle';

            const labelElem = document.createElement('label');
            labelElem.innerText = 'Gain ' + gain + 'x';
            labelElem.style.display = 'inline-block';
            labelElem.style.marginLeft = '5px';
            labelElem.style.verticalAlign = 'middle';

            inputElem.addEventListener('input', setGain);

            inputElem.addEventListener('change', function() {
                setGain();

                if (gain == (predefinedDefaults[chnlName] ?? 1)) {
                    delete gainData[chnlName];
                }
                else {
                    gainData[chnlName] = gain;
                }

                GM.setValue('gainData', JSON.stringify(gainData));
                log('Gain for channel ' + chnlName + ' set to ' + gain);
            });

            container.appendChild(inputElem);
            container.appendChild(labelElem);

            const parent = chnlElem.parentElement.parentElement.parentElement.parentElement;
            parent.appendChild(container);
        }

        setGain();
    }
}

function presetup() {
    if (gainNode == null || inputElem == null || !document.body.contains(inputElem)) {
        setup();
    }
}

function detect() {
    const vidElem = document.querySelector('video');

    if (vidElem != targetVid) {
        if (targetVid) targetVid.removeEventListener('canplay', presetup);
        if (gainNode) gainNode.disconnect();

        targetVid = vidElem;
        gainNode = null;

        if (targetVid) {
            log('New video element found on page');
            targetVid.addEventListener('canplay', presetup);
        }
    }

    presetup();
}

setInterval(detect, 1000);
detect();