Module:MinecraftConnect
De Nefald
Autres actions
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