This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/// Adds a button to videos to amplify the volume | ||
|
||
// @ts-check | ||
|
||
(async function() { | ||
'use strict'; | ||
|
||
/** @type {MediaElementAudioSourceNode | null} */ | ||
let audioContextSource = null; | ||
/** @type {GainNode | null} */ | ||
let audioContextGain = null; | ||
/** @type {HTMLButtonElement | null} */ | ||
let amplifyButton = null; | ||
|
||
const maxGain = 10; | ||
|
||
function setupAudioContext() { | ||
if (!video) return; | ||
if (audioContextSource?.mediaElement === video) return; | ||
|
||
const audioContext = new AudioContext(); | ||
audioContextSource = audioContext.createMediaElementSource(video); | ||
audioContextGain = audioContext.createGain(); | ||
|
||
audioContextSource.connect(audioContextGain); | ||
audioContextGain.connect(audioContext.destination); | ||
} | ||
|
||
function addAmplifyButton() { | ||
/** @type {HTMLDivElement | null} */ | ||
const volumeButton = document.querySelector('.vjs-volume-menu-button'); | ||
if (!volumeButton) return; | ||
const controlBar = volumeButton.parentElement; | ||
if (!controlBar) return; | ||
|
||
/** @type {HTMLButtonElement | null} */ | ||
amplifyButton = controlBar.querySelector('.vjs-amplify-control'); | ||
if (!amplifyButton) { | ||
amplifyButton = document.createElement('button'); | ||
amplifyButton.className = "vjs-amplify-control vjs-control vjs-button"; | ||
amplifyButton.type = "button"; | ||
amplifyButton.title = "Amplify volume"; | ||
|
||
const amplifyButtonSpan = document.createElement('span'); | ||
amplifyButtonSpan.className = "vjs-control-text"; | ||
amplifyButtonSpan.innerText = "Amplify volume"; | ||
amplifyButton.appendChild(amplifyButtonSpan); | ||
|
||
amplifyButton.setAttribute('aria-valuemin', '1'); | ||
amplifyButton.setAttribute('aria-valuemax', `${maxGain}`); | ||
updateButton(); | ||
} | ||
// left click | ||
amplifyButton.onclick = onAmplifyButtonClick; | ||
// right click | ||
amplifyButton.oncontextmenu = onAmplifyButtonRightClick; | ||
console.log('Amplify button:', amplifyButton); | ||
|
||
controlBar.insertBefore(amplifyButton, volumeButton.nextSibling); | ||
} | ||
|
||
function onAmplifyButtonClick() { | ||
console.log('Amplifying volume'); | ||
if (!audioContextGain) return; | ||
|
||
let gain = Math.round(audioContextGain.gain.value); | ||
gain = (gain < maxGain) ? (gain + 1) : 1; | ||
|
||
audioContextGain.gain.value = gain; | ||
updateButton(); | ||
} | ||
|
||
/** @param {MouseEvent} e */ | ||
function onAmplifyButtonRightClick(e) { | ||
console.log('Unamplifying volume'); | ||
if (!audioContextGain) return; | ||
e.preventDefault(); | ||
|
||
let gain = Math.round(audioContextGain.gain.value); | ||
gain = (gain > 1) ? (gain - 1) : maxGain; | ||
|
||
audioContextGain.gain.value = gain; | ||
updateButton(); | ||
} | ||
|
||
function updateButton() { | ||
if (!amplifyButton) return; | ||
const gain = audioContextGain?.gain.value || 1.0; | ||
|
||
amplifyButton?.setAttribute('aria-valuenow', `${gain}`); | ||
amplifyButton?.setAttribute('aria-valuetext', `${gain}`); | ||
} | ||
|
||
/** @type {HTMLVideoElement | null} */ | ||
let video = document.querySelector('video'); | ||
while (!video) { | ||
await new Promise(resolve => setTimeout(resolve, 500)); | ||
video = document.querySelector('video'); | ||
console.log('Found video:', video); | ||
} | ||
console.log('Video found:', video); | ||
setupAudioContext(); | ||
addAmplifyButton(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters