// ==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 // @downloadURL none // ==/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(); })();