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.

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greasyfork.org/scripts/588168/1882655/GA%20Core%20%28ManagerZone%29.js

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

})();