GitHub shorter title edited files

Change title edited file in editor to filename.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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
})();