Skip to content

Instantly share code, notes, and snippets.

@herbaldrinkmate
Last active August 1, 2026 22:11
  • Select an option

Select an option

Disable YouTube Mobile Autoplay (Supports Desktop View)
// ==UserScript==
// @name Disable YouTube Mobile Autoplay (Supports Desktop View)
// @namespace http://tampermonkey.net/
// @version 1.3
// @description Automatically turn off autoplay on YouTube (PC & Mobile)
// @match https://www.youtube.com/*
// @match https://m.youtube.com/*
// @grant none
// @run-at document-start
// @license MIT
// ==/UserScript==
(() => {
'use strict';
const turnOffAutoplay = () => {
// Mobile
const mobileBtn = document.querySelector('.ytwAutonavToggleButtonHost');
if (mobileBtn && mobileBtn.getAttribute('aria-pressed') === 'true') {
mobileBtn.click();
}
// PC
const pcBtn = document.querySelector('.ytp-autonav-toggle-button');
if (pcBtn && pcBtn.getAttribute('aria-checked') === 'true') {
pcBtn.click();
}
};
// Run when page is loaded
window.addEventListener('load', turnOffAutoplay);
// Run on SPA navigation events
window.addEventListener('yt-navigate-finish', turnOffAutoplay);
// Debounce to prevent MutationObserver from firing excessively
let debounceTimer;
const observer = new MutationObserver(() => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(turnOffAutoplay, 500);
});
// Observe DOM changes (e.g., video replacement) and attribute
// changes (the toggle's real state may be set asynchronously,
// after the button element already exists in the DOM)
observer.observe(document.documentElement, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['aria-pressed', 'aria-checked']
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment