DeepCo HR Tracker

Says here your name is....

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         DeepCo HR Tracker
// @namespace    deepco_hr
// @version      2026.04.22
// @description  Says here your name is....
// @icon         https://www.google.com/s2/favicons?sz=64&domain=deepco.app
// @match        https://deepco.app/*
// @license      MIT
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
    'use strict';

    const getVar = (k, d, description = '') => {
        const o = GM_getValue('aaa_user_variables', {});

        if (!(k in o)) {
            o[k] = { value: d, description };
            GM_setValue('aaa_user_variables', o);
            return d;
        }

        if (typeof o[k] !== 'object' || o[k] === null || !('value' in o[k])) {
            o[k] = { value: o[k], description };
            GM_setValue('aaa_user_variables', o);
            return o[k].value;
        }

        if (description && !o[k].description) {
            o[k].description = description;
            GM_setValue('aaa_user_variables', o);
        }

        return o[k].value;
    };

    // ---------- User adjustable config ----------
    const DEBUG     = getVar('DEBUG',     false, "Enable console logging.");
    const SHOW_ID   = getVar('SHOW_ID',   false,  "Prefix the WorkerID on names.");
    const HIGHLIGHT = getVar('HIGHLIGHT', false,  "Apply 'valuable' CSS to new names.");
    const STORAGE_KEY = "workerAliasMap";

    const workerMap = new Map(Object.entries(GM_getValue(STORAGE_KEY, {})));

    function log(...args) {
        if (DEBUG) console.log("[DeepCo HR]", ...args);
    }

    function save() {
        const obj = {};
        for (const [id, names] of workerMap.entries()) {
            obj[id] = Array.from(names);
        }
        GM_setValue(STORAGE_KEY, obj);
    }

    function updateAnchorTitle(anchor, workerId) {
        const names = Array.from(workerMap.get(workerId) || []);
        anchor.title = `${workerId}: ${names.join(' | ')}`;
    }

    function processMessage(el) {
        const workerId = el.dataset.workerId?.replace(/"/g, '');
        if (!workerId) return;

        const anchor = el.querySelector('.worker-name-link');
        const nameSpan = anchor?.querySelector('.truncate');
        if (!anchor || !nameSpan) return;

        const name = nameSpan.textContent.trim();
        if (!name) return;

        if (!workerMap.has(workerId)) {
            workerMap.set(workerId, new Set());
        } else if (!(workerMap.get(workerId) instanceof Set)) {
            workerMap.set(workerId, new Set(workerMap.get(workerId)));
        }

        const set = workerMap.get(workerId);
        const beforeSize = set.size;
        set.add(name);
        const isNewAlias = set.size !== beforeSize;

        if (isNewAlias) {
            log("New alias", workerId, name);
            save();
        }

        // prefix worker id
        if (SHOW_ID && !nameSpan.dataset.prefixed) {
            nameSpan.textContent = `${workerId}:${name}`;
            nameSpan.dataset.prefixed = "1";
        }

        // highlight new alias
        if (HIGHLIGHT && isNewAlias) {
            log("Applying class")
            if (nameSpan) {
                nameSpan.classList.add('valuable');
            }
        }

        updateAnchorTitle(anchor, workerId);
    }

    function scan() {
        document.querySelectorAll('[data-role="chat-message"]').forEach(processMessage);
    }

    scan();

    const observer = new MutationObserver(mutations => {
        for (const m of mutations) {
            for (const node of m.addedNodes) {
                if (!(node instanceof HTMLElement)) continue;

                if (node.matches?.('[data-role="chat-message"]')) {
                    processMessage(node);
                }

                node.querySelectorAll?.('[data-role="chat-message"]').forEach(processMessage);
            }
        }
    });

    observer.observe(document, {
        childList: true,
        subtree: true
    });

})();