본문 바로가기
Tamper Monkey Code #IT 용어
// ==UserScript==
// @name         Click
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Automatically clicks the button
// @match        https://*.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    if (!window.location.href.includes('youtube.com')) {
        return; // YouTube 페이지가 아니면 스크립트 종료
    }

    let observer = null; // Declare observer outside the functions

    function go() {
        const video = document.querySelector('video');
        if (video) {
            // Check if the video duration is a valid number
            if (isFinite(video.duration)) {
                if (video.duration <= 270) { // less than 4.5 minutes case
                    video.currentTime = video.duration - 1;
                    setTimeout(() => { // 500ms delay
                        clickGoButton();
                    }, 500);
                }
            } else {
                console.warn("Video duration is not finite yet.  Waiting...");
                // Optionally, try again after a short delay
                setTimeout(go, 250); // Check again after 250ms
            }
        }
    }

    function clickGoButton() {
        const goButton = document.querySelector('.ytp-skip-ad-button');
        if (goButton) {
            goButton.click();
            console.log("광고 건너뛰기 버튼 클릭!");
        }
    }

    function observeAdChanges() {
        console.log("Entering ObserveAdChanges");
        const targetNode = document.body; // Observe changes in the body
        const config = { attributes: true, childList: true, subtree: true };

        // Define a callback function to execute when mutations are observed
        const callback = function(mutationsList, observer) {
            for(const mutation of mutationsList) {
                if (mutation.type === 'childList') {
                    console.log("childList Mutated");
                    // Check if ad is showing
                    //if (document.querySelector('.ad-showing')) {
                        console.log('Ad is showing (MutationObserver)');
                        go();
                        break; // Exit loop after finding the ad
                    //}
                } else if (mutation.type === 'attributes') {
                    console.log("attribution mutated");
                    // Handle attribute changes if needed
                }
            }
        };

        // Create an observer instance linked to the callback function
        observer = new MutationObserver(callback);

        // Start observing the target node for configured mutations
        observer.observe(targetNode, config);
    }


    // Start observing
    observeAdChanges();


    // Optional: Disconnect the observer when the script is disabled or no longer needed
    // if (module && module.exports) {
    //     module.exports.disconnectObserver = function() {
    //         if (observer) {
    //             observer.disconnect();
    //             console.log("MutationObserver disconnected.");
    //         }
    //     };
    // }

})();

댓글