치지직 1080p 고정 + 광고 팝업 삭제 + 자동 재생

치지직 1080p 고정 및 SVG 버튼 클릭 추가, 각 기능 별도로 실행 후 종료

目前为 2024-11-04 提交的版本。查看 最新版本

// ==UserScript==
// @name         치지직 1080p 고정 + 광고 팝업 삭제 + 자동 재생
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  치지직 1080p 고정 및 SVG 버튼 클릭 추가, 각 기능 별도로 실행 후 종료
// @match        *://chzzk.naver.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    let previousChannelId = null;
    let scriptRunning = false;

    // URL 변화를 감지하는 함수
    const detectChannelChange = () => {
        const currentUrl = window.location.href;
        const channelIdMatch = currentUrl.match(/live\/([a-f0-9]{32})/);
        const currentChannelId = channelIdMatch ? channelIdMatch[1] : null;

        if (currentChannelId && currentChannelId !== previousChannelId) {
            previousChannelId = currentChannelId;
            console.log("채널 변경 감지, 스크립트 재실행");
            if (!scriptRunning) {
                executeScripts();
            }
        }
    };

    // 주기적으로 URL 변경 확인 (5000ms마다 확인)
    setInterval(detectChannelChange, 5000);

    // 각 기능을 수행하는 스크립트 실행 함수
    function executeScripts() {
        scriptRunning = true;

        // 랜덤 지연 시간 생성 함수 (500ms ~ 1500ms 사이 랜덤)
        function getRandomDelay() {
            return Math.floor(Math.random() * 1000) + 500;
        }

        // 중복 실행 방지를 위한 플래그
        let qualitySet = false;
        let svgClicked = false;
        let adRemoved = false;

        // 모든 작업이 완료되었는지 확인하고 스크립트를 종료하는 함수
        function checkAndTerminate() {
            if (qualitySet && svgClicked && adRemoved) {
                console.log("모든 작업 완료, 스크립트 종료");
                scriptRunning = false;
                previousChannelId = null; // 이전 채널 ID 초기화하여 재실행 방지
                return true;
            }
            return false;
        }

        // 품질 설정을 1080p로 고정하는 코드
        const qualityInterval = setInterval(() => {
            if (qualitySet) {
                clearInterval(qualityInterval); // 플래그가 true이면 인터벌 종료
                return;
            }

            const qualityElement = document.querySelector(
                `.pzp-pc-setting-quality-pane__list-container > li:first-child:not(.pzp-pc-ui-setting-item--checked)`
            );
            if (qualityElement) {
                setTimeout(() => {
                    qualityElement.click();
                    console.log("1080p 고정");
                    qualitySet = true; // 작업 완료 플래그 설정
                    clearInterval(qualityInterval);
                    console.log("품질 설정 인터벌 종료");
                    if (checkAndTerminate()) {
                        return;
                    }
                }, getRandomDelay());
            }
        }, 500);

        // SVG 버튼 클릭 코드
        const svgInterval = setInterval(() => {
            if (svgClicked) {
                clearInterval(svgInterval); // 플래그가 true이면 인터벌 종료
                return;
            }

            const svgElement = document.querySelector(
                `svg.pzp-ui-icon__svg[viewBox="0 0 49 49"]`
            );

            if (svgElement) {
                // 마우스 클릭 이벤트 생성
                const clickEvent = new MouseEvent('click', {
                    view: window,
                    bubbles: true,
                    cancelable: true
                });

                setTimeout(() => {
                    svgElement.dispatchEvent(clickEvent); // SVG 요소에 클릭 이벤트 전송
                    console.log("<svg> 요소 클릭 완료");
                    svgClicked = true; // 작업 완료 플래그 설정
                    clearInterval(svgInterval); // 클릭 후 인터벌 종료
                    if (checkAndTerminate()) {
                        return;
                    }
                }, getRandomDelay());
            }
        }, 500);

        // 광고 팝업 제거 코드
        const adInterval = setInterval(() => {
            if (adRemoved) {
                clearInterval(adInterval); // 플래그가 true이면 인터벌 종료
                return;
            }

            const adbb = document.querySelector(`[class^="ad_block_title"]`);
            if (adbb) {
                setTimeout(() => {
                    const closeButton = document.querySelector(`[class^=popup_cell] > button`);
                    if (closeButton) {
                        closeButton.click();
                        console.log("Remove adblock");
                        adRemoved = true; // 작업 완료 플래그 설정
                        clearInterval(adInterval); // 광고 팝업을 찾으면 인터벌을 종료하여 한 번만 실행
                        if (checkAndTerminate()) {
                            return;
                        }
                    }
                }, getRandomDelay());
            }
        }, 500);
    }

    // 처음 페이지 로드 시 스크립트 실행
    if (!scriptRunning) {
        executeScripts();
    }
})();