GA Core (ManagerZone)

Núcleo compartido del GA ManagerZone Assistant: inicializa el namespace window.GA, expone mzFetch (byte-idéntico al monolito) y GA.cargarEnCruce (inyección unificada al input de Cruzar Usuarios). Pensado para @require desde el userscript principal. Debe cargarse PRIMERO.

Tento skript by nemal byť nainštalovaný priamo. Je to knižnica pre ďalšie skripty, ktorú by mali používať cez meta príkaz // @require https://update.greasyfork.org/scripts/588168/1882655/GA%20Core%20%28ManagerZone%29.js

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name         GA Core (ManagerZone)
// @namespace    https://control.managerzone.com/ga
// @version      1.0.0
// @description  Núcleo compartido del GA ManagerZone Assistant: inicializa el namespace window.GA, expone mzFetch (byte-idéntico al monolito) y GA.cargarEnCruce (inyección unificada al input de Cruzar Usuarios). Pensado para @require desde el userscript principal. Debe cargarse PRIMERO.
// ==/UserScript==

(function () {
    'use strict';

    // Namespace compartido entre el principal y todas las libraries @require.
    // En el sandbox de Tampermonkey, window.GA persiste a través de los
    // @require y del script principal (no se filtra a la página).
    window.GA = window.GA || {};
    const GA = window.GA;

    // ------------------------------------------------------------------
    //  mzFetch  — wrapper de GM_xmlhttpRequest (GET) que devuelve Promise.
    //  Cuerpo BYTE-IDÉNTICO al del monolito (userscript 1.7.2, líneas
    //  148–167). Lo usan ip-amistosos, cruce (resolveUser) y racimo.
    //
    //  NOTA: requiere que el PRINCIPAL declare `// @grant GM_xmlhttpRequest`
    //  en su metadata; los @grant no pueden ir en un archivo @require-ado.
    // ------------------------------------------------------------------
    function mzFetch(url) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                onload: function(res) {
                    if (res.status === 200) {
                        resolve(res.responseText);
                    } else {
                        reject("Error HTTP: " + res.status);
                    }
                },
                onerror: function(err) {
                    reject(err);
                }
            });
        });
    }
    GA.mzFetch = mzFetch;

    // ------------------------------------------------------------------
    //  cargarEnCruce  — CÓDIGO NUEVO. Unifica la "danza" de inyección que
    //  hoy está duplicada en 4 sitios (limpiador, botones [+] de bidhistory,
    //  y dos en analizarTransferencias). Agrega uno o más tokens al input
    //  de "Cruzar Usuarios" (#ga-simple-cross-input), deduplicando
    //  case-insensitive y preservando el orden.
    //
    //  Semántica idéntica a las copias existentes:
    //    · split(/[ ,\t\n]+/) → trim → filter(Boolean) sobre lo ya cargado
    //    · dedup case-insensitive vía Set de minúsculas
    //    · join(', ')
    //
    //  @param   {string|Array<string|number>} nombres  token o lista.
    //  @returns {number}  cantidad de tokens efectivamente agregados.
    //
    //  Si el widget de cruce no está en la página, no hace nada y devuelve 0.
    //  Un caller que necesite distinguir "widget ausente" de "0 nuevos"
    //  (p. ej. para alertar) debe chequear el elemento antes de llamar.
    // ------------------------------------------------------------------
    GA.cargarEnCruce = function (nombres) {
        const input = document.getElementById('ga-simple-cross-input');
        if (!input) return 0;

        const actuales = input.value.split(/[ ,\t\n]+/).map(s => s.trim()).filter(Boolean);
        const lower = new Set(actuales.map(s => s.toLowerCase()));
        let agregados = 0;

        (Array.isArray(nombres) ? nombres : [nombres]).forEach(n => {
            n = String(n == null ? '' : n).trim();
            if (n && !lower.has(n.toLowerCase())) {
                actuales.push(n);
                lower.add(n.toLowerCase());
                agregados++;
            }
        });

        input.value = actuales.join(', ');
        return agregados;
    };

})();