|
|
| (3 versions intermédiaires par le même utilisateur non affichées) |
| Ligne 2 : |
Ligne 2 : |
|
| |
|
| function p.button(frame) | | function p.button(frame) |
| -- Récupération des arguments du frame parent (pour {{#invoke}}) | | -- Récupération des arguments |
| local args = frame.args | | local args = frame.args |
| if not args[1] and frame:getParent() then | | if not args[1] and frame:getParent() then |
| Ligne 8 : |
Ligne 8 : |
| end | | end |
| | | |
| -- Récupération et échappement des paramètres
| |
| 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') |
| Ligne 14 : |
Ligne 13 : |
| | | |
| -- Construction de l'adresse complète | | -- Construction de l'adresse complète |
| local adresseComplete | | local adresseComplete = (port == '25565') and server or (server .. ':' .. port) |
| if port == '25565' then
| |
| adresseComplete = server
| |
| else
| |
| adresseComplete = server .. ':' .. port
| |
| end
| |
|
| |
| -- Échappement manuel pour JavaScript
| |
| local function jsEscape(str)
| |
| return str:gsub("\\", "\\\\"):gsub("'", "\\'"):gsub('"', '\\"'):gsub("\n", "\\n")
| |
| end
| |
|
| |
| local adresseJS = jsEscape(adresseComplete)
| |
| local texteHTML = mw.text.encode(texte)
| |
|
| |
| -- Génération d'un ID unique pour éviter les conflits
| |
| local uniqueId = 'mc-btn-' .. 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 .. ' 📋')
| |
| :done()
| |
|
| |
| -- JavaScript pour la copie dans le presse-papier
| |
| local jsCode = [[
| |
| (function() {
| |
| var btn = document.getElementById(']] .. uniqueId .. [[').querySelector('button');
| |
| if (!btn) return;
| |
|
| |
| btn.addEventListener('click', function() {
| |
| var adresse = ']] .. adresseJS .. [[';
| |
|
| |
| 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);
| |
| }
| |
| })();
| |
| ]]
| |
| | | |
| local script = mw.html.create('script'):wikitext(jsCode) | | -- Création d'un simple span avec data attributes (comme PreToClip) |
| | local container = mw.html.create('span') |
| | :addClass('minecraft-connect-wrapper') |
| | :attr('data-mc-server', adresseComplete) |
| | :attr('data-mc-text', texte) |
| | :wikitext(texte .. ' 📋') |
| | | |
| return tostring(html) .. tostring(script) | | return tostring(container) |
| end | | end |
|
| |
|
| return p | | return p |