disable-youtube-autoplay/disable-youtube-autoplay.user.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-02-04 22:58:41 -05:00
// ==UserScript==
// @name Turn off Youtube Autoplay
// @namespace https://bubbletea.dev/
2024-07-17 17:49:31 -04:00
// @version 2.1.0
2021-02-04 22:58:41 -05:00
// @description Disables Youtube recommended videos from automatically playing
// @author shibao
// @include https://www.youtube.com/watch*
2022-01-06 22:22:13 -05:00
// @downloadURL https://gitea.bubbletea.dev/shibao/disable-youtube-autoplay/raw/branch/stable/disable-youtube-autoplay.user.js
// @updateURL https://gitea.bubbletea.dev/shibao/disable-youtube-autoplay/raw/branch/stable/disable-youtube-autoplay.user.js
2021-02-04 22:58:41 -05:00
// @grant none
// ==/UserScript==
2022-01-06 22:22:13 -05:00
// from https://developer.mozilla.org/en-US/docs/Web/API/Node#recurse_through_child_nodes
function eachNode(rootNode, callback) {
if (!callback) {
2024-07-17 17:49:37 -04:00
const nodes = [];
2022-01-06 22:22:13 -05:00
eachNode(rootNode, function (node) {
2024-07-17 17:49:37 -04:00
nodes.push(node);
});
return nodes;
2022-01-06 22:22:13 -05:00
}
if (false === callback(rootNode)) {
2024-07-17 17:49:37 -04:00
return false;
2022-01-06 22:22:13 -05:00
}
if (rootNode.hasChildNodes()) {
2024-07-17 17:49:37 -04:00
const nodes = rootNode.childNodes;
2022-01-06 22:22:13 -05:00
for (let i = 0, l = nodes.length; i < l; ++i) {
if (false === eachNode(nodes[i], callback)) {
2024-07-17 17:49:37 -04:00
return;
2022-01-06 22:22:13 -05:00
}
}
2021-02-04 22:58:41 -05:00
}
}
(function () {
2024-07-17 17:49:37 -04:00
"use strict";
2022-01-06 22:22:13 -05:00
const observer = new MutationObserver(function (mutationList) {
for (const mutation of mutationList) {
for (const addedNode of mutation.addedNodes) {
// recurses through all child nodes as well
eachNode(addedNode, function (node) {
2024-07-17 17:49:37 -04:00
node.nodeName == "DIV" &&
node.classList.contains("ytp-autonav-toggle-button") &&
node.getAttribute("aria-checked") == "true" &&
2024-07-17 17:49:31 -04:00
node.click();
2022-01-06 22:22:13 -05:00
});
}
2024-07-17 17:49:37 -04:00
}
2022-01-06 22:22:13 -05:00
});
observer.observe(document, {
childList: true,
subtree: true,
2024-07-17 17:49:37 -04:00
attributes: true,
2022-01-06 22:22:13 -05:00
});
})();