Mastodon img Zoom Fix and Auto-Close

Replace zoomable image with clone and close modal on click

// ==UserScript==
// @name         Mastodon img Zoom Fix and Auto-Close
// @description  Replace zoomable image with clone and close modal on click
// @match        https://mastodon.social/*
// @run-at       document-idle
// @version 0.0.1.20250524080115
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==

const observer = new MutationObserver(() => {
  const oldElement = document.querySelector('.zoomable-image img');

  if (oldElement && !oldElement.dataset.replaced) {
    const newElement = oldElement.cloneNode(true); // deep clone
    newElement.dataset.replaced = 'true'; // mark the new one
    oldElement.parentNode.replaceChild(newElement, oldElement);
  }
});

observer.observe(document.body, {
  childList: true,
  subtree: true
});

document.querySelector('.zoomable-image img')
  .addEventListener('click', () => {
    document.querySelector('.media-modal__buttons .icon-button[title="Close"]').click();
  });