Created
July 22, 2025 22:18
-
-
Save gsuberland/928847fd65b2317c5ec132ffc9dde8b9 to your computer and use it in GitHub Desktop.
Greasemonkey / Tampermonkey script to bring back hoverable alt text in Mastodon 4.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Mastodon 4.4 - Reinstate title text! | |
// @namespace http://tampermonkey.net/ | |
// @version 2025-07-22 | |
// @description Restores alt text in the title attribute of images and videos, allowing the alt text to be viewed in a tooltip while hovering with the mouse. See https://github.com/mastodon/mastodon/issues/33799 | |
// @author Graham Sutherland | |
// @match https://chaos.social/* | |
// @icon https://mastodon.social/favicon.ico | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
new MutationObserver(mutationList => { | |
mutationList.forEach(mutation => { | |
// watch for new 'img' and 'video' elements | |
Array.from(mutation.addedNodes) | |
.filter(el => el.nodeName != '#text') | |
.flatMap(el => [ | |
(el.tagName == 'IMG' || el.tagName == 'VIDEO') ? [ el ] : [], | |
Array.from(el.getElementsByTagName('IMG')), | |
Array.from(el.getElementsByTagName('VIDEO')) | |
]) | |
.flat() | |
.forEach(el => | |
{ | |
// if this media element has alt text, but no title text (missing attribute or empty string), clone alt text into the title attribute | |
if (el.hasAttribute("alt")) | |
{ | |
if (!el.hasAttribute("title") || el.getAttribute("title") == "") | |
{ | |
el.setAttribute("title", el.getAttribute("alt")); | |
} | |
} | |
}); | |
}); | |
}).observe(document.body, { childList: true, subtree: true }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment