GitHub shorter title edited files

Change title edited file in editor to filename.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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
})();