From 8be6a63187efe4a339e3f860fb3c94fe94c5cb82 Mon Sep 17 00:00:00 2001 From: Milad Raeisi Date: Thu, 9 Jan 2025 08:49:55 +0400 Subject: [PATCH] Refactor RTL text handling and improve alignment for Persian, Hebrew, and Arabic content --- Chatgpt-RTL.user.js | 48 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/Chatgpt-RTL.user.js b/Chatgpt-RTL.user.js index b14a66d..cce07c9 100644 --- a/Chatgpt-RTL.user.js +++ b/Chatgpt-RTL.user.js @@ -9,19 +9,35 @@ // @grant none // ==/UserScript== -(function () { - 'use strict'; - const PERSIAN_REGEX = /[\u0600-\u06FF\uFB8A\u067E\u0686\u06AF\u200C\u200F]+/; - const HEBREW_REGEX = /[\u0590-\u05FF\uFB1D-\uFB4F]+/; - const ARABIC_REGEX = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDCF\uFDF0-\uFDFF\uFE70-\uFEFF]+/; - const RTL_REGEX = new RegExp(PERSIAN_REGEX.source + '|' + HEBREW_REGEX.source + '|' + ARABIC_REGEX.source); +;(function() { + "use strict"; + + var PERSIAN_REGEX = /[\u0600-\u06FF]/; + var HEBREW_REGEX = /[\u0590-\u05FF]/; + var ARABIC_REGEX = /[\u0600-\u06FF]/; + var RTL_REGEX = new RegExp([PERSIAN_REGEX.source, HEBREW_REGEX.source, ARABIC_REGEX.source].join("|")); function containsRTLText(text) { return RTL_REGEX.test(text); } 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) { @@ -29,6 +45,22 @@ } 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; for (var i = 0; i < childNodes.length; i++) { var childNode = childNodes[i]; @@ -50,7 +82,7 @@ const mutation = mutationsList[i]; modifyPersianTextElements(mutation.target.parentNode); } - } + }; modifyPersianTextElements(document.body);