YouTube Auto Dislike

Because YouTube no longer displays the number of dislikes, the scripts automatically dislike all videos. Wait 15 seconds to see the effect.

目前為 2023-05-22 提交的版本,檢視 最新版本

// ==UserScript==
// @name        YouTube Auto Dislike
// @namespace   gqqnbig
// @match       https://www.youtube.com/watch?*
// @grant       none
// @version     1.2
// @author      gqqnbig
// @description Because YouTube no longer displays the number of dislikes, the scripts automatically dislike all videos. Wait 15 seconds to see the effect.
// @license     GNU General Public License v3.0
// ==/UserScript==


(function () {
	//from https://github.com/Anarios/return-youtube-dislike/blob/main/Extensions/UserScript/Return%20Youtube%20Dislike.user.js
	const LIKED_STATE = "LIKED_STATE";
	const DISLIKED_STATE = "DISLIKED_STATE";
	const NEUTRAL_STATE = "NEUTRAL_STATE";

	let isMobile = location.hostname == "m.youtube.com";
	let isShorts = () => location.pathname.startsWith("/shorts");


	function isInViewport(element) {
		const rect = element.getBoundingClientRect();
		const height = innerHeight || document.documentElement.clientHeight;
		const width = innerWidth || document.documentElement.clientWidth;
		return (
			// When short (channel) is ignored, the element (like/dislike AND short itself) is
			// hidden with a 0 DOMRect. In this case, consider it outside of Viewport
			!(rect.top == 0 && rect.left == 0 && rect.bottom == 0 && rect.right == 0) &&
			rect.top >= 0 &&
			rect.left >= 0 &&
			rect.bottom <= height &&
			rect.right <= width
		);
	}

	function getButtons() {
		if (isShorts()) {
			let elements = document.querySelectorAll(
				isMobile
					? "ytm-like-button-renderer"
					: "#like-button > ytd-like-button-renderer"
			);
			for (let element of elements) {
				if (isInViewport(element)) {
					return element;
				}
			}
		}
		if (isMobile) {
			return (
				document.querySelector(".slim-video-action-bar-actions .segmented-buttons") ??
				document.querySelector(".slim-video-action-bar-actions")
			);
		}
		if (document.getElementById("menu-container")?.offsetParent === null) {
			return (
				document.querySelector("ytd-menu-renderer.ytd-watch-metadata > div") ??
				document.querySelector("ytd-menu-renderer.ytd-video-primary-info-renderer > div")
			);
		} else {
			return document
				.getElementById("menu-container")
				?.querySelector("#top-level-buttons-computed");
		}
	}

	function getDislikeButton() {
		return getButtons().children[0].tagName ===
		"YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER"
			? getButtons().children[0].children[1] === undefined ? document.querySelector("#segmented-dislike-button") : getButtons().children[0].children[1]
			: getButtons().children[1];
	}

	function getLikeButton() {
		return getButtons().children[0].tagName ===
		"YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER"
			? document.querySelector("#segmented-like-button") !== null ? document.querySelector("#segmented-like-button") : getButtons().children[0].children[0]
			: getButtons().children[0];
	}

	function isVideoLiked() {
		if (isMobile) {
			return (
				getLikeButton().querySelector("button").getAttribute("aria-label") ==
				"true"
			);
		}
		return getLikeButton().classList.contains("style-default-active");
	}

	function isVideoDisliked() {
		if (isMobile) {
			return (
				getDislikeButton().querySelector("button").getAttribute("aria-label") ==
				"true"
			);
		}
		return getDislikeButton().classList.contains("style-default-active");
	}

	function getState() {
		if (isVideoLiked()) {
			return LIKED_STATE;
		}
		if (isVideoDisliked()) {
			return DISLIKED_STATE;
		}
		return NEUTRAL_STATE;
	}


	setTimeout(() => {
		if (getState() == NEUTRAL_STATE) {
			getDislikeButton().querySelector('button').click();
		}
	}, 15000);

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址