YouTube Auto Like

Automatically likes a video or livestream on YouTube

目前為 2023-12-21 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();