YouTube Auto Like

Automatically likes a video or livestream on YouTube

As of 2023-12-20. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         YouTube Auto Like
// @namespace    http://tampermonkey.net/
// @version      1.02
// @description  Automatically 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('.ytp-live');
  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();