Dynamic RTL/LTR Direction

Dynamically set text direction for Deepseek chat elements based on content

目前为 2025-01-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Dynamic RTL/LTR Direction
  3. // @namespace http://tampermonkey.net/
  4. // @version V3
  5. // @description Dynamically set text direction for Deepseek chat elements based on content
  6. // @author Reda Elsayed
  7. // @match https://chat.deepseek.com/
  8. // @match https://chat.deepseek.com/*
  9. // @match https://chat.deepseek.com/*/chat/*/*
  10. // @icon https://www.deepseek.com/path/to/icon.png
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16. const textareaClass = "c92459f0";
  17. const targetClass = "ds-markdown ds-markdown--block";
  18. const submitButtonClass = "f6d670";
  19. function isRTL(text) {
  20. const rtlPattern = /[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]/;
  21. return rtlPattern.test(text);
  22. }
  23. function updateDirection() {
  24. const targetElements = document.getElementsByClassName(targetClass);
  25. if (targetElements.length > 0) {
  26. const latestElement = targetElements[targetElements.length - 1];
  27. const textContent = latestElement.textContent.trim();
  28. const currentDirection = latestElement.style.direction;
  29. const newDirection = isRTL(textContent) ? "rtl" : "ltr";
  30. if (textContent.length > 0 && currentDirection !== newDirection) {
  31. latestElement.style.direction = newDirection;
  32. console.log(`Direction updated to: ${newDirection}`);
  33. }
  34. }
  35. }
  36. function startObserving() {
  37. const observer = new MutationObserver(() => {
  38. updateDirection();
  39. });
  40. observer.observe(document.body, { childList: true, subtree: true });
  41. console.log("MutationObserver started.");
  42. }
  43. function handleSubmitButtonClick() {
  44. const submitButton = document.getElementsByClassName(submitButtonClass)[0];
  45. if (submitButton) {
  46. submitButton.addEventListener("click", () => {
  47. setTimeout(updateDirection, 500);
  48. });
  49. } else {
  50. console.error("Submit button not found!");
  51. }
  52. }
  53. updateDirection();
  54. startObserving();
  55. handleSubmitButtonClick();
  56. })();