YouTube Auto Like

Automatically likes a video or livestream on YouTube

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

// ==UserScript==
// @name         YouTube Auto Like
// @namespace    http://tampermonkey.net/
// @version      1.10
// @description  Automatically likes a video or livestream on YouTube
// @author       Yukiteru
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @require      https://gf.qytechs.cn/scripts/470224-tampermonkey-config/code/Tampermonkey%20Config.js
// @license      MIT
// ==/UserScript==

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

const config_desc = {
  ratio: {
    name: 'Like after percentage',
    processor: 'int_range-1-100',
    value: 50
  },
  only_sub: {
    name: 'Only like subscribed channels',
    input: "current",
    processor: "not",
    formatter: "boolean",
    value: true,
  }
}
const config = GM_config(config_desc);

// window.addEventListener(GM_config_event, (e) => {
  // console.log(e.detail);
  // console.log(checkSub())
// });

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 shouldLike() {
  if (isSubscribed()) return true
  return !config.only_sub
}

function isLivestream() {
  const liveBadge = document.querySelector('.ytp-live');
  return liveBadge !== 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");
  const percentage = config.ratio / 100
  if (video.currentTime / video.duration > percentage && shouldLike()) {
    like(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() || !shouldLike()) return false; // exit if already liked
    if (isLivestream() && shouldLike()) return like(); // like and exit if this 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或关注我们的公众号极客氢云获取最新地址