YouTube autolike

Auto-like YouTube video if you are subscribed to the author's channel.

Tính đến 24-03-2021. Xem phiên bản mới nhất.

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

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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 autolike
// @namespace    https://github.com/blackstar0169/youtube-autolike-script
// @version      1.1
// @description  Auto-like YouTube video if you are subscribed to the author's channel.
// @author       blackstar0169
// @match        https://www.youtube.com/watch*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var watcher = null;
    var likeSelector = '#info-contents #top-level-buttons > ytd-toggle-button-renderer > a';
    var subscribeSelector = '.ytd-subscribe-button-renderer';

    // Check if the video is already liked
    function isLiked() {
        var likeBtn = document.querySelector(likeSelector);
        return likeBtn ? likeBtn.querySelector(':scope > .style-default-active') != null : false;
    }

    function autoLike() {
        var subscribeBtn = document.querySelector(subscribeSelector + '[subscribed]');

        // If the button exsits, the user is subscribed to this channel
        if (subscribeBtn) {
            var likeBtn = document.querySelector(likeSelector);

            if (likeBtn && !isLiked()) {
                likeBtn.click();
                return true;
            }
        }

        return false;
    }

    // Watch untile the like button has or has been pressed
    function startWatcher() {
        watcher = setInterval(function () {
            if (document.querySelector(subscribeSelector)) {
                if (isLiked() || autoLike()) {
                    clearInterval(watcher);
                }
            }
        }, 1000);
    }

    // Wait for the interface load event
    window.addEventListener("yt-navigate-finish", startWatcher, true)
})();