Last active
November 28, 2025 08:21
-
-
Save lumynou5/b036f405a0888bf9c3b9a3f560e36f3d to your computer and use it in GitHub Desktop.
Disable YouTube autoplaying everywhere.
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 YouTube Disable Autoplaying | |
| // @version 1.0.2 | |
| // @description Disable YouTube autoplaying everywhere. | |
| // @author Lumynous | |
| // @license MIT | |
| // @match https://www.youtube.com/* | |
| // @noframes | |
| // @downloadURL https://gist.github.com/lumynou5/b036f405a0888bf9c3b9a3f560e36f3d/raw/youtube-disable-autoplaying.user.js | |
| // @updateURL https://gist.github.com/lumynou5/b036f405a0888bf9c3b9a3f560e36f3d/raw/~meta | |
| // ==/UserScript== | |
| 'use strict'; | |
| const querySelectorAsync = (function () { | |
| const callbacks = new Set(); | |
| const observer = new MutationObserver((mutations) => { | |
| for (const mutation of mutations) { | |
| for (const node of mutation.addedNodes) { | |
| if (node.nodeType !== Node.ELEMENT_NODE) | |
| continue; | |
| for (const {selector, callback, self} of callbacks) { | |
| if (!node.matches(selector)) | |
| continue; | |
| callback(node); | |
| callbacks.delete(self); | |
| } | |
| if (!callbacks.size) { | |
| observer.disconnect(); | |
| return; | |
| } | |
| } | |
| } | |
| }); | |
| return function (selector) { | |
| return new Promise((resolve) => { | |
| const elm = document.querySelector(selector) | |
| if (elm) { | |
| resolve(elm); | |
| return; | |
| } | |
| if (!callbacks.size) | |
| observer.observe(document, {childList: true, subtree: true}); | |
| callbacks.add({ | |
| selector, | |
| callback: resolve, | |
| get self() { return this; }, | |
| }); | |
| }); | |
| }; | |
| })(); | |
| const button = document.createElement('button'); | |
| button.classList.add('ytp-button'); | |
| button.style.width = '40px'; | |
| button.style.height = '100%'; | |
| const container = document.createElement('div'); | |
| container.classList.add('ytp-autonav-toggle-button-container'); | |
| const inner = document.createElement('div'); | |
| inner.classList.add('ytp-autonav-toggle-button'); | |
| inner.style.margin = '0 auto'; | |
| button.appendChild(container).appendChild(inner); | |
| const manager = document.querySelector('yt-playlist-manager'); | |
| let state = false; | |
| function setState(newState) { | |
| state = newState; | |
| manager.set('canAutoAdvance_', state); | |
| inner.setAttribute('aria-checked', state.toString()); | |
| }; | |
| button.addEventListener('click', () => setState(!state)); | |
| document.addEventListener('yt-page-data-updated', (ev) => { | |
| // For miniplayer, the URL doesn't reflect it's a playlist. | |
| setState(state); | |
| if (ev.detail.pageType !== 'watch' || !location.search.match(/\?(?:.*&)?list=.*/)) | |
| return; | |
| document.querySelector('#playlist-actions #end-actions #flexible-item-buttons').replaceChildren(button); | |
| }); | |
| // Prevent home page videos from autoplaying. | |
| const preview = await querySelectorAsync('ytd-video-preview'); | |
| preview.remove(); | |
| // The shorts player is lazily created until watching a shorts. | |
| const shorts = await querySelectorAsync('#shorts-player video'); | |
| const shortsObserver = new MutationObserver((mutations) => { | |
| for (const mutation of mutations) { | |
| if (mutation.removedNodes.length) | |
| continue; | |
| mutation.target.loop = false; | |
| } | |
| }); | |
| shortsObserver.observe(shorts, {attributes: true}); |
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 YouTube Disable Autoplaying | |
| // @version 1.0.2 | |
| // @description Disable YouTube autoplaying everywhere. | |
| // @author Lumynous | |
| // @license MIT | |
| // @match https://www.youtube.com/* | |
| // @noframes | |
| // @downloadURL https://gist.github.com/lumynou5/b036f405a0888bf9c3b9a3f560e36f3d/raw/youtube-disable-autoplaying.user.js | |
| // @updateURL https://gist.github.com/lumynou5/b036f405a0888bf9c3b9a3f560e36f3d/raw/~meta | |
| // ==/UserScript== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

請問能夠讓首頁也停止自動播放嗎?
滑鼠停在上面就會自動播放