正確的Twitter貼文圖片替代文字

將Twitter貼文的圖片替代文字從單純的「圖片」改為帶有使用者名稱、帳號、網域的替代文字,以增加辨識度。

目前为 2025-01-22 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 正確的Twitter貼文圖片替代文字
  3. // @name:en Correct Twitter Post Image Alt Text
  4. // @name:ja 正確なTwitter投稿畫像代替テキスト
  5. // @name:es Texto Alternativo Correcto para Imágenes en Tweets de Twitter
  6. // @name:fr Texte Alternatif Correct pour les Images des Tweets sur Twitter
  7. // @name:de Korrekte Alternativtexte für Bilder in Twitter-Beiträgen
  8. // @name:it Testo Alternativo Corretto per Immagini nei Tweet di Twitter
  9. // @name:ko Twitter 게시물의 정확한 이미지 대체 텍스트
  10.  
  11. // @namespace https://github.com/Max46656
  12. // @version 1.0.1
  13. // @author Max
  14. // @match https://twitter.com/*
  15. // @match https://x.com/*
  16. // @match https://mobile.twitter.com/*
  17. // @icon https://www.google.com/s2/favicons?sz=64&domain=x.com
  18. // @license MPL2.0
  19.  
  20. // @description 將Twitter貼文的圖片替代文字從單純的「圖片」改為帶有使用者名稱、帳號、網域的替代文字,以增加辨識度。
  21. // @description:en Updates alt text for Twitter images to include username, account, and domain for better recognition.
  22. // @description:ja Twitterの投稿の畫像のaltテキストを、ユーザー名、アカウント、ドメインを含むように変更し、識別性を向上させます。
  23. // @description:es Actualiza el texto alternativo de las imágenes de Twitter para incluir nombre de usuario, cuenta y dominio para una mejor identificación.
  24. // @description:fr Met à jour le texte alternatif des images sur Twitter pour inclure le nom d'utilisateur, le compte et le domaine pour une meilleure reconnaissance.
  25. // @description:de Aktualisiert den alternativen Text für Twitter-Bilder, um Benutzername, Konto und Domain für eine bessere Erkennung einzuschließen.
  26. // @description:it Aggiorna il testo alternativo delle immagini su Twitter per includere nome utente, account e dominio per una migliore identificazione.
  27. // @description:ko Twitter 게시물의 이미지 대체 텍스트를 사용자 이름, 계정 및 도메인을 포함하여 업데이트하여 인식률을 높입니다.
  28. // ==/UserScript==
  29.  
  30. class AltTextUpdater {
  31. constructor() {
  32. this.selectors = {
  33. tweetWithImg: "article:has(img)",
  34. photoWithTweet: "div[aria-labelledby='modal-header']",
  35. textContainer: "div.css-175oi2r.r-1awozwy.r-18u37iz.r-1wbh5a2.r-dnmrzs span.css-1jxf684.r-bcqeeo.r-1ttztb7.r-qvutc0.r-poiln3"
  36. };
  37.  
  38. this.langPatterns = {
  39. 'zh-Hant': { imgAlt: '圖片', connector: '的', domain: '來自推特'},
  40. 'en': { imgAlt: 'Image', connector: '\'s ', domain: ' form Twitter' },
  41. 'ja': { imgAlt: '畫像', connector: 'の', domain: 'Twitterから'},
  42. 'es': { imgAlt: 'Imagen', connector: ' de ', domain: ' de Twitter' },
  43. 'fr': { imgAlt: 'Image', connector: ' de ', domain: ' de Twitter' },
  44. 'de': { imgAlt: 'Bild', connector: ' von ', domain: ' von Twitter' },
  45. 'it': { imgAlt: 'Immagine', connector: ' di ', domain: ' da Twitter' },
  46. 'ko': { imgAlt: '이미지', connector: '의 ', domain: 'Twitter에서' }
  47. };
  48.  
  49. this.checkNowAndUpcomingTweets();
  50. }
  51.  
  52. checkNowAndUpcomingTweets() {
  53. this.updateTweetsWithImages();
  54. this.observeSet();
  55. }
  56.  
  57. observeSet() {
  58. let observer = new MutationObserver(this.handleMutations.bind(this));
  59. observer.observe(document, { childList: true, subtree: true });
  60. }
  61.  
  62. handleMutations(mutations) {
  63. mutations.forEach((mutation) => {
  64. if (mutation.addedNodes.length > 0) {
  65. this.updateTweetsWithImages();
  66. }
  67. });
  68. }
  69.  
  70. updateTweetsWithImages() {
  71. const tweetImgContainers = document.querySelectorAll(
  72. `${this.selectors.tweetWithImg}, ${this.selectors.photoWithTweet}`
  73. );
  74. tweetsWithImg.forEach((tweet) => {
  75. this.updateAltText(tweet);
  76. });
  77. }
  78.  
  79. updateAltText(tweet) {
  80. const lang = this.detectLanguage();
  81. const { imgAlt, connector,domain} = this.langPatterns[lang];
  82. const img = tweet.querySelector(`img[alt="${imgAlt}"]`);
  83. //console.log(img);
  84.  
  85. if (img) {
  86. const targetSpans = tweet.querySelectorAll(this.selectors.textContainer);
  87. const spansWithText = Array.from(targetSpans).filter(span => span.textContent);
  88. const userName = spansWithText[0];
  89. const account = spansWithText[2];
  90. img.setAttribute("alt", `${userName.textContent}(${account.textContent})${connector}${imgAlt}${domain}`);
  91. }
  92. }
  93. detectLanguage() {
  94. const htmlLang = document.documentElement.lang;
  95. return this.langPatterns[htmlLang] ? htmlLang : 'en';
  96. }
  97. }
  98. new AltTextUpdater();

QingJ © 2025

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