« MediaWiki:Common.js » : différence entre les versions
Page de l’interface de MediaWiki
Autres actions
{{MinecraftConnect}} |
Aucun résumé des modifications |
||
| Ligne 15 : | Ligne 15 : | ||
/ | // Gestion des boutons Minecraft Connect (inspiré de PreToClip) | ||
(function() { | |||
'use strict'; | |||
function | |||
navigator.clipboard.writeText( | mw.hook('wikipage.content').add(function($content) { | ||
var | $content.find('.minecraft-connect-btn').each(function() { | ||
var $btn = $(this); | |||
// Éviter la double initialisation | |||
if ($btn.data('mc-initialized')) return; | |||
$btn.data('mc-initialized', true); | |||
$btn.on('click', function(e) { | |||
e.preventDefault(); | |||
var textToCopy = $btn.attr('data-copy-text'); | |||
copyToClipboard(textToCopy, $btn); | |||
}); | |||
}); | |||
}); | |||
function copyToClipboard(text, $button) { | |||
var originalText = $button.text(); | |||
// Méthode moderne (Clipboard API) | |||
if (navigator.clipboard && window.isSecureContext) { | |||
navigator.clipboard.writeText(text).then(function() { | |||
showSuccess($button, originalText, text); | |||
}).catch(function(err) { | |||
fallbackCopy(text, $button, originalText); | |||
}); | |||
} else { | |||
fallbackCopy(text, $button, originalText); | |||
} | |||
} | |||
function fallbackCopy(text, $button, originalText) { | |||
var $temp = $('<textarea>') | |||
.css({ | |||
position: 'fixed', | |||
top: 0, | |||
left: 0, | |||
width: '2em', | |||
height: '2em', | |||
padding: 0, | |||
border: 'none', | |||
outline: 'none', | |||
boxShadow: 'none', | |||
background: 'transparent', | |||
opacity: 0 | |||
}) | |||
.val(text) | |||
.appendTo('body') | |||
.select(); | |||
try { | |||
var successful = document.execCommand('copy'); | |||
if (successful) { | |||
showSuccess($button, originalText, text); | |||
} else { | |||
showError($button, originalText); | |||
} | |||
} catch (err) { | |||
showError($button, originalText); | |||
} | |||
$temp.remove(); | |||
} | |||
function showSuccess($button, originalText, copiedText) { | |||
// Notification MediaWiki (comme PreToClip) | |||
mw.notify('Adresse copiée : ' + copiedText, { | |||
type: 'success', | |||
autoHide: true, | |||
tag: 'minecraft-copy' | |||
}); | |||
// Feedback visuel sur le bouton | |||
$button | |||
.text('✓ Copié !') | |||
.prop('disabled', true) | |||
.removeClass('mw-ui-progressive') | |||
.addClass('mw-ui-constructive'); | |||
setTimeout(function() { | setTimeout(function() { | ||
$button | |||
.text(originalText) | |||
.prop('disabled', false) | |||
.removeClass('mw-ui-constructive') | |||
.addClass('mw-ui-progressive'); | |||
}, 2000); | }, 2000); | ||
} | |||
function showError($button, originalText) { | |||
mw.notify('Erreur lors de la copie', { | |||
type: 'error', | |||
autoHide: true | |||
}); | |||
$button | |||
.text('✗ Erreur') | |||
.removeClass('mw-ui-progressive') | |||
.addClass('mw-ui-destructive'); | |||
setTimeout(function() { | |||
$button | |||
.text(originalText) | |||
.removeClass('mw-ui-destructive') | |||
.addClass('mw-ui-progressive'); | |||
}, 2000); | |||
} | |||
}()); | |||