PTT預設字型使用微軟正黑體

PTT字型用微軟正黑體比較不傷眼

目前为 2023-08-03 提交的版本。查看 最新版本

// ==UserScript==
// @name         PTT預設字型使用微軟正黑體
// @namespace    https://github.com/livinginpurple
// @version      2023.08.03.08
// @description  PTT字型用微軟正黑體比較不傷眼
// @license      WTFPL
// @author       livinginpurple
// @match        https://*.ptt.cc/*
// @match        https://disp.cc/b/*
// @match        https://disp.cc/m/*
// @run-at       document-end
// @grant        GM.xmlHttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
    'use strict';
    const scriptName = GM_info.script.name;
    console.log(`${scriptName} is loading.`);
    const fontSetting = 'fontSetting';

    changeFont(GM_getValue(fontSetting));

    const options = [
        {label: "微軟正黑體", value: "Microsoft JhengHei"},
        {label: "Noto Sans CJK TC Regular", value: "Noto Sans CJK TC Regular"},
        {label: "Noto Sans TC", value: "Noto Sans TC"},
        {label: "Noto Serif TC", value: "Noto Serif TC"},
        {label: "微軟雅黑體", value: "Microsoft YaHei"}
    ];

    let container;

    GM_registerMenuCommand("字型設定", openOptionsMenu);

    function openOptionsMenu() {
        container = document.createElement("div");
        container.style.position = "absolute";
        container.style.top = "50%";
        container.style.left = "50%";
        container.style.transform = "translate(-50%, -50%)";
        container.style.padding = "20px";
        container.style.borderRadius = "10px";
        container.style.backgroundColor = "#fff";
        container.style.boxShadow = "0px 0px 10px rgba(0, 0, 0, 0.5)";
        container.style.zIndex = "9999";

        const label = document.createElement("label");
        label.textContent = "請選擇字型:";
        label.style.display = "block";
        label.style.marginBottom = "5px";
        label.style.fontSize = "14px";
        label.style.fontWeight = "bold";

        const select = document.createElement("select");
        select.style.marginBottom = "10px";
        select.style.width = "100%";
        select.style.height = "30px";
        select.style.border = "1px solid #ccc";
        select.style.borderRadius = "5px";
        select.style.backgroundColor = "#fff";
        select.style.fontSize = "14px";
        select.style.color = "#333";
        select.style.padding = "5px";

        options.forEach(option => {
            const optionElement = document.createElement("option");
            optionElement.value = option.value;
            optionElement.textContent = option.label;
            select.appendChild(optionElement);
        });

        const button = document.createElement("button");
        button.style.width = "100%";
        button.style.height = "30px";
        button.style.border = "none";
        button.style.borderRadius = "5px";
        button.style.backgroundColor = "#007bff";
        button.style.color = "#fff";
        button.style.fontSize = "14px";
        button.style.cursor = "pointer";
        button.textContent = "Save";
        button.addEventListener("click", saveOptions);

        container.appendChild(label);
        container.appendChild(select);
        container.appendChild(button);

        document.body.appendChild(container);
    }

    function saveOptions() {
        const select = document.querySelector("select");
        const selectedOption = select.value;
        GM_setValue(fontSetting, selectedOption);
        changeFont(selectedOption);
        container.remove();
    }

    function changeFont(font) {
        switch (document.domain) {
            case "disp.cc":
                $('body').css({ 'font-family': font });
                break;
            default:
                $('.bbs-content').css({ 'font-family': font });
                break;
        }
        console.log(`字型已切換成:${font}`);
    }
    console.log(`${scriptName} is running.`);
})();