Greasy Fork

来自缓存

Greasy Fork is available in English.

DeepSeek - Chat title on tab title

Chat title on tab title

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         DeepSeek - Chat title on tab title
// @namespace    http://tampermonkey.net/
// @version      2025-03-11
// @description  Chat title on tab title
// @author       jackiechan285
// @match        https://chat.deepseek.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=deepseek.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    class ChatTitleManager {
        constructor() {
            this.storage = new Map();
            this.currentChatId = null;
        }

        handleApiData(chatId, title) {
            if (!chatId || !title) return;

            this.storage.set(chatId, title);
            if (chatId === this.currentChatId) {
                this.updateTabTitle(title);
            }
        }

        setCurrentChat(chatId) {
            this.currentChatId = chatId;
            const cachedTitle = this.storage.get(chatId) || 'DeepSeek';
            this.updateTabTitle(cachedTitle);
        }

        updateTabTitle(title) {
            if (title && document.title !== title) {
                document.title = title;
            }
        }
    }

    const titleManager = new ChatTitleManager();

    function getChatIdFromPath() {
        const match = window.location.pathname.match(/\/chat\/[^/]+\/([^/]+)/);
        return match?.[1] || null;
    }

    function handleUrlChange() {
        titleManager.setCurrentChat(getChatIdFromPath());
    }

    function setupHistoryMonitoring() {
        const originalPushState = history.pushState.bind(history);
        const originalReplaceState = history.replaceState.bind(history);

        history.pushState = function(state, title, url) {
            originalPushState(state, title, url);
            window.dispatchEvent(new Event('urlchange'));
        };

        history.replaceState = function(state, title, url) {
            originalReplaceState(state, title, url);
            window.dispatchEvent(new Event('urlchange'));
        };

        window.addEventListener('popstate', () => {
            window.dispatchEvent(new Event('urlchange'));
        });

        window.addEventListener('urlchange', handleUrlChange);
    }

    function setupXHRInterceptor() {
        const originalSend = XMLHttpRequest.prototype.send;
        const originalOpen = XMLHttpRequest.prototype.open;

        XMLHttpRequest.prototype.open = function(method, url) {
            this._method = method;
            this._requestUrl = url;
            return originalOpen.apply(this, arguments);
        };

        XMLHttpRequest.prototype.send = function(data) {
            const xhr = this;
            const url = xhr._requestUrl;
            const method = xhr._method?.toUpperCase();

            if (method === 'POST' && url.includes('/api/v0/chat_session/update_title')) {
                try {
                    const payload = JSON.parse(data);
                    titleManager.handleApiData(payload?.chat_session_id, payload?.title);
                } catch (e) {}
            }

            xhr.addEventListener('readystatechange', function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    try {
                        const response = JSON.parse(xhr.responseText);
                        const bizData = response?.data?.biz_data;

                        if (url.includes('/api/v0/chat/history_messages')) {
                            titleManager.handleApiData(bizData?.chat_session?.id, bizData?.chat_session?.title);
                        }
                        else if (url.includes('/api/v0/chat_session/items')) {
                            const session = bizData?.chat_sessions?.[0];
                            titleManager.handleApiData(session?.chat_session_id, session?.item?.title);
                        }
                    } catch (e) {}
                }
            });

            return originalSend.apply(xhr, arguments);
        };
    }

    document.addEventListener('DOMContentLoaded', async function() {
        await new Promise(resolve => setTimeout(resolve, 10));
        handleUrlChange();
    });

    setupHistoryMonitoring();
    setupXHRInterceptor();
})();