« Module:MinecraftConnect » : différence entre les versions
De Nefald
Autres actions
mAucun résumé des modifications |
m échappement |
||
| Ligne 11 : | Ligne 11 : | ||
local server = mw.text.trim(args[1] or args.serveur or 'play.exemple.fr') | 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 port = mw.text.trim(args[2] or args.port or '25565') | ||
local texte = mw.text.trim(args[3] or args.texte or | local texte = mw.text.trim(args[3] or args.texte or "Copier l'adresse") | ||
-- Construction de l'adresse complète | -- Construction de l'adresse complète | ||
| Ligne 21 : | Ligne 21 : | ||
end | end | ||
-- Échappement pour JavaScript | -- Échappement pour JavaScript (utilisation de mw.text.jsonEncode) | ||
local | local adresseJS = mw.text.jsonEncode(adresseComplete) | ||
local | local texteHTML = mw.text.encode(texte) | ||
-- Génération d'un ID unique pour éviter les conflits | -- Génération d'un ID unique pour éviter les conflits | ||
local uniqueId = 'mc-btn-' .. mw.text.encode(server:gsub('[^%w]', '-')) | local uniqueId = 'mc-btn-' .. mw.text.encode(server:gsub('[^%w]', '-')) | ||
-- HTML du bouton | -- HTML du bouton | ||
local html = mw.html.create('span') | local html = mw.html.create('span') | ||
:addClass('minecraft-connect-button') | :addClass('minecraft-connect-button') | ||
| Ligne 37 : | Ligne 37 : | ||
:attr('title', 'Cliquer pour copier : ' .. mw.text.encode(adresseComplete)) | :attr('title', 'Cliquer pour copier : ' .. mw.text.encode(adresseComplete)) | ||
:attr('data-server', adresseComplete) | :attr('data-server', adresseComplete) | ||
:wikitext( | :wikitext(texteHTML .. ' 📋') -- Code HTML pour 📋 | ||
:done() | :done() | ||
| Ligne 43 : | Ligne 43 : | ||
local jsCode = string.format([[ | local jsCode = string.format([[ | ||
(function() { | (function() { | ||
var btn = document.getElementById( | var btn = document.getElementById(%s).querySelector('button'); | ||
if (!btn) return; | if (!btn) return; | ||
| Ligne 51 : | Ligne 51 : | ||
if (navigator.clipboard && window.isSecureContext) { | if (navigator.clipboard && window.isSecureContext) { | ||
navigator.clipboard.writeText(adresse).then(function() { | navigator.clipboard.writeText(adresse).then(function() { | ||
afficherFeedback(btn, ' | afficherFeedback(btn, '\u2713 Copié !', 'success'); | ||
}).catch(function() { | }).catch(function() { | ||
fallbackCopy(adresse, btn); | fallbackCopy(adresse, btn); | ||
| Ligne 71 : | Ligne 71 : | ||
var success = document.execCommand('copy'); | var success = document.execCommand('copy'); | ||
if (success) { | if (success) { | ||
afficherFeedback(bouton, ' | afficherFeedback(bouton, '\u2713 Copié !', 'success'); | ||
} else { | } else { | ||
afficherFeedback(bouton, ' | afficherFeedback(bouton, '\u2717 Erreur', 'error'); | ||
} | } | ||
} catch (err) { | } catch (err) { | ||
afficherFeedback(bouton, ' | afficherFeedback(bouton, '\u2717 Erreur', 'error'); | ||
} | } | ||
| Ligne 104 : | Ligne 104 : | ||
})(); | })(); | ||
]], | ]], | ||
mw.text. | mw.text.jsonEncode(uniqueId), | ||
adresseJS | |||
) | ) | ||
Version du 10 novembre 2025 à 08:56
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