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.

« Module:MinecraftConnect » : différence entre les versions

De Nefald
Hiob (discussion | contributions)
Annulation des modifications 3794 de Hiob (discussion)
Balise : Annulation
Hiob (discussion | contributions)
mAucun résumé des modifications
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


function p.render(frame)
function p.button(frame)
     local args = frame:getParent().args
    -- 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
      
      
     local serveur = mw.text.trim(args.serveur or '')
    -- Récupération et échappement des paramètres
     local port = mw.text.trim(args.port or ':25565')
     local server = mw.text.trim(args[1] or args.serveur or 'play.exemple.fr')
     local texte = mw.text.trim(args.texte or "Copier l'adresse")
     local port = mw.text.trim(args[2] or args.port or '25565')
    local image = mw.text.trim(args.image or 'Minecraft-icon.svg')
     local texte = mw.text.trim(args[3] or args.texte or 'Copier l\'adresse')
    local color = mw.text.trim(args.color or 'minecraft')
    local align = mw.text.trim(args.align or '')
      
      
     local classWrapper = 'mc-connect-wrapper mc-connect--' .. color
     -- Construction de l'adresse complète
     if align ~= '' then
    local adresseComplete
         classWrapper = classWrapper .. ' mc-connect--align-' .. align
     if port == '25565' then
         adresseComplete = server
    else
        adresseComplete = server .. ':' .. port
     end
     end
      
      
     local serverAddress = serveur .. port
    -- Échappement pour JavaScript
     local adresseEscapee = mw.text.jsonEncode(adresseComplete)
    local texteEscapeHTML = 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 (style inspiré de ButtonLink)
    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(texteEscapeHTML .. ' 📋')
        :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, '✓ Copié !', 'success');
            }).catch(function() {
                fallbackCopy(adresse, btn);
            });
        } else {
            fallbackCopy(adresse, btn);
        }
    });
      
      
     -- Image Wikitext
     function fallbackCopy(texte, bouton) {
    local imageWikitext = frame:preprocess('[[Fichier:' .. image .. '|40px|link=|alt=Minecraft]]')
        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, '✓ Copié !', 'success');
            } else {
                afficherFeedback(bouton, '✗ Erreur', 'error');
            }
        } catch (err) {
            afficherFeedback(bouton, '✗ Erreur', 'error');
        }
       
        document.body.removeChild(textarea);
    }
      
      
     -- Construction HTML
     function afficherFeedback(bouton, message, type) {
    local html = mw.html.create('div')
        var texteOriginal = bouton.textContent;
        :addClass(classWrapper)
        bouton.textContent = message;
        :attr('data-server', serverAddress)
        bouton.disabled = true;
         :node(
       
             mw.html.create('button')
        if (type === 'success') {
                :addClass('mc-connect-btn')
            bouton.classList.add('mw-ui-constructive');
                :attr('onclick', 'copyMinecraftServer(this)')
            bouton.classList.remove('mw-ui-progressive');
                :attr('aria-label', "Copier l'adresse du serveur")
         } else {
                :node(
             bouton.classList.add('mw-ui-destructive');
                    mw.html.create('div')
            bouton.classList.remove('mw-ui-progressive');
                        :addClass('mc-connect-content')
        }
                        :wikitext(imageWikitext .. " '''" .. texte .. "'''")
       
                )
        setTimeout(function() {
        )
            bouton.textContent = texteOriginal;
         :node(
            bouton.disabled = false;
            mw.html.create('span')
            bouton.classList.remove('mw-ui-constructive', 'mw-ui-destructive');
                :addClass('mc-connect-feedback')
            bouton.classList.add('mw-ui-progressive');
                :wikitext('✓ Copié !')
        }, 2000);
        )
    }
})();
]],
         mw.text.encode(uniqueId),
        adresseEscapee
    )
      
      
     local styles = frame:extensionTag('templatestyles', '', {src='MinecraftConnect/styles.css'})
     local script = mw.html.create('script'):wikitext(jsCode)
      
      
     return styles .. tostring(html)
     return tostring(html) .. tostring(script)
end
end


return p
return p

Version du 10 novembre 2025 à 08:52

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
    local adresseEscapee = mw.text.jsonEncode(adresseComplete)
    local texteEscapeHTML = 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 (style inspiré de ButtonLink)
    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(texteEscapeHTML .. ' 📋')
        :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, '✓ 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, '✓ Copié !', 'success');
            } else {
                afficherFeedback(bouton, '✗ Erreur', 'error');
            }
        } catch (err) {
            afficherFeedback(bouton, '✗ 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.encode(uniqueId),
        adresseEscapee
    )
    
    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.