Last active
May 11, 2026 00:25
-
-
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.1.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=.*/)) { | |
| document.querySelector("#playlist-actions #end-actions #flexible-item-buttons") | |
| .replaceChildren(button); | |
| } else if (ev.detail.pageType === "channel" && location.pathname.match(/^\/[^/]+(?:\/featured)?$/)) { | |
| document.querySelector("ytd-channel-video-player-renderer") | |
| ?.player | |
| .addEventListener("play", function callback(ev) { | |
| ev.currentTarget.pause(); | |
| ev.currentTarget.removeEventListener("play", callback, true); | |
| }, true); | |
| } | |
| }); | |
| document.addEventListener("yt-popup-opened", (ev) => { | |
| if (ev.target.is !== "ytd-sponsorships-offer-renderer") | |
| return; | |
| ev.target.playerElement.pause(); | |
| }); | |
| // Prevent home page videos from autoplaying. | |
| const preview = await querySelectorAsync("#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.1.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== |
Author
請問能夠讓首頁也停止自動播放嗎?
滑鼠停在上面就會自動播放
Author
@leadra 改好了。0.4.0
Author
了解,感謝^^
Author
2026-05-09更新
YouTube幾天前改了什麼,現在這樣移除掉播放器,雖然不會播放,但YouTube仍會以為有播放,會影響觀看紀錄。所以我會正式移除這個功能,之後就自己調整帳號設定。
編輯:我找到了。1.1.2外顯行為將與過去一致,關閉首頁游標懸停時自動播放。主要是這樣沒登入也可以生效,而且那項設定好像也會讓頻道頁面中影片與右側推薦影片不會有懸停時的動圖(非自動播放、不影響觀看紀錄,只有幾幀影格循環的那個)。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

YouTube 停用自動播放
功能
安裝
在瀏覽器安裝 Tampermonkey 擴充套件後,點擊此頁面上的「Raw」按鈕,然後點擊「安裝」即可。
YouTube Disable Autoplaying
Features
Installation
Ensure Tampermonkey installed on your browser. Click the "Raw" button on this page, and then click the "Install".