DeepCo HR Tracker

Says here your name is....

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==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
    });

})();