X/Twitter Image Easy Close

X/Twitter Image Easy Close functionality

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         X/Twitter Image Easy Close
// @namespace    https://github.com/BD9Max
// @version      1.0
// @description  X/Twitter Image Easy Close functionality
// @author       krbd9max
// @match        https://twitter.com/*
// @match        https://x.com/*
// @license      MIT
// @grant        none
// @icon         https://upload.wikimedia.org/wikipedia/commons/c/ce/X_logo_2023.svg
// ==/UserScript==

(function() {
    'use strict';

    // Function to trigger the native Twitter close action
    const closeImageOverlay = () => {
        const closeButton = document.querySelector('[data-testid="app-bar-close"]');
        if (closeButton) {
            closeButton.click();
        } else {
            // Fallback: If the button isn't found, use the browser back action to exit the photo URL
            window.history.back();
        }
    };

    document.addEventListener('click', (e) => {
        // Only run if we are currently looking at a photo/media overlay
        if (window.location.pathname.includes('/photo/')) {
            
            // Check if the click is on an interactive UI element (Like, Retweet, Reply buttons)
            // We exclude these so you can still interact with the post without it closing.
            const isControl = e.target.closest('[role="button"]') || 
                              e.target.closest('[role="menuitem"]') ||
                              e.target.closest('[data-testid="KeybindLayer"]');

            if (!isControl) {
                // Prevent the default Twitter click behavior and close the overlay
                e.stopPropagation();
                closeImageOverlay();
            }
        }
    }, true); // Use capture phase to intercept the click immediately
})();