DeepCo HR Tracker

Says here your name is....

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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
    });

})();