GitHub shorter title edited files

Change title edited file in editor to filename.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name            GitHub shorter title edited files
// @description     Change title edited file in editor to filename.
// @author          hawkeye116477, krystian3w
// @namespace       https://greasyfork.org/users/167625
// @icon            https://i.imgur.com/KmWlHbe.png
// @match           https://github.com/*/*/edit/*/*
// @version         1.0.8
// @grant           none
// @run-at          document-idle
// @compatible      firefox Firefox
// @compatible      chrome Chrome
// @compatible      edge Edge
// ==/UserScript==

"use strict";

(function () {
    let lastUrl = location.href;

    // Function to run on pages with "edit" in the URL
    function runOnEditPage() {
        if (!location.href.includes("/edit/")) return;

        const updateTitle = () => {
            const el = document.querySelector('[aria-label="File name"]');
            if (el && el.value) {
                document.title = el.value;
                return true;
            }
            return false;
        };

        if (!updateTitle()) {
            const observer = new MutationObserver(() => {
                if (updateTitle()) observer.disconnect();
            });
            observer.observe(document.body, { childList: true, subtree: true });
        }
    }

    // Observe SPA URL changes
    const observeUrl = () => {
        const bodyObserver = new MutationObserver(() => {
            if (location.href !== lastUrl) {
                lastUrl = location.href;
                runOnEditPage();
            }
        });
        bodyObserver.observe(document.body, { childList: true, subtree: true });
    };

    // Hook into history API (for pushState/replaceState)
    const interceptHistory = () => {
        const origPush = history.pushState;
        const origReplace = history.replaceState;

        const handler = function () {
            const url = location.href;
            if (url !== lastUrl) {
                lastUrl = url;
                runOnEditPage();
            }
        };

        history.pushState = function () {
            origPush.apply(this, arguments);
            handler();
        };

        history.replaceState = function () {
            origReplace.apply(this, arguments);
            handler();
        };

        window.addEventListener("popstate", handler);
    };

    observeUrl();
    interceptHistory();
    runOnEditPage(); // Initial load
})();