Chzzk Auto High Quality

Automatically select highest quality on Chzzk streams

// ==UserScript==
// @name         Chzzk Auto High Quality
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Automatically select highest quality on Chzzk streams
// @author       DSK
// @match        https://chzzk.naver.com/*
// @grant        none
// @license MIT
// ==/UserScript==
 
(function() {
    'use strict';
 
    let lastUrl = location.href;
 
    function handleAdBlockPopup() {
        const popupContainer = document.querySelector('div[class^="popup_container"]');
        if (popupContainer && popupContainer.textContent.includes('광고 차단 프로그램을 사용 중이신가요')) {
            popupContainer.remove();
        }
    }
 
    function selectHighestQuality() {
        const settingsButton = document.querySelector('button[class*="pzp-pc-setting-button"]');
        if (!settingsButton) return;
 
        settingsButton.click();
 
        const qualityButton = document.querySelector('div[class*="pzp-pc-setting-intro-quality"]');
        if (!qualityButton) return;
        qualityButton.click();
 
        // Find quality option that contains "1080" or "720"
        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;
 
        qualityOption.focus();
        const enterEvent = new KeyboardEvent('keydown', {
            bubbles: true,
            cancelable: true,
            key: 'Enter',
            code: 'Enter',
            keyCode: 13,
            which: 13
        });
        qualityOption.dispatchEvent(enterEvent);
    }
 
    // Listen for click events
    document.addEventListener('click', () => {
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            if (lastUrl.includes('/live/') || lastUrl.includes('/video/')) {
                setTimeout(() => {
                    handleAdBlockPopup();
                }, 500);
                setTimeout(() => {
                    handleAdBlockPopup();
                }, 1000);
                setTimeout(() => {
                    handleAdBlockPopup();
                    selectHighestQuality();
                }, 2000);
            }
            document.body.style.overflow = 'auto';
            document.documentElement.style.overflow = 'auto';
            document.body.style.position = 'relative';
            document.documentElement.style.position = 'relative';
        }
    });
     setTimeout(() => {
        handleAdBlockPopup();
    }, 500);
    setTimeout(() => {
        handleAdBlockPopup();
    }, 1000);
    // Execute once when page loads
    setTimeout(() => {
        handleAdBlockPopup();
        selectHighestQuality();
    }, 2000);
    // 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();
        }
    });
})();