Last active
August 25, 2023 01:45
-
-
Save gregilo/f2c034c2a91fba10a2de868e2b490b6c to your computer and use it in GitHub Desktop.
Stop YouTube Channel Featured Video Autoplay (TamperMonkey/Greasemonkey)
This file contains hidden or 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 Stop YouTube Channel Autoplay | |
| // @namespace https://github.com/gregilo | |
| // @version 0.2 | |
| // @description Stop featured videos from autoplaying on YouTube Channel landing pages | |
| // @author gregilo | |
| // @match https://www.youtube.com/channel* | |
| // @match https://www.youtube.com/user* | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| /** | |
| * Poll for a given set of acceptance criteria with timeout (thank you David Walsh!) | |
| * @param {function} fn Function containing acceptance criteria | |
| * @return {object} Promise that resolves when acceptance criteria is met (rejects on timeout) | |
| */ | |
| function poll(fn) { | |
| const endTime = Number(new Date()) + 10000; // timeout after 10 seconds | |
| const interval = 100; // poll every millisecond | |
| const checkCondition = function(resolve, reject) { | |
| // If the condition is met, we're done! | |
| const result = fn(); | |
| if (result) { | |
| resolve(result); | |
| } | |
| // If the condition isn't met but the timeout hasn't elapsed, go again | |
| else if (Number(new Date()) < endTime) { | |
| setTimeout(checkCondition, interval, resolve, reject); | |
| } | |
| // Didn't match and too much time, reject! | |
| else { | |
| reject(new Error('timed out for ' + fn + ': ' + arguments)); | |
| } | |
| }; | |
| return new Promise(checkCondition); | |
| } | |
| /** | |
| * Check if video element is loaded on page | |
| * @return {boolean} | |
| */ | |
| function detectChannelVideo() { | |
| let videos = document.getElementsByTagName('video'); | |
| return (videos.length && typeof videos[0] !== 'undefined'); | |
| } | |
| poll(detectChannelVideo) | |
| .then(() => { | |
| console.log('adding event listener') | |
| let videos = document.getElementsByTagName('video'); | |
| let theVideo = videos[0]; | |
| theVideo.addEventListener('loadeddata', () => theVideo.pause()); | |
| }) | |
| .catch(err => console.error(err)); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment