YouTube autolike

Auto-like YouTube video if you are subscribed to the author's channel. Or can set an option to like all videos regardless if you are subrscribed or not.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube autolike
// @namespace    https://github.com/blackstar0169/youtube-autolike-script
// @version      1.8
// @description  Auto-like YouTube video if you are subscribed to the author's channel. Or can set an option to like all videos regardless if you are subrscribed or not.
// @author       blackstar0169
// @match        https://www.youtube.com/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// @grant        GM_registerMenuCommand
// @license      CC-BY-NC-SA-4.0
// @require      https://update.greasyfork.org/scripts/521137/1505441/MonkeyConfigg.js
// ==/UserScript==

(function() {
    'use strict';

    var watcher = null;
    const likeSelector = '.ytLikeButtonViewModelHost button';
    const subscribeSelector = '#subscribe-button button';

    // Config
    const config = new MonkeyConfig({
        title: 'YouTube autolike Configuration',
        menuCommand: true,
        params: {
            like_all_videos: {
                type: 'checkbox',
                default: false
            }
        }
    });

    function getObjectProperty(obj, keyStr) {
        const keys = keyStr.split('.');

        for (var i = 0; i < keys.length; i++) {
            var key = keys[i];
            if (obj && Array.isArray(obj)) {
                key = parseInt(key);
                if (isNaN(key) || typeof obj[key] === 'undefined') {
                    return undefined;
                }
            } else if (!obj || !obj.hasOwnProperty(key)) {
                return undefined;
            }
            obj = obj[key];
        }
        return obj;
    }

    // Check if the video is already liked
    function isLiked() {
        const btn = document.querySelector(likeSelector);
        if (btn === null) {
            console.error("[YouTube autolike] can not find like button !");
            return false;
        }
        return btn.getAttribute('aria-pressed') == 'true';
    }

    function isSubscribed() {
        const btn = document.querySelector(subscribeSelector);
        if (btn === null) {
            console.error("[YouTube autolike] can not find subscribe button !");
            return false;
        }
        return btn.classList.contains('yt-spec-button-shape-next--tonal') ||
            getObjectProperty(window.ytInitialPlayerResponse, 'annotations.0.playerAnnotationsExpandedRenderer.featuredChannel.subscribeButton.subscribeButtonRenderer.subscribed');
    }

    // Try to click on the like button. Return true un success
    function autoLike() {
        // If the user is subscribed to this channel
        const likeAllVideos = config.get('like_all_videos');
        if (isSubscribed() || likeAllVideos) {
            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() {
        if (watcher === null) {
            watcher = setInterval(function () {
                if (isLiked() || autoLike()) {
                    clearInterval(watcher);
                    watcher = null;
                }
            }, 1000);
        }
    }

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