Jump to content

MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 2: Line 2:




/* Botão de copiar para blocos de código */
$(function() {
$(function() {
     $('pre, .mw-code, .syntaxhighlight').each(function() {
    // Procura apenas por blocos de código técnicos e ignora a área de produtos
     $('.mw-highlight pre, .syntaxhighlight pre, pre.mw-code-example').each(function() {
         var $this = $(this);
         var $this = $(this);
       
        // Verifica se o elemento já tem um botão para não duplicar
        if ($this.parent().find('.copy-code-button').length > 0) return;
         var $button = $('<button>')
         var $button = $('<button>')
             .text('Copy')
             .text('Copy')
             .addClass('copy-code-button')
             .addClass('copy-code-button')
            .css({
                'float': 'right',
                'cursor': 'pointer',
                'padding': '2px 8px',
                'font-size': '12px'
            })
             .click(function() {
             .click(function() {
                 var text = $this.text().replace(/Copy$/, ''); // Remove o texto do próprio botão
                 var text = $this.text();
                 navigator.clipboard.writeText(text).then(function() {
                 navigator.clipboard.writeText(text).then(function() {
                     $button.text('Copied!');
                     $button.text('Copied!');
Line 22: Line 20:
                 });
                 });
             });
             });
         $this.prepend($button);
 
        // Insere o botão antes do bloco de código
         $this.before($button);
     });
     });
});
});