YT Music Persian Lyrics RTL Fixer

This user script automatically detects Persian lyrics on YouTube Music and corrects their alignment by setting the text direction to right-to-left (RTL). It ensures Persian lines are properly displayed with RTL alignment and right text alignment, while keeping non-Persian lyrics in the default left-to-right (LTR) format. The script continuously monitors the page for dynamic lyric changes and applies the appropriate styling in real time.

  1. // ==UserScript==
  2. // @name YT Music Persian Lyrics RTL Fixer
  3. // @namespace Violentmonkey Scripts
  4. // @match https://music.youtube.com/*
  5. // @version 1.0.1
  6. // @description This user script automatically detects Persian lyrics on YouTube Music and corrects their alignment by setting the text direction to right-to-left (RTL). It ensures Persian lines are properly displayed with RTL alignment and right text alignment, while keeping non-Persian lyrics in the default left-to-right (LTR) format. The script continuously monitors the page for dynamic lyric changes and applies the appropriate styling in real time.
  7. // @author Sina Zadeh
  8.  
  9. // @license MIT
  10. // ==/UserScript==
  11. function isPersian(text) {
  12. return /[\u0600-\u06FF]/.test(text); // Persian/Arabic character range
  13. }
  14.  
  15. function updateLyricsDirection() {
  16. const lines = document.querySelectorAll('.blyrics-container > div');
  17. lines.forEach(line => {
  18. const text = line.textContent || '';
  19. if (isPersian(text)) {
  20. line.style.direction = 'rtl';
  21. line.style.textAlign = 'right';
  22. } else {
  23. line.style.direction = 'ltr';
  24. line.style.textAlign = 'left';
  25. }
  26. });
  27. }
  28.  
  29. // Run initially and on lyrics change
  30. const observer = new MutationObserver(updateLyricsDirection);
  31. observer.observe(document.body, { childList: true, subtree: true });
  32.  
  33. // Initial run
  34. updateLyricsDirection();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址