Force Webpage to Use PingFang Font

强制网页使用苹方字体,如果本地无法找到,则询问是否下载

当前为 2024-08-01 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Force Webpage to Use PingFang Font
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  强制网页使用苹方字体,如果本地无法找到,则询问是否下载
// @author       Your Name
// @match        *://*/*
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// @license MIT
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const fontURL = 'https://github.com/accr/koudaiHK_Web/blob/master/fonts/PingFang-SC-Regular/PingFang%20Regular.woff';
    const permanentCancelKey = 'permanentCancelPingFang';

    // 添加苹方字体的CSS样式
    function addPingFangFont() {
        GM_addStyle(`
            @font-face {
                font-family: 'PingFang';
                src: url('${fontURL}') format('woff');
                font-weight: normal;
                font-style: normal;
            }
            body, p, h1, h2, h3, h4, h5, h6, a, span, div {
                font-family: 'PingFang', 'PingFang SC', 'PingFang TC', 'PingFang HK', 'PingFang SC Regular', 'PingFang TC Regular', 'PingFang HK Regular', sans-serif !important;
            }
        `);
    }

    // 检查本地是否已有苹方字体
    function checkLocalFont() {
        let testElement = document.createElement('span');
        testElement.style.fontFamily = 'PingFang';
        testElement.innerText = 'Test';
        document.body.appendChild(testElement);
        let fontAvailable = (testElement.clientHeight > 0);
        document.body.removeChild(testElement);
        return fontAvailable;
    }

    // 显示询问对话框
    function showConfirmDialog() {
        if (confirm('苹方字体未检测到,是否下载以强制网页使用苹方字体?')) {
            addPingFangFont();
        } else if (confirm('是否永久取消下载苹方字体?')) {
            GM_setValue(permanentCancelKey, true);
        }
    }

    // 主函数
    function main() {
        const permanentCancel = GM_getValue(permanentCancelKey, false);
        if (permanentCancel) return;

        if (checkLocalFont()) {
            GM_addStyle(`
                body, p, h1, h2, h3, h4, h5, h6, a, span, div {
                    font-family: 'PingFang', 'PingFang SC', 'PingFang TC', 'PingFang HK', 'PingFang SC Regular', 'PingFang TC Regular', 'PingFang HK Regular', sans-serif !important;
                }
            `);
        } else {
            showConfirmDialog();
        }
    }

    // 运行主函数
    main();
})();