Apply Vazir font to Persian/RTL content across selected websites
当前为
// ==UserScript==
// @name Persian Font Fix (Vazir)
// @namespace https://greasyfork.org/en/scripts/538095-persian-font-fix-vazir
// @version 1.5
// @description Apply Vazir font to Persian/RTL content across selected websites
// @author TheSina
// @match *://*.telegram.org/*
// @match *://*.x.com/*
// @match *://*.twitter.com/*
// @match *://*.instagram.com/*
// @match *://*.facebook.com/*
// @match *://*.whatsapp.com/*
// @match *://*.github.com/*
// @match *://*.youtube.com/*
// @match *://*.soundcloud.com/*
// @match *://www.google.com/*
// @match *://gemini.google.com/*
// @match *://translate.google.com/*
// @match *://*.chatgpt.com/*
// @match *://*.openai.com/*
// @match *://fa.wikipedia.org/*
// @match *://app.slack.com/*
// @match *://*.goodreads.com/*
// @grant GM_addStyle
// @run-at document-start
// @license MIT
// ==/UserScript==
(function () {
'use strict';
// --- Style Injection ---
GM_addStyle(`
@font-face {
font-family: 'VazirmatnCustom';
src: local('Vazirmatn');
unicode-range: U+0600-06FF, U+0750-077F, U+08A0-08FF, U+FB50-FDFF, U+FE70-FEFF;
}
html, body, p, span, div, h1, h2, h3, h4, h5, h6, a, li, ul, ol, td, th, input, textarea {
font-family: 'VazirmatnCustom', 'Noto Sans', 'Noto Color Emoji', sans-serif !important;
}
`);
// --- Efficient Character Replacement ---
const characterReplacements = {
'\u064A': '\u06CC', // Arabic Yeh -> Persian Yeh
'\u0643': '\u06A9' // Arabic Kaf -> Persian Kaf
};
const fixText = (text) => text.replace(/[\u064A\u0643]/g, char => characterReplacements[char]);
// --- High-Performance DOM Traversal ---
const fixPersianCharsInNode = (rootNode) => {
const treeWalker = document.createTreeWalker(rootNode, NodeFilter.SHOW_TEXT);
let currentNode;
while ((currentNode = treeWalker.nextNode())) {
const parentTag = currentNode.parentElement?.tagName;
if (parentTag === 'SCRIPT' || parentTag === 'STYLE') continue;
const originalValue = currentNode.nodeValue;
const fixedValue = fixText(originalValue);
if (originalValue !== fixedValue) {
currentNode.nodeValue = fixedValue;
}
}
};
// --- Input and Textarea Handling ---
const processInputElement = (el) => {
if (el.dataset.__persianFixAttached) return;
el.dataset.__persianFixAttached = "true";
if (el.value) {
el.value = fixText(el.value);
}
el.addEventListener('input', () => {
const originalValue = el.value;
const fixedValue = fixText(originalValue);
if (originalValue !== fixedValue) {
const start = el.selectionStart;
const end = el.selectionEnd;
el.value = fixedValue;
el.setSelectionRange(start, end);
}
});
};
const processAllInputs = (root = document) => {
const elements = root.querySelectorAll('input[type="text"], textarea');
for (const el of elements) {
processInputElement(el);
}
};
// --- Mutation Observer for Dynamic Content ---
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE || node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
fixPersianCharsInNode(node);
processAllInputs(node);
}
}
}
});
// --- Script Initialization ---
const start = () => {
fixPersianCharsInNode(document.body);
processAllInputs(document);
observer.observe(document.body, {
childList: true,
subtree: true
});
};
const waitForBody = () => {
if (document.body) {
start();
} else {
requestAnimationFrame(waitForBody);
}
};
waitForBody();
})();