Basculer le menu
Changer de menu des préférences
Basculer le menu personnel
Non connecté(e)
Votre adresse IP sera visible au public si vous faites des modifications.
Version datée du 10 novembre 2025 à 08:56 par Hiob (discussion | contributions) (échappement)

La documentation pour ce module peut être créée à Module:MinecraftConnect/doc

local p = {}

function p.button(frame)
    -- Récupération des arguments du frame parent (pour {{#invoke}})
    local args = frame.args
    if not args[1] and frame:getParent() then
        args = frame:getParent().args
    end
    
    -- Récupération et échappement des paramètres
    local server = mw.text.trim(args[1] or args.serveur or 'play.exemple.fr')
    local port = mw.text.trim(args[2] or args.port or '25565')
    local texte = mw.text.trim(args[3] or args.texte or "Copier l'adresse")
    
    -- Construction de l'adresse complète
    local adresseComplete
    if port == '25565' then
        adresseComplete = server
    else
        adresseComplete = server .. ':' .. port
    end
    
    -- Échappement pour JavaScript (utilisation de mw.text.jsonEncode)
    local adresseJS = mw.text.jsonEncode(adresseComplete)
    local texteHTML = mw.text.encode(texte)
    
    -- Génération d'un ID unique pour éviter les conflits
    local uniqueId = 'mc-btn-' .. mw.text.encode(server:gsub('[^%w]', '-'))
    
    -- HTML du bouton
    local html = mw.html.create('span')
        :addClass('minecraft-connect-button')
        :attr('id', uniqueId)
        :tag('button')
            :addClass('mw-ui-button mw-ui-progressive')
            :attr('type', 'button')
            :attr('title', 'Cliquer pour copier : ' .. mw.text.encode(adresseComplete))
            :attr('data-server', adresseComplete)
            :wikitext(texteHTML .. ' 📋') -- Code HTML pour 📋
        :done()
    
    -- JavaScript pour la copie dans le presse-papier
    local jsCode = string.format([[
(function() {
    var btn = document.getElementById(%s).querySelector('button');
    if (!btn) return;
    
    btn.addEventListener('click', function() {
        var adresse = %s;
        
        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(adresse).then(function() {
                afficherFeedback(btn, '\u2713 Copié !', 'success');
            }).catch(function() {
                fallbackCopy(adresse, btn);
            });
        } else {
            fallbackCopy(adresse, btn);
        }
    });
    
    function fallbackCopy(texte, bouton) {
        var textarea = document.createElement('textarea');
        textarea.value = texte;
        textarea.style.position = 'fixed';
        textarea.style.opacity = '0';
        document.body.appendChild(textarea);
        textarea.select();
        
        try {
            var success = document.execCommand('copy');
            if (success) {
                afficherFeedback(bouton, '\u2713 Copié !', 'success');
            } else {
                afficherFeedback(bouton, '\u2717 Erreur', 'error');
            }
        } catch (err) {
            afficherFeedback(bouton, '\u2717 Erreur', 'error');
        }
        
        document.body.removeChild(textarea);
    }
    
    function afficherFeedback(bouton, message, type) {
        var texteOriginal = bouton.textContent;
        bouton.textContent = message;
        bouton.disabled = true;
        
        if (type === 'success') {
            bouton.classList.add('mw-ui-constructive');
            bouton.classList.remove('mw-ui-progressive');
        } else {
            bouton.classList.add('mw-ui-destructive');
            bouton.classList.remove('mw-ui-progressive');
        }
        
        setTimeout(function() {
            bouton.textContent = texteOriginal;
            bouton.disabled = false;
            bouton.classList.remove('mw-ui-constructive', 'mw-ui-destructive');
            bouton.classList.add('mw-ui-progressive');
        }, 2000);
    }
})();
]], 
        mw.text.jsonEncode(uniqueId),
        adresseJS
    )
    
    local script = mw.html.create('script'):wikitext(jsCode)
    
    return tostring(html) .. tostring(script)
end

return p
Les témoins (''cookies'') nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de témoins.