Last active
February 5, 2025 22:52
-
-
Save juliendargelos/300e765afa68e4a3530ec8e0849d608c to your computer and use it in GitHub Desktop.
User Script that forces original audio track on YouTube videos
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 Force original audio track on YouTube videos | |
// @match https://youtube.com/* | |
// @match https://www.youtube.com/* | |
// ==/UserScript== | |
const ORIGINAL_AUDIO_TRACK_NAME_PATTERN = /^.+?\([^\)]+\)\s+[^\(\)]+|.+?\s+original$/ | |
new MutationObserver(debounce(100, update)).observe(document.head, { | |
childList: true, | |
subtree: true, | |
characterData: true | |
}) | |
setTimeout(update, 500) | |
function update() { | |
for (const element of document.querySelectorAll('ytd-player')) { | |
useOriginalAudioTrack(element.player_) | |
} | |
} | |
function useOriginalAudioTrack(player) { | |
const tracks = player.getAvailableAudioTracks() | |
for (const track of tracks) { | |
const { name } = track.getLanguageInfo() | |
if (ORIGINAL_AUDIO_TRACK_NAME_PATTERN.test(name)) { | |
player.setAudioTrack(track) | |
break | |
} | |
} | |
} | |
function debounce(delay, callback) { | |
let timeout | |
return (...args) => { | |
clearTimeout(timeout) | |
timeout = setTimeout(callback, delay) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment