Grok Return from Uploaded Files View via ESC

When on grok.com/files, ESC takes user back

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         Grok Return from Uploaded Files View via ESC
// @namespace    nisc
// @version      2025.06.08-A
// @description  When on grok.com/files, ESC takes user back
// @homepageURL  https://github.com/nisc/grok-userscripts/
// @author       nisc
// @match        https://grok.com/files
// @icon         https://grok.com/images/favicon-light.png
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    /**
     * Configuration object containing all constants used in the script
     * KEYS: Keyboard key mappings for navigation
     * NAVIGATION: Default URL to return to when no history exists
     */
    const CONFIG = {
        KEYS: {
            ESCAPE: 'Escape'
        },
        NAVIGATION: {
            DEFAULT_URL: 'https://grok.com'
        }
    };

    /**
     * Handles the ESC key press event
     *
     * Behavior:
     * 1. When ESC is pressed, prevents default browser behavior
     * 2. Checks browser history length:
     *    - If history exists (length >= 2), goes back one page
     *    - If no history (length < 2), redirects to grok.com
     *
     * Note: History length < 2 means we can't go back (current page is the only one)
     */
    function handleEscapeKey(event) {
        if (event.key === CONFIG.KEYS.ESCAPE) {
            event.preventDefault();  // Stop browser's default ESC behavior

            if (window.history.length < 2) {
                // No previous page exists, go to home page
                window.open(CONFIG.NAVIGATION.DEFAULT_URL, '_self');
            } else {
                // Previous page exists, go back to it
                window.history.back();
            }
        }
    }

    // Listen for ESC key press anywhere on the page
    document.addEventListener('keydown', handleEscapeKey);
})();