« MediaWiki:Common.js » : différence entre les versions
Page de l’interface de MediaWiki
Autres actions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 14 : | Ligne 14 : | ||
} ); | } ); | ||
/** | |||
* MinecraftConnect - Boutons de copie d'adresse serveur Minecraft | |||
* Inspiré de l'extension PreToClip | |||
*/ | |||
(function() { | (function() { | ||
'use strict'; | 'use strict'; | ||
function initMinecraftButtons($content) { | |||
$content.find('.minecraft-connect- | $content.find('.minecraft-connect-wrapper').each(function() { | ||
var $ | var $wrapper = $(this); | ||
// Éviter la double initialisation | // Éviter la double initialisation | ||
if ($ | if ($wrapper.data('mc-initialized')) { | ||
$ | return; | ||
} | |||
$wrapper.data('mc-initialized', true); | |||
var serverAddress = $wrapper.data('mc-server'); | |||
var buttonText = $wrapper.data('mc-text'); | |||
// Créer le bouton | |||
var $button = $('<button>') | |||
.addClass('mw-ui-button mw-ui-progressive minecraft-connect-btn') | |||
.attr('type', 'button') | |||
.attr('title', 'Cliquer pour copier : ' + serverAddress) | |||
.text(buttonText + ' 📋'); | |||
// Remplacer le span par le bouton | |||
$wrapper.replaceWith($button); | |||
$ | // Gestion du clic | ||
$button.on('click', function() { | |||
copyToClipboard(serverAddress, $button, buttonText); | |||
copyToClipboard( | |||
}); | }); | ||
}); | }); | ||
} | } | ||
function copyToClipboard(text, $button) { | function copyToClipboard(text, $button, originalText) { | ||
// Méthode moderne (Clipboard API) | // Méthode moderne (Clipboard API) | ||
if (navigator.clipboard && | if (navigator.clipboard && navigator.clipboard.writeText) { | ||
navigator.clipboard.writeText(text).then(function() { | navigator.clipboard.writeText(text).then( | ||
function() { | |||
showSuccess($button, originalText); | |||
}, | |||
function() { | |||
// Fallback si échec | |||
fallbackCopy(text, $button, originalText); | |||
} | |||
); | |||
} else { | } else { | ||
// Fallback pour anciens navigateurs | |||
fallbackCopy(text, $button, originalText); | fallbackCopy(text, $button, originalText); | ||
} | } | ||
| Ligne 53 : | Ligne 71 : | ||
function fallbackCopy(text, $button, originalText) { | function fallbackCopy(text, $button, originalText) { | ||
var $temp = $('<textarea>') | var $temp = $('<textarea>') | ||
.val(text) | |||
.css({ | .css({ | ||
position: 'fixed', | position: 'fixed', | ||
| Ligne 63 : | Ligne 82 : | ||
outline: 'none', | outline: 'none', | ||
boxShadow: 'none', | boxShadow: 'none', | ||
background: 'transparent' | background: 'transparent' | ||
}) | }) | ||
.appendTo('body'); | |||
.appendTo('body') | |||
$temp[0].select(); | |||
$temp[0].setSelectionRange(0, 99999); | |||
try { | try { | ||
var successful = document.execCommand('copy'); | var successful = document.execCommand('copy'); | ||
if (successful) { | if (successful) { | ||
showSuccess($button, originalText | showSuccess($button, originalText); | ||
} else { | } else { | ||
showError($button, originalText); | showError($button, originalText); | ||
| Ligne 84 : | Ligne 103 : | ||
} | } | ||
function showSuccess($button, originalText | function showSuccess($button, originalText) { | ||
mw.notify('Adresse copiée dans le presse-papier !', { | |||
mw.notify('Adresse copiée | |||
type: 'success', | type: 'success', | ||
autoHide: true, | autoHide: true, | ||
tag: 'minecraft- | tag: 'minecraft-connect' | ||
}); | }); | ||
$button | $button | ||
.text('✓ Copié !') | .text('✓ Copié !') | ||
.removeClass('mw-ui-progressive') | .removeClass('mw-ui-progressive') | ||
.addClass('mw-ui-constructive'); | .addClass('mw-ui-constructive') | ||
.prop('disabled', true); | |||
setTimeout(function() { | setTimeout(function() { | ||
$button | $button | ||
.text(originalText) | .text(originalText + ' 📋') | ||
.prop('disabled', false) | .prop('disabled', false) | ||
.removeClass('mw-ui-constructive') | .removeClass('mw-ui-constructive') | ||
| Ligne 111 : | Ligne 128 : | ||
mw.notify('Erreur lors de la copie', { | mw.notify('Erreur lors de la copie', { | ||
type: 'error', | type: 'error', | ||
autoHide: true | autoHide: true, | ||
tag: 'minecraft-connect' | |||
}); | }); | ||
| Ligne 121 : | Ligne 139 : | ||
setTimeout(function() { | setTimeout(function() { | ||
$button | $button | ||
.text(originalText) | .text(originalText + ' 📋') | ||
.removeClass('mw-ui-destructive') | .removeClass('mw-ui-destructive') | ||
.addClass('mw-ui-progressive'); | .addClass('mw-ui-progressive'); | ||
}, 2000); | }, 2000); | ||
} | } | ||
// Initialisation au chargement et pour VisualEditor | |||
mw.hook('wikipage.content').add(initMinecraftButtons); | |||
}()); | }()); | ||