Skip to content

Instantly share code, notes, and snippets.

@lumynou5
Last active November 28, 2025 08:21
Show Gist options
  • Select an option

  • Save lumynou5/b036f405a0888bf9c3b9a3f560e36f3d to your computer and use it in GitHub Desktop.

Select an option

Save lumynou5/b036f405a0888bf9c3b9a3f560e36f3d to your computer and use it in GitHub Desktop.
Disable YouTube autoplaying everywhere.
// ==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});
// ==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==
@leadra
Copy link

leadra commented Nov 19, 2025

了解,感謝^^

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