Chzzk script

자동 최고화질 설정, 광고 차단 팝업 삭제

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Chzzk script
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  자동 최고화질 설정, 광고 차단 팝업 삭제
// @author       author
// @match        *://*.chzzk.naver.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chzzk.naver.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const script = document.createElement('script');
    script.src = 'https://unpkg.com/[email protected]/dist/xhook.min.js';
    script.onload = function() {
        window.xhook.after((request, response) => {
            if (!response || !response.text) return;
            const url = request.url;

            if (url.includes("/live-detail") || /\/service\/v\d+\/videos\/\d+/.test(url)) {
                try {
                    const data = JSON.parse(response.text);
                    if (data && data.content && typeof data.content === "object") {
                        data.content.dab = false;
                        response.text = JSON.stringify(data);
                    }
                } catch (e) {
                }
            }
        });
    };
    document.head.appendChild(script);

    window.addEventListener('DOMContentLoaded', () => {
        let lastUrl = location.href;
        let qualityInterval = null;
        let playbackInterval = null;

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

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

            settingsButton.click();

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

                setTimeout(() => {
                    const qualityOptions = document.querySelectorAll('li[class*="quality-item"]');
                    let qualityOption = Array.from(qualityOptions).find(option => option.textContent.includes('1080')) ||
                                        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
                    });

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

                    qualityOption.dispatchEvent(enterEvent);
                }, 50);
            }, 50);

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

        function startQualityCheck() {
            if (!qualityInterval) {
                qualityInterval = setInterval(selectHighestQuality, 50);
            }
        }

        document.addEventListener('click', () => {
            setTimeout(() => {
                if (location.href !== lastUrl) {
                    lastUrl = location.href;
                    if (lastUrl.includes('/live/') || lastUrl.includes('/video/')) {
                        startQualityCheck();
                    }
                }
            }, 100);
        });

        if (location.href.includes('/live/') || location.href.includes('/video/')) {
            startQualityCheck();
        }
    });
})();