YouTube Auto Like

Automatically likes a video or livestream on YouTube

当前为 2023-12-21 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Auto Like
// @namespace    http://tampermonkey.net/
// @version      1.11
// @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://greasyfork.org/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();