KML Title Updater

Update page title for KML pages

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         KML Title Updater
// @namespace    https://greasyfork.org/
// @version      1.0
// @description  Update page title for KML pages
// @author       Ethkuil
// @match        https://kml.corp.kuaishou.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function updatePageTitle() {
        const currentUrl = window.location.href;

        // 匹配#/后的第一段
        const match = currentUrl.match(/#\/([^/]+)/);

        if (!match) return;

        const firstSegment = match[1];
        if (firstSegment === 'personal' || firstSegment === 'project') {
            // 提取页面主题信息
            const pageContentElement = document.evaluate(
                '//*[@id="__kml_page_content__"]/div[1]/div/div',
                document,
                null,
                XPathResult.FIRST_ORDERED_NODE_TYPE,
                null
            ).singleNodeValue;
            const pageContentText = pageContentElement ? pageContentElement.textContent.trim() : '';

            // 自定义标题
            const newTitle = `:: ${pageContentText}`;
            document.title = newTitle;
        }

    }

    // 监听URL变化
    let lastUrl = window.location.href;

    // 创建一个观察器来检测URL变化
    const observer = new MutationObserver(() => {
        const currentUrl = window.location.href;
        if (currentUrl !== lastUrl) {
            lastUrl = currentUrl;
            // 给DOM一些时间加载
            setTimeout(updatePageTitle, 500);
        }
    });
    observer.observe(document, { subtree: true, childList: true });

    // 初始执行一次
    setTimeout(updatePageTitle, 1000);
})();