DCInside Auto Dark Mode (Desktop)

DCInside 다크 모드를 브라우저 설정에 맞춰 자동으로 전환

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         DCInside Auto Dark Mode (Desktop)
// @namespace    https://dcinside.com/
// @version      1.2
// @description  DCInside 다크 모드를 브라우저 설정에 맞춰 자동으로 전환
// @author       iwj9
// @match        https://dcinside.com/*
// @match        https://*.dcinside.com/*
// @exclude      https://m.dcinside.com/*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
    let currentUrl = location.href;

    function isDarkActive() {
        return !!document.getElementById('css-darkmode');
    }

    function applyBuiltIn() {
        try {
            if (typeof window.darkmode === 'function') {
                window.darkmode();
                return true;
            }
        } catch (e) {}
        return false;
    }

    function setCookies(val) {
        const expire = "expires=Thu, 01 Jan 9999 00:00:00 GMT";
        document.cookie = `used_darkmode=${val}; path=/; domain=.dcinside.com; ${expire}`;
        document.cookie = `darkmode=${val}; path=/; domain=.dcinside.com; ${expire}`;
    }

    function injectCSS() {
        if (isDarkActive()) return;
        setCookies(1);
        const link = document.createElement('link');
        link.id = 'css-darkmode';
        link.rel = 'stylesheet';
        link.href = 'https://nstatic.dcinside.com/dc/w/css/dark.css?v=8';
        document.head.appendChild(link);
    }

    function removeCSS() {
        if (!isDarkActive()) return;
        setCookies(0);
        const link = document.getElementById('css-darkmode');
        link && link.remove();
    }

    function swapImages(dark) {
        // 로고 및 안내 이미지
        requestIdleCallback(() => {
            const img = selector => document.querySelector(selector);
            const logo = img('.logo_img');
            const logo2 = img('.logo_img2');
            const guide = img('.minor_make_guide img');

            if (logo) logo.src = dark
                ? 'https://nstatic.dcinside.com/dc/w/images/dark/dcin_logo_dark.png'
                : (window.logo_img || logo.src);

            if (logo2 && typeof window.logo_prefix !== 'undefined') {
                logo2.src = dark
                    ? `https://nstatic.dcinside.com/dc/w/images/dark/tit_${logo_prefix}gallery_dark.png`
                    : `https://nstatic.dcinside.com/dc/w/images/tit_${logo_prefix}gallery.png`;
            }

            if (guide) guide.src = dark
                ? 'https://nstatic.dcinside.com/dc/w/images/dark/minor_infoimg2_dark.png'
                : 'https://nstatic.dcinside.com/dc/w/images/minor_infoimg2.png';
        });
    }

    function enableDark() {
        if (applyBuiltIn()) return;
        injectCSS();
        swapImages(true);
    }

    function disableDark() {
        if (applyBuiltIn()) return;
        removeCSS();
        swapImages(false);
    }

    function syncMode() {
        const wantDark = darkQuery.matches;
        const isNowDark = isDarkActive();
        if (wantDark && !isNowDark) enableDark();
        else if (!wantDark && isNowDark) disableDark();
    }

    // 초기, 로드 이후, 설정 변경 감지
    syncMode();
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', syncMode);
    }
    // 더 이상 실시간으로 다크모드를 갱신하지 않음.  
    // darkQuery.addEventListener('change', syncMode);

})();