youtube Automatic like

Watch the video for 2 minutes and like it to support the video you like

// ==UserScript==
// @name         youtube Automatic like
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Watch the video for 2 minutes and like it to support the video you like
// @author       You
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// ==/UserScript==

(function() {
    const titleEl = document.querySelector("title");

    const observer = new MutationObserver(() => {
        console.log("偵測到影片切換!");
        runMyScript();
    });

    observer.observe(titleEl, { childList: true });

    function runMyScript() {
        const video = document.querySelector("video.html5-main-video");
        const currentUrl = window.location.href; 
        if ((video)||(currentUrl.includes("watch?v="))) {
            console.log("找到新影片:", video);

            function isVideoPlaying(video) {
                return !!(video && !video.paused && !video.ended && video.readyState > 2);
            }

            setTimeout(() => {
                const video = document.querySelector('video.html5-main-video');
                if (isVideoPlaying(video)) {
                    console.log('30秒後:正在播放,執行按讚');
                    triggerLikeButton();
                } else {
                    console.log('30秒後:未播放,不執行');
                }
            }, 120000);

            function triggerLikeButton() {
                const likeButton = document.querySelector('button[title="我喜歡"]');
                if (likeButton) {
                    likeButton.click();
                    console.log('已按讚');
                    showMsg();
                } else {
                    console.log('找不到按讚按鈕');
                }
            }

            function showMsg() {
                const msg = document.createElement('div');
                msg.textContent = '已按讚';
                Object.assign(msg.style, {
                    position: 'fixed',
                    left: '50%',
                    transform: 'translateX(-50%)',
                    top: '30%',
                    padding: '10px 20px',
                    background: 'rgba(0,0,0,0.8)',
                    color: '#fff',
                    borderRadius: '8px',
                    zIndex: 9999,
                    fontSize: '16px',
                    opacity: '0',
                    transition: 'opacity 0.3s'
                });
                document.body.appendChild(msg);

                setTimeout(() => { msg.style.opacity = '1'; }, 10);
                setTimeout(() => {
                    msg.style.opacity = '0';
                    setTimeout(() => {
                        document.body.removeChild(msg);
                    }, 300);
                }, 2500);
            }
        }
    }
})();