Support for RTL languages

This commit is contained in:
AlirezaF80
2023-07-03 15:46:28 +03:30
parent b7eac78d67
commit 8ad53a142c
+9 -6
View File
@@ -2,8 +2,8 @@
// @name ChatGPT RTL // @name ChatGPT RTL
// @namespace http://chat.openai.com // @namespace http://chat.openai.com
// @author Alireza Farzaneh // @author Alireza Farzaneh
// @version 0.1.0 // @version 0.1.1
// @description Changes the direction of Persian text on ChatGPT interface to RTL // @description Fixes the direction of RTL languages in ChatGPT's interface
// @match https://chat.openai.com/* // @match https://chat.openai.com/*
// @grant none // @grant none
// ==/UserScript== // ==/UserScript==
@@ -11,10 +11,13 @@
(function() { (function() {
'use strict'; 'use strict';
const PERSIAN_REGEX = /[\u0600-\u06FF\uFB8A\u067E\u0686\u06AF\u200C\u200F]+/; 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 to check if a given string contains Persian text // Function to check if a given string contains a text in any RTL language
function containsPersianText(text) { function containsRTLText(text) {
return PERSIAN_REGEX.test(text); return RTL_REGEX.test(text);
} }
// Function to modify a single text node // Function to modify a single text node
@@ -27,7 +30,7 @@
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];
if (childNode.nodeType === Node.TEXT_NODE && containsPersianText(childNode.textContent)) { if (childNode.nodeType === Node.TEXT_NODE && containsRTLText(childNode.textContent)) {
changeTextElementDirection(childNode); changeTextElementDirection(childNode);
} else if (childNode.nodeType === Node.ELEMENT_NODE) { } else if (childNode.nodeType === Node.ELEMENT_NODE) {
modifyPersianTextElements(childNode); modifyPersianTextElements(childNode);