Difference between revisions of "MediaWiki:Common.js"
Jump to navigation
Jump to search
Line 2: | Line 2: | ||
$(document).ready(function() { | $(document).ready(function() { | ||
− | + | // Função para copiar texto e mostrar feedback | |
− | var textToCopy = $( | + | function handleCopy(element) { |
+ | var textToCopy = $(element).attr('data-copy'); | ||
+ | // Cria elemento temporário para copiar | ||
var tempInput = document.createElement('textarea'); | var tempInput = document.createElement('textarea'); | ||
tempInput.value = textToCopy; | tempInput.value = textToCopy; | ||
document.body.appendChild(tempInput); | document.body.appendChild(tempInput); | ||
tempInput.select(); | tempInput.select(); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | try { | |
− | + | document.execCommand('copy'); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | // | + | // Adiciona classe de feedback |
− | + | $(element).addClass('copied'); | |
− | |||
− | // | + | // Remove feedback após 1.5 segundos |
− | + | setTimeout(function() { | |
− | + | $(element).removeClass('copied'); | |
− | + | }, 1500); | |
− | } | ||
− | + | } catch (err) { | |
− | + | console.error('Erro ao copiar:', err); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | |||
+ | document.body.removeChild(tempInput); | ||
} | } | ||
− | + | ||
− | // | + | // Click nos elementos .warp-copy |
− | function | + | $(document).on('click', '.warp-copy', function() { |
− | + | handleCopy(this); | |
− | + | }); | |
− | + | ||
− | } | + | // Click nas imagens dos NPCs |
+ | function initNpcCopy() { | ||
+ | $('.contents-equipment .tile-top.tile-image a').off('click.npccopy').on('click.npccopy', function(e) { | ||
+ | e.preventDefault(); | ||
+ | var npcId = $(this).attr('href').replace('#',''); | ||
+ | handleCopy($(this).closest('.contents-equipment').find('.warp-copy')[0]); | ||
+ | }); | ||
} | } | ||
− | + | ||
− | // | + | // Inicializa quando o documento está pronto |
+ | initNpcCopy(); | ||
+ | |||
+ | // Reinicia quando novo conteúdo é carregado | ||
if (typeof mw !== 'undefined' && mw.hook) { | if (typeof mw !== 'undefined' && mw.hook) { | ||
mw.hook('wikipage.content').add(function() { | mw.hook('wikipage.content').add(function() { | ||
− | + | initNpcCopy(); | |
− | |||
− | |||
− | |||
− | |||
− | |||
}); | }); | ||
} | } | ||
− | |||
− | + | // Auto-expandir seções com anchor links | |
− | + | function expandSectionForAnchor(hash) { | |
− | // | + | var targetElement = $(hash); |
− | function | + | if (targetElement.length) { |
− | + | targetElement.parents('.mw-collapsible.mw-collapsed').removeClass('mw-collapsed') | |
− | + | .find('.mw-collapsible-content').show(); | |
− | + | setTimeout(function() { | |
− | + | $('html, body').animate({ scrollTop: targetElement.offset().top - 100 }, 200); | |
− | + | }, 400); | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | } | ||
} | } | ||
− | + | ||
− | // | + | // Trata hash na URL inicial |
− | + | if (window.location.hash) { | |
− | + | setTimeout(function() { expandSectionForAnchor(window.location.hash); }, 300); | |
− | |||
− | if ( | ||
− | |||
− | |||
− | |||
} | } | ||
+ | |||
+ | // Trata clicks em links âncora | ||
+ | $(document).on('click', 'a[href^="#"]', function(e) { | ||
+ | e.preventDefault(); | ||
+ | var hash = $(this).attr('href'); | ||
+ | history.pushState(null, null, hash); | ||
+ | expandSectionForAnchor(hash); | ||
+ | }); | ||
}); | }); |
Revision as of 13:07, 30 April 2025
/* Any JavaScript here will be loaded for all users on every page load. */ $(document).ready(function() { // Função para copiar texto e mostrar feedback function handleCopy(element) { var textToCopy = $(element).attr('data-copy'); // Cria elemento temporário para copiar var tempInput = document.createElement('textarea'); tempInput.value = textToCopy; document.body.appendChild(tempInput); tempInput.select(); try { document.execCommand('copy'); // Adiciona classe de feedback $(element).addClass('copied'); // Remove feedback após 1.5 segundos setTimeout(function() { $(element).removeClass('copied'); }, 1500); } catch (err) { console.error('Erro ao copiar:', err); } document.body.removeChild(tempInput); } // Click nos elementos .warp-copy $(document).on('click', '.warp-copy', function() { handleCopy(this); }); // Click nas imagens dos NPCs function initNpcCopy() { $('.contents-equipment .tile-top.tile-image a').off('click.npccopy').on('click.npccopy', function(e) { e.preventDefault(); var npcId = $(this).attr('href').replace('#',''); handleCopy($(this).closest('.contents-equipment').find('.warp-copy')[0]); }); } // Inicializa quando o documento está pronto initNpcCopy(); // Reinicia quando novo conteúdo é carregado if (typeof mw !== 'undefined' && mw.hook) { mw.hook('wikipage.content').add(function() { initNpcCopy(); }); } // Auto-expandir seções com anchor links function expandSectionForAnchor(hash) { var targetElement = $(hash); if (targetElement.length) { targetElement.parents('.mw-collapsible.mw-collapsed').removeClass('mw-collapsed') .find('.mw-collapsible-content').show(); setTimeout(function() { $('html, body').animate({ scrollTop: targetElement.offset().top - 100 }, 200); }, 400); } } // Trata hash na URL inicial if (window.location.hash) { setTimeout(function() { expandSectionForAnchor(window.location.hash); }, 300); } // Trata clicks em links âncora $(document).on('click', 'a[href^="#"]', function(e) { e.preventDefault(); var hash = $(this).attr('href'); history.pushState(null, null, hash); expandSectionForAnchor(hash); }); });