Chzzk Auto High Quality

Chzzk자동으로 최고화질로 설정

// ==UserScript==
// @name         Chzzk Auto High Quality
// @namespace    http://tampermonkey.net/
// @version      2.3
// @description  Chzzk자동으로 최고화질로 설정
// @author       DSK
// @match        https://chzzk.naver.com/*
// @grant        none
// @license MIT
// ==/UserScript==
(function() {
    'use strict';

    let lastUrl = location.href;
    let adBlockInterval = null;
    let qualityInterval = null;
    let playbackInterval = null;

    function handleAdBlockPopup() {
        const popupContainer = document.querySelector('div[class^="popup_container"]');
        if (popupContainer && popupContainer.textContent.includes('광고 차단 프로그램을 사용 중이신가요')) {
            popupContainer.remove();
            if (adBlockInterval) {
                clearInterval(adBlockInterval);
                adBlockInterval = null;
            }
            return true;
        }
        return false;
    }

    function handlePlaybackButton() {
        const playbackSwitchButton = document.querySelector('button.pzp-pc-playback-switch');
        if (playbackSwitchButton && !playbackSwitchButton.querySelector('rect')) {
            playbackSwitchButton.click();
            return false;
        }
        if (playbackInterval) {
            clearInterval(playbackInterval);
            playbackInterval = null;
        }
        return true;
    }

    function selectHighestQuality() {
        const settingsButton = document.querySelector('button[class*="pzp-pc-setting-button"]');
        if (!settingsButton) return false;

        settingsButton.click();

        const qualityButton = document.querySelector('div[class*="pzp-pc-setting-intro-quality"]');
        if (!qualityButton) return false;
        qualityButton.click();

        const qualityOptions = document.querySelectorAll('li[class*="quality-item"]');
        let qualityOption = Array.from(qualityOptions).find(option => option.textContent.includes('1080'));
        if (!qualityOption) {
            qualityOption = Array.from(qualityOptions).find(option => option.textContent.includes('720'));
        }
        if (!qualityOption) return false;

        qualityOption.focus();
        const enterEvent = new KeyboardEvent('keydown', {
            bubbles: true,
            cancelable: true,
            key: 'Enter',
            code: 'Enter',
            keyCode: 13,
            which: 13
        });

        if (!playbackInterval) {
            playbackInterval = setInterval(handlePlaybackButton, 100);
        }

        qualityOption.dispatchEvent(enterEvent);

        if (qualityInterval) {
            clearInterval(qualityInterval);
            qualityInterval = null;
        }
        return true;
    }

    function startIntervals() {
        if (!adBlockInterval) {
            adBlockInterval = setInterval(handleAdBlockPopup, 100);
        }
        if (!qualityInterval) {
            qualityInterval = setInterval(selectHighestQuality, 100);
        }
    }

    // Listen for click events
    document.addEventListener('click', () => {
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            if (lastUrl.includes('/live/') || lastUrl.includes('/video/')) {
                startIntervals();
            }
            document.body.style.overflow = 'auto';
            document.documentElement.style.overflow = 'auto';
            document.body.style.position = 'relative';
            document.documentElement.style.position = 'relative';
        }
    });

    // Execute once when page loads
    startIntervals();

    // Listen for 'k' key press
    document.addEventListener('keydown', (event) => {
        if (event.key === 'k') {
            const playbackSwitchButton = document.querySelector('button.pzp-pc-playback-switch');
            playbackSwitchButton.click();
        }
    });
    // Listen for 'm' key press
    document.addEventListener('keydown', (event) => {
        if (event.key === 'm') {
            const volumeButton = document.querySelector('button.pzp-pc-volume-button');
            volumeButton.click();
        }
    });
    // Listen for 't' key press
    document.addEventListener('keydown', (event) => {
        if (event.key === 't') {
            const viewmodeButton = document.querySelector('button.pzp-pc-viewmode-button');
            viewmodeButton.click();
        }
    });
    // Listen for 'f' key press
    document.addEventListener('keydown', (event) => {
        if (event.key === 'f') {
            const fullscreenButton = document.querySelector('button.pzp-pc-fullscreen-button');
            fullscreenButton.click();
        }
    });
})();