Skip to content

Instantly share code, notes, and snippets.

@lumynou5
Last active May 14, 2025 18:05
Show Gist options
  • Save lumynou5/b036f405a0888bf9c3b9a3f560e36f3d to your computer and use it in GitHub Desktop.
Save lumynou5/b036f405a0888bf9c3b9a3f560e36f3d to your computer and use it in GitHub Desktop.
Disable YouTube autoplaying everywhere.
// ==UserScript==
// @name YouTube Disable Autoplaying
// @version 0.4.3
// @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
// ==/UserScript==
'use strict';
const querySelectorAsync = (function () {
const callbacks = new Set();
const observer = new MutationObserver((mutations) => {
for (const {callback, selector} of callbacks) {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeType !== Node.ELEMENT_NODE) continue;
if (node.matches(selector)) callback(node);
}
}
}
});
observer.observe(document, {childList: true, subtree: true});
return function (selector) {
return new Promise((resolve) => {
const elm = document.querySelector(selector)
if (elm) {
resolve(elm);
return;
}
callbacks.add({callback: resolve, selector});
});
};
})();
const manager = document.getElementsByTagName('yt-playlist-manager')[0];
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);
let state = false;
const changeState = (newState) => {
state = newState;
manager.set('canAutoAdvance_', state);
inner.setAttribute('aria-checked', state.toString());
};
button.addEventListener('click', () => changeState(!state));
(async () => {
const preview = await querySelectorAsync('ytd-video-preview');
preview.remove();
})();
document.addEventListener('yt-navigate-finish', async () => {
if (location.pathname === '/watch' && location.search.match(/\?(?:.*&)?list=.*/)) {
changeState(state);
document.querySelector('#content #playlist-action-menu #flexible-item-buttons').replaceChildren(button);
} else if (location.pathname.startsWith('/shorts/')) {
let player = await querySelectorAsync('#shorts-player');
setTimeout(() => player.setLoopVideo(false), 1000);
}
});
@lumynou5
Copy link
Author

YouTube 停用自動播放

功能

  • 在播放清單上新增按鈕,用以切換是否自動播放下一個項目。
  • 防止 Shorts 自動重播。

安裝

在瀏覽器安裝 Tampermonkey 擴充套件後,點擊此頁面上的「Raw」按鈕,然後點擊「安裝」即可。

YouTube Disable Autoplaying

Features

  • Adds a button on playlists to toggle autoplaying.
  • Prevents Shorts from auto-looping.

Installation

Ensure Tampermonkey installed on your browser. Click the "Raw" button on this page, and then click the "Install".

@leadra
Copy link

leadra commented Nov 5, 2024

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

@lumynou5
Copy link
Author

lumynou5 commented Nov 6, 2024

@leadra 改好了。0.4.0

PS:炒飯妹妹很可愛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment