YouTube Auto-Liker

Hit Like button if Subbed

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name YouTube Auto-Liker
// @description Hit Like button if Subbed
// @match https://www.youtube.com/watch*
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Logo_of_YouTube_%282015-2017%29.svg/2560px-Logo_of_YouTube_%282015-2017%29.svg.png
// @version 5.1
// @license MIT

// @namespace https://greasyfork.org/users/803889
// ==/UserScript==




function ytLiker() {
    SUBSCRIBE_BUTTON = document.querySelector('#subscribe-button-shape .yt-core-attributed-string--white-space-no-wrap').innerHTML == 'Subscribed'
    LIKE_BUTTON = document.querySelector('button.yt-spec-button-shape-next--segmented-start')

    if (!SUBSCRIBE_BUTTON) {
        console.log('YouTube Auto-Liker: You\'re not subscribed!')
        return
    }
    else if (LIKE_BUTTON.ariaPressed == 'false') {
        LIKE_BUTTON.click()
        console.log('YouTube Auto-Liker: Video Liked!')
        return
    }
    else {
        console.log('YouTube Auto-Liker: Video Already Liked!')
        return
    }
}


function setupObserver() {
    const observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            // && document.querySelector('button.yt-spec-button-shape-next--segmented-start')
            if (mutation.type === "attributes" && mutation.attributeName === 'video-id') { //&& mutation.attributeName === 'yt-spec-touch-feedback-shape__fill'
                console.log('YouTube Auto-Liker: Video ID changed. Checking if subscribed and liking')
                counter = 0
                querySelector = setInterval(function checker() {
                    if (document.querySelector('button.yt-spec-button-shape-next--segmented-start')) {
                        ytLiker()
                        clearInterval(querySelector)
                    } else {
                        counter++
                        if (counter <= 15) {
                            console.log('YouTube Auto-Liker: --Counter limited reached-- Could not like video or setup observer. Try refreshing the page.?')
                            clearInterval(querySelector)
                            return
                        }
                    }
                }, 2000)
            }
        })
    })

    watchMetadata = document.querySelector('ytd-watch-metadata.watch-active-metadata.style-scope.ytd-watch-flexy.style-scope.ytd-watch-flexy')
    observer.observe(watchMetadata, {
        attributes: true
    })
    console.log('YouTube Auto-Liker: Monitoring for new videos')
}

counter = 0
try {
    interval_documentHidden = setInterval(function checker() {
        if (!document.hidden && document.readyState === 'complete' && (document.querySelector('#subscribe-button-shape .yt-core-attributed-string--white-space-no-wrap') !== null) && (document.querySelector('button.yt-spec-button-shape-next--segmented-start') !== null)) {
            if (counter <= 15) {
                console.log('YouTube Auto-Liker: Checking if subscribed and liking')
                setupObserver()
                ytLiker()
                clearInterval(interval_documentHidden)
            } else {
                counter++
                if (counter <= 15) {
                    console.log('YouTube Auto-Liker: --Counter limited reached-- Could not like video or setup observer. Try refreshing the page.?')
                    clearInterval(interval_documentHidden)
                    return
                }
            }
        }
    }, 2000)
} catch (err) {
    console.log('YouTube Auto-Liker: Error occured\n######\n\nerr' + err + '\n\n######')
}