Refactor RTL text handling and improve alignment for Persian, Hebrew, and Arabic content

This commit is contained in:
Milad Raeisi
2025-01-09 08:49:55 +04:00
parent 8751a416dc
commit 8be6a63187
+40 -8
View File
@@ -9,19 +9,35 @@
// @grant none // @grant none
// ==/UserScript== // ==/UserScript==
(function () { ;(function() {
'use strict'; "use strict";
const PERSIAN_REGEX = /[\u0600-\u06FF\uFB8A\u067E\u0686\u06AF\u200C\u200F]+/;
const HEBREW_REGEX = /[\u0590-\u05FF\uFB1D-\uFB4F]+/; var PERSIAN_REGEX = /[\u0600-\u06FF]/;
const ARABIC_REGEX = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDCF\uFDF0-\uFDFF\uFE70-\uFEFF]+/; var HEBREW_REGEX = /[\u0590-\u05FF]/;
const RTL_REGEX = new RegExp(PERSIAN_REGEX.source + '|' + HEBREW_REGEX.source + '|' + ARABIC_REGEX.source); var ARABIC_REGEX = /[\u0600-\u06FF]/;
var RTL_REGEX = new RegExp([PERSIAN_REGEX.source, HEBREW_REGEX.source, ARABIC_REGEX.source].join("|"));
function containsRTLText(text) { function containsRTLText(text) {
return RTL_REGEX.test(text); return RTL_REGEX.test(text);
} }
function changeTextElementDirection(element) { function changeTextElementDirection(element) {
element.parentNode.setAttribute('dir', 'rtl'); if (element.parentNode) {
const parentElement = element.parentNode;
parentElement.setAttribute('dir', 'rtl');
parentElement.style.cssText += 'text-align: right !important; direction: rtl !important;';
// Apply to parent containers to ensure proper alignment
let currentElement = parentElement;
while (currentElement && currentElement !== document.body) {
if (currentElement.classList.contains('markdown-content') ||
currentElement.classList.contains('text-message') ||
/^H[1-6]$/.test(currentElement.tagName)) {
currentElement.style.cssText += 'text-align: right !important; direction: rtl !important;';
}
currentElement = currentElement.parentElement;
}
}
} }
function isAlreadyModified(element) { function isAlreadyModified(element) {
@@ -29,6 +45,22 @@
} }
function modifyPersianTextElements(element) { function modifyPersianTextElements(element) {
if (element === document.body) {
const style = document.createElement('style');
style.textContent = `
.markdown-content, .text-message,
.markdown-content h1, .markdown-content h2,
.markdown-content h3, .markdown-content h4,
.markdown-content h5, .markdown-content h6,
.markdown-content p, .markdown-content div,
[dir="rtl"] {
text-align: right !important;
direction: rtl !important;
}
`;
document.head.appendChild(style);
}
var childNodes = element.childNodes; var childNodes = element.childNodes;
for (var i = 0; i < childNodes.length; i++) { for (var i = 0; i < childNodes.length; i++) {
var childNode = childNodes[i]; var childNode = childNodes[i];
@@ -50,7 +82,7 @@
const mutation = mutationsList[i]; const mutation = mutationsList[i];
modifyPersianTextElements(mutation.target.parentNode); modifyPersianTextElements(mutation.target.parentNode);
} }
} };
modifyPersianTextElements(document.body); modifyPersianTextElements(document.body);