Skip to content

Instantly share code, notes, and snippets.

@lac5
Last active June 17, 2024 15:04
Show Gist options
  • Select an option

  • Save lac5/95c3ae2abbd37b35e2a35ac2c49d9996 to your computer and use it in GitHub Desktop.

Select an option

Save lac5/95c3ae2abbd37b35e2a35ac2c49d9996 to your computer and use it in GitHub Desktop.
Stops YouTube from automatically pausing the video.
// ==UserScript==
// @name YouTube - Anti-Anti-AFK
// @namespace larryc5
// @version 1.0
// @description Stops YouTube from automatically pausing the video.
// @author Larry Costigan
// @include /^https?:\/\/(?:[^\/?#]*\.)?youtube.com\/.*$/
// @downloadURL https://gist.githubusercontent.com/larryc5/95c3ae2abbd37b35e2a35ac2c49d9996/raw/youtube-anti-anti-afk.user.js
// @updateURL https://gist.githubusercontent.com/larryc5/95c3ae2abbd37b35e2a35ac2c49d9996/raw/youtube-anti-anti-afk.meta.js
// ==/UserScript==
// ==UserScript==
// @name YouTube - Anti-Anti-AFK
// @namespace larryc5
// @version 1.0
// @description Stops YouTube from automatically pausing the video.
// @author Larry Costigan
// @include /^https?:\/\/(?:[^\/?#]*\.)?youtube.com\/.*$/
// @downloadURL https://gist.githubusercontent.com/larryc5/95c3ae2abbd37b35e2a35ac2c49d9996/raw/youtube-anti-anti-afk.user.js
// @updateURL https://gist.githubusercontent.com/larryc5/95c3ae2abbd37b35e2a35ac2c49d9996/raw/youtube-anti-anti-afk.meta.js
// ==/UserScript==
console.log('YouTube anti-anti-AFK: start');
{
const pathname = '/watch';
const elemType = document.createElement('video').nodeType;
const videoSelect = 'video.video-stream';
const pauseHandler = function() {
console.log('YouTube anti-anti-AFK: pause');
if (!this.ended && !document.hasFocus()) {
console.log('YouTube anti-anti-AFK: play');
this.play();
const reload = ()=> {
if (this.paused && !this.ended && !document.hasFocus()) {
const onload = ()=> {
this.removeEventListener('loadeddata', onload);
this.play();
setTimeout(reload, 5000);
};
this.addEventListener('loadeddata', onload);
this.load();
}
};
setTimeout(reload, 5000);
}
};
Array.from(document.querySelectorAll(videoSelect)).forEach(node => {
console.log('YouTube anti-anti-AFK: found video');
node.addEventListener('pause', pauseHandler);
});
new MutationObserver(mutations => {
if (location.pathname === pathname) {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(addedNode => {
if (addedNode.nodeType === elemType && addedNode.matches(videoSelect)) {
console.log('YouTube anti-anti-AFK: found video');
addedNode.removeEventListener('pause', pauseHandler);
addedNode.addEventListener('pause', pauseHandler);
}
});
});
}
}).observe(document.documentElement, {
childList: true,
subtree: true,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment