|
// ==UserScript== |
|
// @name Auto click "Related" sidebar button |
|
// @namespace http://tampermonkey.net/ |
|
// @version 0.3 |
|
// @description Auto click "Related" sidebar button |
|
// @author Zack H |
|
// @match https://www.youtube.com/* |
|
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com |
|
// @grant none |
|
// @homepage https://gist.github.com/zphixon/fdedb0db45feb4f8c8dd4cf53b6d89a1 |
|
// ==/UserScript== |
|
|
|
(function() { |
|
'use strict'; |
|
|
|
const chipSelector = 'yt-chip-cloud-chip-renderer'; |
|
const autoSelectIntervalMs = 1000; |
|
|
|
// Unfortunate state-tracking variables |
|
let shouldAutoClick = true; |
|
let wasAutoClick = true; |
|
let chipsWithClickListeners = []; |
|
let prevLocation = window.location.href; |
|
|
|
// Tampermonkey's builtin eslint complains if I reference the variables directly, so put |
|
// them behind doubly-unfortunate getters/setters. hmmmm |
|
let getWasAutoClick = () => wasAutoClick; |
|
let setWasAutoClick = (newValue, why) => { |
|
wasAutoClick = newValue; |
|
}; |
|
let setShouldAutoClick = (newValue, why) => { |
|
shouldAutoClick = newValue; |
|
}; |
|
|
|
let doAutoSelectRelated = () => { |
|
// Only look for chips on the watch page - We don't use @match for this, since |
|
// navigating to the watch page from the subscriptions page doesn't seem to reliably |
|
// re-run this script. YouTube must be doing something weird. |
|
if (!window.location.href.includes('youtube.com/watch')) { |
|
return; |
|
} |
|
|
|
// We also haven't reliably re-run this script when clicking on a video from the |
|
// sidebar, so re-enable auto clicking if we think we navigated somewhere new. |
|
if (window.location.href != prevLocation) { |
|
setShouldAutoClick(true); |
|
} |
|
prevLocation = window.location.href; |
|
|
|
// Find the chip we want to click on |
|
let chipToClick; |
|
for (let chip of document.querySelectorAll(chipSelector)) { |
|
if (chip.textContent.trim() == 'Related') { |
|
chipToClick = chip; |
|
} else if (chip.textContent.trim().startsWith('From ') && chipToClick == undefined) { |
|
// Prefer "Related", but accept "From ChannelName" if there is no "Related" |
|
chipToClick = chip; |
|
} |
|
|
|
if (!chipsWithClickListeners.includes(chip)) { |
|
chipsWithClickListeners.push(chip); |
|
chip.addEventListener('click', () => { |
|
// If the user clicked on a chip, stop auto-clicking. |
|
if (!getWasAutoClick()) { |
|
setShouldAutoClick(false); |
|
} |
|
|
|
// We no longer care if this was an auto-click. |
|
setWasAutoClick(false); |
|
}); |
|
} |
|
} |
|
|
|
if (chipToClick && shouldAutoClick && !chipToClick.selected) { |
|
// This was an auto-click. Don't stop auto-clicking yet - the chips seem to |
|
// disappear and reappear on page load sometimes, which re-selects All. We |
|
// may need to click Related again. |
|
setWasAutoClick(true); |
|
chipToClick.click(); |
|
} |
|
}; |
|
|
|
setInterval(doAutoSelectRelated, autoSelectIntervalMs); |
|
})(); |
|
|