Created
October 22, 2021 23:58
-
-
Save AjaxGb/a85e17af8d4623bc176af9466178abe0 to your computer and use it in GitHub Desktop.
Spare My Left Ear - Userscript to fix audio playing in only one ear
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
// ==UserScript== | |
// @name Spare My Left Ear | |
// @version 0.1 | |
// @description Fix audio playing in only one ear by changing all stereo audio on the page into mono | |
// @author AjaxGb | |
// @include * | |
// @run-at context-menu | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const mediaSymbol = Symbol('spareMyLeftEarSource'); | |
class SpareMyLeftEar { | |
constructor() { | |
if (unsafeWindow.spareMyLeftEarManager) { | |
throw new Error('Attempted to create a second Spare My Left Ear manager'); | |
} | |
this.ctx = new AudioContext(); | |
this.ctx.destination.channelCount = 1; | |
unsafeWindow.spareMyLeftEarManager = this; | |
} | |
static getInstance() { | |
if (!unsafeWindow.spareMyLeftEarManager) { | |
unsafeWindow.spareMyLeftEarManager = new SpareMyLeftEar(); | |
} | |
return unsafeWindow.spareMyLeftEarManager; | |
} | |
addMedia(media) { | |
if (media[mediaSymbol]) { | |
return false; | |
} | |
console.log('Sparing left ear from:', media); | |
const src = this.ctx.createMediaElementSource(media); | |
src.connect(this.ctx.destination); | |
media[mediaSymbol] = src; | |
return true; | |
} | |
addAllMedia() { | |
for (const media of document.querySelectorAll('video, audio')) { | |
manager.addMedia(media); | |
} | |
} | |
addAllMediaForever() { | |
this.addAllMedia(); | |
if (!this.observer) { | |
this.observer = new MutationObserver(() => this.addAllMedia()); | |
this.observer.observe(document.body, { | |
subtree: true, | |
childList: true, | |
}); | |
} | |
} | |
} | |
const manager = SpareMyLeftEar.getInstance(); | |
manager.addAllMediaForever(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment