YouTube Music RTL Formatter

Automatically apply RTL direction, Vazirmatn font, proper margins, and customizable font size to Persian/Arabic text on music.youtube.com

  1. // ==UserScript==
  2. // @name YouTube Music RTL Formatter
  3. // @version 1.0
  4. // @description Automatically apply RTL direction, Vazirmatn font, proper margins, and customizable font size to Persian/Arabic text on music.youtube.com
  5. // @author Zen
  6. // @match https://music.youtube.com/*
  7. // @grant GM_addStyle
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_registerMenuCommand
  11. // @license MIT
  12. // @supportURL https://github.com/Zen-CloudLabs/UserScripts/issues
  13. // @homepageURL https://github.com/Zen-CloudLabs/UserScripts
  14. // @icon https://music.youtube.com/img/favicon_144.png
  15.  
  16. // @namespace https://gf.qytechs.cn/users/1425911
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. const fontLink = document.createElement('link');
  23. fontLink.href = 'https://fonts.googleapis.com/css2?family=Vazirmatn&display=swap';
  24. fontLink.rel = 'stylesheet';
  25. document.head.appendChild(fontLink);
  26.  
  27. const defaultFontSize = 14;
  28. let fontSize = GM_getValue('fontSize', defaultFontSize);
  29.  
  30. function updateFontSize(newSize) {
  31. fontSize = newSize;
  32. GM_setValue('fontSize', fontSize);
  33. document.documentElement.style.setProperty('--rtl-font-size', `${fontSize}px`);
  34. }
  35.  
  36. GM_addStyle(`
  37. :root {
  38. --rtl-font-size: ${fontSize}px;
  39. }
  40. .vazirmatn-rtl {
  41. font-family: 'Vazirmatn', sans-serif !important;
  42. direction: rtl !important;
  43. text-align: right !important;
  44. padding: 10px 15px !important;
  45. margin: 10px 0 !important;
  46. box-sizing: border-box !important;
  47. font-size: var(--rtl-font-size) !important;
  48. }
  49. `);
  50.  
  51. function isPersianOrArabic(text) {
  52. const persianArabicRegex = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/g;
  53. const matches = text.match(persianArabicRegex) || [];
  54. return matches.length / text.length > 0.3;
  55. }
  56.  
  57. function updateRTLAndFont(div) {
  58. if (isPersianOrArabic(div.textContent)) {
  59. div.classList.add('vazirmatn-rtl');
  60. div.style.fontSize = `var(--rtl-font-size)`;
  61. } else {
  62. div.classList.remove('vazirmatn-rtl');
  63. }
  64. }
  65.  
  66. const observer = new MutationObserver((mutations) => {
  67. mutations.forEach((mutation) => {
  68. mutation.addedNodes.forEach((node) => {
  69. if (node.nodeType === 1 && node.matches('yt-formatted-string.non-expandable.description.style-scope.ytmusic-description-shelf-renderer')) {
  70. updateRTLAndFont(node);
  71. }
  72. });
  73.  
  74. if (mutation.type === 'characterData' || mutation.type === 'childList') {
  75. const targetDiv = mutation.target;
  76. if (targetDiv.nodeType === 1 && targetDiv.matches('yt-formatted-string.non-expandable.description.style-scope.ytmusic-description-shelf-renderer')) {
  77. updateRTLAndFont(targetDiv);
  78. }
  79. }
  80. });
  81. });
  82.  
  83. observer.observe(document.body, { childList: true, subtree: true, characterData: true });
  84.  
  85. document.querySelectorAll('yt-formatted-string.non-expandable.description.style-scope.ytmusic-description-shelf-renderer').forEach(updateRTLAndFont);
  86.  
  87. GM_registerMenuCommand('Change Font Size', () => {
  88. const newFontSize = prompt('Enter the desired font size (e.g., 14, 16):', fontSize);
  89. if (newFontSize && !isNaN(newFontSize)) {
  90. updateFontSize(Number(newFontSize));
  91. } else {
  92. alert('Please enter a valid number.');
  93. }
  94. });
  95. })();

QingJ © 2025

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