mirror of
https://github.com/HUSSEIN-Syu/LLMs-RTL.git
synced 2026-11-03 12:19:41 +00:00
Support all mutation types
This commit is contained in:
+15
-20
@@ -2,7 +2,7 @@
|
|||||||
// @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.1
|
// @version 0.1.2
|
||||||
// @description Fixes the direction of RTL languages in ChatGPT's interface
|
// @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
|
||||||
@@ -15,49 +15,44 @@
|
|||||||
const ARABIC_REGEX = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDCF\uFDF0-\uFDFF\uFE70-\uFEFF]+/;
|
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);
|
const RTL_REGEX = new RegExp(PERSIAN_REGEX.source + '|' + HEBREW_REGEX.source + '|' + ARABIC_REGEX.source);
|
||||||
|
|
||||||
// Function to check if a given string contains a text in any RTL language
|
|
||||||
function containsRTLText(text) {
|
function containsRTLText(text) {
|
||||||
return RTL_REGEX.test(text);
|
return RTL_REGEX.test(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to modify a single text node
|
|
||||||
function changeTextElementDirection(element) {
|
function changeTextElementDirection(element) {
|
||||||
element.parentNode.setAttribute('dir', 'rtl');
|
element.parentNode.setAttribute('dir', 'rtl');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to recursively search and modify elements containing Persian text
|
function isAlreadyModified(element) {
|
||||||
|
return element.parentNode.getAttribute('dir') === 'rtl';
|
||||||
|
}
|
||||||
|
|
||||||
function modifyPersianTextElements(element) {
|
function modifyPersianTextElements(element) {
|
||||||
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 && containsRTLText(childNode.textContent)) {
|
if (childNode.nodeType === Node.TEXT_NODE) {
|
||||||
|
if (isAlreadyModified(childNode)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to handle mutation events
|
|
||||||
function handleMutations(mutationsList) {
|
function handleMutations(mutationsList) {
|
||||||
for (var i = 0; i < mutationsList.length; i++) {
|
for (let i = 0; i < mutationsList.length; i++) {
|
||||||
var mutation = mutationsList[i];
|
const mutation = mutationsList[i];
|
||||||
if (mutation.type === 'childList') {
|
modifyPersianTextElements(mutation.target.parentNode);
|
||||||
var addedNodes = mutation.addedNodes;
|
|
||||||
for (var j = 0; j < addedNodes.length; j++) {
|
|
||||||
var addedNode = addedNodes[j];
|
|
||||||
if (addedNode.nodeType === Node.ELEMENT_NODE) {
|
|
||||||
modifyPersianTextElements(addedNode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modify elements containing Persian text
|
|
||||||
modifyPersianTextElements(document.body);
|
modifyPersianTextElements(document.body);
|
||||||
|
|
||||||
// Watch for DOM mutations and apply RTL direction to newly added Persian text
|
|
||||||
var observer = new MutationObserver(handleMutations);
|
var observer = new MutationObserver(handleMutations);
|
||||||
observer.observe(document.body, { childList: true, subtree: true });
|
observer.observe(document.body, { childList: true, subtree: true, attributes: true, characterData: true });
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user