// ==UserScript== // @name Youtube Audio Mode // @description Listen to only the audio on YouTube without loading the video. // @version 1.2.2 // @include https://www.youtube.com/* // @license GPL-3.0+; http://www.gnu.org/licenses/gpl-3.0.txt // @run-at document-end // @grant GM_xmlhttpRequest // @grant GM.setValue // @grant GM.getValue // @noframes // @downloadURL https://git.io/vhek9 // ==/UserScript== (function(open) { window.addEventListener("yt-navigate-finish", audioMode); window.onYouTubeIframeAPIReady = audioMode(); async function audioMode() { if (location.pathname == "/watch") { let video = document.getElementsByTagName("video")[0]; let audioMode = await GM.getValue("ytAudioMode"); addToMenu(audioMode); if (audioMode) { setPoster(video, ["maxres", "hq", "sd"]); watchStream(video); } } } // Watch the media streams so we can select the audio function watchStream(video) { XMLHttpRequest.prototype.open = function(method, url) { let validStream = /^(?!.*live=1).+audio.+$/; if (validStream.test(url) && ! video.src.includes("audio")) { video.pause(); video.src = url.split("&range")[0]; video.play(); } open.apply(this, arguments); } } // Add audio mode to the settings menu async function addToMenu(audioMode) { let panel = document.getElementsByClassName("ytp-panel-menu")[0]; if (!panel.innerHTML.includes("Audio Mode")) { panel.innerHTML += `