Greasy Fork

Greasy Fork is available in English.

字体与大小调整

实时修改字体和大小,并通过菜单操作,无须刷新,自动保存设置。

当前为 2025-05-07 提交的版本,查看 最新版本

// ==UserScript==
// @name         字体与大小调整
// @namespace    http://tampermonkey.net/
// @history      1.0.0 初始版本
// @version      1.0.1
// @description  实时修改字体和大小,并通过菜单操作,无须刷新,自动保存设置。
// @author       pcysanji
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @run-at       document-start
// @exclude      *://weread.qq.com/*
// ==/UserScript==

(function () {
    'use strict';

    const CUSTOM_FONT = "'PingFang SC bold', 'Segoe UI Emoji'";
    const STYLE_ID = "custom-font-style";

    const SIZE_KEY = 'font_size_scale';
    const STEP_KEY = 'font_step';
    const FONT_KEY = 'font_enable';

    let currentScale = parseFloat(localStorage.getItem(SIZE_KEY)) || 1.0;
    let fontStep = parseFloat(localStorage.getItem(STEP_KEY)) || 0.1;
    let fontEnabled = localStorage.getItem(FONT_KEY) !== 'false'; // 默认启用字体
    const menuHandles = {};

    function applyStyles() {
        let style = document.getElementById(STYLE_ID);
        if (!style) {
            style = document.createElement("style");
            style.id = STYLE_ID;
            document.head.appendChild(style);
        }

        const fontFamily = fontEnabled ? `font-family: ${CUSTOM_FONT} !important;` : '';
        style.innerText = `
            html, body, *:not(i) {
                ${fontFamily}
                font-size: ${currentScale}rem !important;
            }
        `;
    }

    function updateMenu() {
        for (let key in menuHandles) {
            if (menuHandles[key]) GM_unregisterMenuCommand(menuHandles[key]);
        }

        menuHandles.status = GM_registerMenuCommand(`📏 当前字体大小 ${currentScale.toFixed(2)}rem`, () => {}, { autoClose: false });

        menuHandles.increase = GM_registerMenuCommand("🔼 放大字体", () => {
            currentScale = Math.min(currentScale + fontStep, 5);
            saveAndApply();
        }, { autoClose: false });

        menuHandles.decrease = GM_registerMenuCommand("🔽 缩小字体", () => {
            currentScale = Math.max(currentScale - fontStep, 0.5);
            saveAndApply();
        }, { autoClose: false });

        menuHandles.resetSize = GM_registerMenuCommand("🔁 恢复默认字体大小", () => {
            currentScale = 1.0;
            saveAndApply();
        }, { autoClose: false });

        menuHandles.setStep = GM_registerMenuCommand(`⚙️ 设置缩放步长(当前 ${fontStep})`, () => {
            const input = prompt("请输入缩放步长,例如 0.1", fontStep.toString());
            const val = parseFloat(input);
            if (!isNaN(val) && val > 0 && val <= 5) {
                fontStep = val;
                localStorage.setItem(STEP_KEY, fontStep);
                updateMenu();
            } else {
                alert("无效的输入,请输入一个正数");
            }
        }, { autoClose: false });

        menuHandles.enableFont = GM_registerMenuCommand("✅ 修改字体", () => {
            fontEnabled = true;
            localStorage.setItem(FONT_KEY, 'true');
            applyStyles();
        }, { autoClose: false });

        menuHandles.disableFont = GM_registerMenuCommand("❌ 恢复字体", () => {
            fontEnabled = false;
            localStorage.setItem(FONT_KEY, 'false');
            applyStyles();
        }, { autoClose: false });
    }

    function saveAndApply() {
        localStorage.setItem(SIZE_KEY, currentScale);
        applyStyles();
        updateMenu();
    }

    // 初始化样式
    applyStyles();
    updateMenu();
})();