YouTube Auto Like

Auto likes a video or livestream on YouTube

目前为 2023-12-19 提交的版本。查看 最新版本

// ==UserScript==
// @name         YouTube Auto Like
// @namespace    http://tampermonkey.net/
// @version      1.01
// @description  Auto likes a video or livestream on YouTube
// @author       Yukiteru
// @match        https://www.youtube.com/watch*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license      MIT
// ==/UserScript==

function printLog(message) {
  console.log(`[YouTube Auto Like]: ${message}`);
}

function getLikeButton() {
  return document.querySelector("like-button-view-model button");
}

function isLiked() {
  return getLikeButton().getAttribute("aria-pressed") === "true";
}

function isSubscribed() {
  const subscribeButton = document.querySelector(
    "ytd-subscribe-button-renderer"
  );
  return subscribeButton.hasAttribute("subscribed");
}

function isLivestream() {
  const metaTag = document.querySelector("meta[itemprop='isLiveBroadcast']");
  return metaTag !== null;
}

function like(video) {
  if (isLiked()) printLog('user liked manually')
  else getLikeButton().click();
  video.removeEventListener("timeupdate", listener);
}

function listener() {
  const video = document.querySelector("video.video-stream");
  if (video.currentTime / video.duration > 0.8 && isSubscribed()) {
    like(video); // like after watched 80 percent of the video
  }
}

function findLikeButton() {
  const observer = new MutationObserver((mutations, observer) => {
    if (!getLikeButton()) return; // Keep trying to check like status
    const likeStatus = getLikeButton().getAttribute("aria-pressed");
    if (!likeStatus) return;

    printLog("like status checked");
    observer.disconnect();

    if (isLiked() || !isSubscribed()) return false; // exit if already liked or not subscribed
    if (isLivestream() && isSubscribed()) return like(); // like and exit if subscribed and is a livestream

    const video = document.querySelector("video.video-stream");
    video.addEventListener("timeupdate", listener);
  });
  observer.observe(document, { childList: true, subtree: true });
}

findLikeButton();

QingJ © 2025

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