Update Chatgpt-RTL.user.js

This commit is contained in:
HUSSEIN
2025-09-25 19:07:30 +03:30
committed by GitHub
parent 75a0fca61f
commit f98ae446d1
+55 -63
View File
@@ -1,91 +1,83 @@
// ==UserScript== // ==UserScript==
// @name ChatGPT RTL // @name ChatGPT RTL
// @namespace http://chat.openai.com // @namespace http://chat.openai.com
// @namespace http://deepseek.com
// @author Alireza Farzaneh // @author Alireza Farzaneh
// @version 0.1.2 // @version 0.1.2.1
// @description Fixes the direction of RTL languages in ChatGPT's interface // @description Fixes the direction of RTL languages in LLM's interface
// @match https://chat.openai.com/* // @match https://chat.openai.com/*
// @match https://chatgpt.com/* // @match https://chatgpt.com/*
// @match https://chat.deepseek.com/*
// @match https://claude.ai/*
// @match https://chat.qwen.ai/*
// @grant none // @grant none
// ==/UserScript== // ==/UserScript==
;(function() { ;(function() {
"use strict"; "use strict";
const RTL_REGEX = /[\u0590-\u05FF\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/;
var PERSIAN_REGEX = /[\u0600-\u06FF]/; const styleId = 'deepseek-rtl-fix-style';
var HEBREW_REGEX = /[\u0590-\u05FF]/; if (!document.getElementById(styleId)) {
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) {
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) {
return element.parentNode.getAttribute('dir') === 'rtl';
}
function modifyPersianTextElements(element) {
if (element === document.body) {
const style = document.createElement('style'); const style = document.createElement('style');
style.id = styleId;
style.textContent = ` style.textContent = `
.markdown-content, .text-message, .deepseek-rtl-applied, .deepseek-rtl-applied * {
.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; direction: rtl !important;
text-align: right !important;
} }
`; `;
document.head.appendChild(style); document.head.appendChild(style);
} }
var childNodes = element.childNodes; function applyRTLToNode(node) {
for (var i = 0; i < childNodes.length; i++) { if (!node) return;
var childNode = childNodes[i]; let elem = null;
if (childNode.nodeType === Node.TEXT_NODE) { if (node.nodeType === Node.TEXT_NODE) {
if (isAlreadyModified(childNode)) { elem = node.parentElement;
continue; } else if (node.nodeType === Node.ELEMENT_NODE) {
} elem = node;
if (containsRTLText(childNode.textContent)) {
changeTextElementDirection(childNode);
}
} else if (childNode.nodeType === Node.ELEMENT_NODE) {
modifyPersianTextElements(childNode);
} }
if (!elem) return;
if (elem.closest('.deepseek-rtl-applied')) return;
const txt = elem.textContent || "";
if (RTL_REGEX.test(txt)) {
elem.classList.add('deepseek-rtl-applied');
} }
} }
function handleMutations(mutationsList) { function initialScan(root) {
for (let i = 0; i < mutationsList.length; i++) { if (!root) root = document.body;
const mutation = mutationsList[i]; const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null, false);
modifyPersianTextElements(mutation.target.parentNode); let cur;
while ((cur = walker.nextNode())) {
applyRTLToNode(cur);
}
} }
};
modifyPersianTextElements(document.body); initialScan(document.body);
const observer = new MutationObserver((mutations) => {
for (const mut of mutations) {
if (mut.type === 'childList') {
for (const nd of mut.addedNodes) {
applyRTLToNode(nd);
if (nd.nodeType === Node.ELEMENT_NODE) {
initialScan(nd);
}
}
} else if (mut.type === 'characterData') {
applyRTLToNode(mut.target);
}
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
characterData: true
});
var observer = new MutationObserver(handleMutations);
observer.observe(document.body, { childList: true, subtree: true, attributes: true, characterData: true });
})(); })();