Last active
May 11, 2021 15:21
-
-
Save kch/f5abeaf3797bbfe8b4170aaa280d4caa to your computer and use it in GitHub Desktop.
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 Youtube ensure HD | |
// @description Sets youtube player to highest resolution if drops from an HD res | |
// @match *://www.youtube.com/watch* | |
// @match *://youtu.be/watch* | |
// ==/UserScript== | |
let intervalID = setInterval(()=> { | |
let qs = (q) => document.querySelector(q) | |
let qsa = (q) => [...document.querySelectorAll(q)] | |
let firstMatch = (q,p) => qsa(q).filter(e => e.innerText.match(p))[0] | |
let clickSeq = (m,...ms) => m && (m = firstMatch(...m)) && (m.click(), setTimeout(clickSeq(...ms), 250)) | |
const adSelector = '.ytp-ad-player-overlay' | |
const buttonWithBadgeSelector = '.ytp-settings-button:is(.ytp-hd-quality-badge,.ytp-4k-quality-badge,.ytp-8k-quality-badge)' | |
const buttonWithoutBadgeSelector = '.ytp-settings-button:not(.ytp-hd-quality-badge):not(.ytp-4k-quality-badge):not(.ytp-8k-quality-badge)' | |
if (qs(adSelector)) return // don't try anything during ads | |
if (!qs(buttonWithoutBadgeSelector)) return // don't try if we can't find a settings button without a badge (ie not present somehow, or has badge) | |
console.log("Resetting youtube to HD") | |
clickSeq( | |
['.ytp-settings-button', /()/], | |
['.ytp-menuitem', /^Quality/], | |
['.ytp-menuitem', /^(4320p|2160p|1440p|1080p|720p)/]) | |
// check that we got the hd badge or else give up, probably not hd video | |
setTimeout(()=> { | |
if (qs(buttonWithBadgeSelector)) return | |
clearInterval(intervalID) | |
console.log("HD content not set, aborting further attempts") | |
}, 600) | |
}, 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment