Greasy Fork

chzzk-1080p

치지직 1080p 해상도로 고정합니다

当前为 2024-11-12 提交的版本,查看 最新版本

// ==UserScript==
// @name         chzzk-1080p
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  치지직 1080p 해상도로 고정합니다
// @match        *://chzzk.naver.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function setResolution() {
        const qualityItems = document.querySelectorAll(
            '.pzp-pc-setting-quality-pane__list-container li'
        );

        if (!qualityItems) {
            console.error("해상도 설정 요소를 찾을 수 없습니다.");
            return;
        }

        let targetQuality = null;

        for (let i = 0; i < qualityItems.length; i++) {
            const item = qualityItems[i];
            if (item.textContent.includes("1080p")) {
                targetQuality = item;
                break;
            } else if (item.textContent.includes("720p")) {
                targetQuality = item;
            }
        }

        if (targetQuality) {
            targetQuality.click();
            console.log("해상도가 설정되었습니다. (" + targetQuality.textContent + ")");
        } else {
            console.error("1080p 또는 720p 해상도를 찾을 수 없습니다.");
        }
    }

    function fixScrollIssue() {
        try {
            document.body.style.overflow = 'auto';
            document.documentElement.style.overflow = 'auto';
        } catch(e){
            console.error("overflow 속성 조정 실패:", e);
        }

        try {
            document.body.style.position = 'relative';
            document.documentElement.style.position = 'relative';
        } catch(e) {
            console.error("position 속성 조정 실패:", e);
        }

        try {
            document.body.style.height = 'auto';
            document.documentElement.style.height = 'auto';
        } catch(e) {
            console.error("height 속성 조정 실패:", e);
        }
        console.log("스크롤 문제가 수정되었습니다.");
    }

    function applySettingsOnSpecificPages() {
        if (
            location.href.startsWith("https://chzzk.naver.com/live/") ||
            location.href.startsWith("https://chzzk.naver.com/video")
        ) {
            setTimeout(() => {
                setResolution();
            }, 1000);
            fixScrollIssue();
        } else {
            fixScrollIssue();
        }
    }

    // 페이지 로드 시 설정 적용
    window.addEventListener("load", applySettingsOnSpecificPages);

    // URL 변경 감지하여 설정 재적용 - 더 안정적인 방법
    let lastUrl = location.href;
    const urlCheckInterval = setInterval(() => {
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            applySettingsOnSpecificPages();
        }
    }, 2000); // 2초 간격으로 확인. 너무 짧으면 성능 저하 가능.

    // 페이지 새로고침 후 설정 재적용
    window.addEventListener("unload", () => {
        setTimeout(() => {
            applySettingsOnSpecificPages();
        }, 1000);
    });

    // 해상도 설정 변경 후 요청 확인
    function monitorNetworkRequests() {
        let previousRequestCount = 0;

        const observer = new MutationObserver(() => {
            const xhrs = document.querySelectorAll('iframe[src*="ex-nlive-streaming.navercdn.com"]');
            if (xhrs.length > previousRequestCount) {
                previousRequestCount = xhrs.length;
                console.log("해상도 설정이 변경되었습니다. 요청이 발생하였습니다.");
            }
        });

        observer.observe(document.body, { childList: true, subtree: true });
    }

    monitorNetworkRequests();
})();