Greasy Fork is available in English.
替换网页的字体
当前为
// ==UserScript==
// @name 替换网页字体
// @author ai
// @version 2.4
// @match *://*/*
// @run-at document-start
// @grant GM_addStyle
// @grant GM_getValues
// @grant GM_getValue
// @grant GM_setValues
// @grant GM_setValue
// @grant GM_listValues
// @grant GM_addValueChangeListener
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @description 替换网页的字体
// @namespace http://greasyfork.icu/users/22620
// ==/UserScript==
(function() {
'use strict';
const DEFAULT_CONFIG = {
font: '微软雅黑',
exclusions: '',
enabled: true
};
class FontManager {
constructor() {
if(!GM_listValues().length) GM_setValues(DEFAULT_CONFIG);
GM_listValues().forEach(key => {
GM_addValueChangeListener(key, () => {
this.config = this.getConfig();
if (key === "enabled") this.updateMenu();
this.applyFont();
})
});
this.config = this.getConfig();
this.initMenu();
this.styleElement = GM_addStyle(this.generateCSS(this.config.font));
this.getWebFonts();
}
getConfig() {
return GM_getValues(DEFAULT_CONFIG)
}
saveConfig(key, value) {
if (GM_getValue(key) === value) return;
GM_setValue(key, value);
}
async getWebFonts() {
await document.fonts.ready;
const fonts = new Set();
document.fonts.forEach(font => fonts.add(`"${font.family}"`));
this.WebFonts = [...fonts].join(', ');
this.applyFont();
}
generateCSS(font) {
let exclusions = 'i, [class*="icon"], [class*="emoji"], [class*="fa-"], [class*="symbol"]';
if (this.config.exclusions) exclusions = `${exclusions},${this.config.exclusions}`
const notSelector = exclusions.split(',')
.map(s => s.trim())
.filter(Boolean)
.map(s => `:not(${s})`)
.join('');
return `*${notSelector} { font-family: ${font} !important; }`;
}
applyFont() {
if (this.config.enabled) {
const fonts = this.WebFonts ? `${this.config.font}, ${this.WebFonts}` : this.config.font;
this.styleElement.textContent = this.generateCSS(fonts);
} else {
this.styleElement.textContent = '';
}
}
initMenu() {
this.menuCommands = new Map();
this.updateMenu();
}
updateMenu() {
if (!window.top === window) return
this.menuCommands.forEach(id => GM_unregisterMenuCommand(id));
this.menuCommands.clear();
this.menuCommands.set('enabled', GM_registerMenuCommand(
this.config.enabled ? '❌ 禁用字体替换' : '✅ 启用字体替换',
() => this.saveConfig("enabled", !this.config.enabled)
));
this.menuCommands.set('font', GM_registerMenuCommand('🖋️ 配置字体名称',
() => this.promptConfig('font', '请输入新的字体名称')
));
this.menuCommands.set('exclusions', GM_registerMenuCommand('🚫 配置排除元素列表',
() => this.promptConfig('exclusions', '请输入新的排除元素选择器')
));
this.menuCommands.set('reset', GM_registerMenuCommand('🗑️ 恢复默认设置',
() => this.resetToDefault()
));
}
promptConfig(type, promptText) {
const current = this.config[type];
const newValue = prompt(`${promptText}:\n(用逗号分隔,例如: ${type === 'font' ? '"微软雅黑", system-ui' : 'i, [class*="ico"]'})`, current);
if (newValue) {
this.saveConfig(type, newValue);
}
}
resetToDefault() {
if (confirm('确定要将所有配置恢复为默认值吗?')) GM_setValues(DEFAULT_CONFIG);
}
}
new FontManager();
})();