Difference between revisions of "MediaWiki:Common.js"
Jump to navigation
Jump to search
Line 11: | Line 11: | ||
document.execCommand('copy'); | document.execCommand('copy'); | ||
document.body.removeChild(tempInput); | document.body.removeChild(tempInput); | ||
+ | |||
+ | // Adiciona a classe 'copied' para mostrar o tooltip "Copied!" | ||
+ | $(this).addClass('copied'); | ||
var originalColor = $(this).css('color'); | var originalColor = $(this).css('color'); | ||
Line 17: | Line 20: | ||
setTimeout(function() { | setTimeout(function() { | ||
$(self).css('color', originalColor); | $(self).css('color', originalColor); | ||
+ | // Remove a classe 'copied' após o timeout | ||
+ | $(self).removeClass('copied'); | ||
}, 500); | }, 500); | ||
}); | }); |
Revision as of 11:53, 30 April 2025
/* Any JavaScript here will be loaded for all users on every page load. */ $(document).ready(function() { $(document).on('click', '.warp-copy', function() { var textToCopy = $(this).attr('data-copy'); var tempInput = document.createElement('textarea'); tempInput.value = textToCopy; document.body.appendChild(tempInput); tempInput.select(); document.execCommand('copy'); document.body.removeChild(tempInput); // Adiciona a classe 'copied' para mostrar o tooltip "Copied!" $(this).addClass('copied'); var originalColor = $(this).css('color'); $(this).css('color', 'green'); var self = this; setTimeout(function() { $(self).css('color', originalColor); // Remove a classe 'copied' após o timeout $(self).removeClass('copied'); }, 500); }); }); /* Auto-expand sections when clicking anchored links */ $(document).ready(function() { console.log("Inicializando script para expandir seções com links âncora"); // Handle initial page load with hash if (window.location.hash) { console.log("Página carregada com hash: " + window.location.hash); // Pequeno delay para garantir que o DOM esteja completamente carregado setTimeout(function() { expandSectionForAnchor(window.location.hash); }, 300); } // Handle clicks on anchor links within the page $(document).on('click', 'a[href^="#"]', function(event) { var hash = $(this).attr('href'); console.log("Clique em link âncora: " + hash); // Previne o comportamento padrão para tratar manualmente event.preventDefault(); // Atualiza a URL sem recarregar a página if (history.pushState) { history.pushState(null, null, hash); } else { location.hash = hash; } expandSectionForAnchor(hash); }); // Function to expand section containing an anchor function expandSectionForAnchor(hash) { console.log("Procurando e expandindo seção para âncora: " + hash); // Try to find the anchor element var targetElement = $(hash); if (targetElement.length) { console.log("Elemento alvo encontrado"); // Encontrar todas as seções colapsáveis que contêm o elemento // Primeiro os pais diretos var collapsibleSections = targetElement.parents('.mw-collapsible.mw-collapsed'); // Depois verifica se o próprio elemento está em uma seção colapsável var directCollapsible = targetElement.closest('.mw-collapsible.mw-collapsed'); if (directCollapsible.length) { collapsibleSections = collapsibleSections.add(directCollapsible); } console.log("Seções colapsáveis encontradas: " + collapsibleSections.length); // Expandir cada seção encontrada if (collapsibleSections.length > 0) { collapsibleSections.each(function() { var section = $(this); console.log("Expandindo seção colapsável"); // Remover a classe collapsed section.removeClass('mw-collapsed'); // Tentar clicar no botão de expansão var toggleButton = section.find('.mw-collapsible-toggle').first(); if (toggleButton.length) { console.log("Clicando no botão de expansão"); toggleButton.click(); } // Para tabelas, garantir que as linhas sejam mostradas if (section.hasClass('wikitable')) { console.log("Expandindo tabela wikitable"); section.find('tr:not(:first-child)').show(); } // Forçar exibição do conteúdo section.find('.mw-collapsible-content').show(); }); // Aguardar a expansão antes de rolar setTimeout(function() { scrollToTarget(targetElement); }, 400); } else { // Se não houver seções colapsáveis, apenas role até o elemento scrollToTarget(targetElement); } } else { console.log("Elemento alvo não encontrado para hash: " + hash); } } // Função auxiliar para rolar até o elemento alvo function scrollToTarget(element) { console.log("Rolando até o elemento alvo"); $('html, body').animate({ scrollTop: element.offset().top - 100 }, 200); } // Adicionar hook para quando o conteúdo da wiki for atualizado if (typeof mw !== 'undefined' && mw.hook) { mw.hook('wikipage.content').add(function() { console.log("Conteúdo da wiki atualizado"); if (window.location.hash) { setTimeout(function() { expandSectionForAnchor(window.location.hash); }, 300); } }); } }); /* Script para adicionar funcionalidade de cópia às imagens dos NPCs */ $(function() { // Espera o carregamento completo do conteúdo da wiki function initNpcCopyLinks() { console.log("Inicializando links de cópia para NPCs"); // Identifica os links dentro dos containers de imagem $('.contents-equipment .tile-top.tile-image a').each(function() { var $link = $(this); var linkHref = $link.attr('href') || ''; // Extrai o ID do NPC do link (remove o # do início) var npcId = linkHref.startsWith('#') ? linkHref.substring(1) : linkHref; if (npcId) { // Remove handlers anteriores para evitar duplicação $link.off('mousedown.npccopy'); // Adiciona um handler de clique que irá copiar o texto $link.on('mousedown.npccopy', function(e) { // Texto que será copiado - usa o ID específico do NPC var textToCopy = "@warp " + npcId; // Cria um elemento temporário para copiar o texto var tempInput = document.createElement('textarea'); tempInput.value = textToCopy; document.body.appendChild(tempInput); tempInput.select(); try { // Executa o comando de cópia document.execCommand('copy'); // Mostrar feedback visual temporário $('<div class="copy-notification" style="position:fixed;bottom:20px;right:20px;background:#4CAF50;color:white;padding:10px;border-radius:5px;z-index:9999;">Copiado: ' + textToCopy + '</div>') .appendTo('body') .delay(1500) .fadeOut(300, function() { $(this).remove(); }); } catch (err) { console.error('Erro ao copiar texto: ', err); } // Remove o elemento temporário document.body.removeChild(tempInput); // Permite que o evento continue normalmente return true; }); } }); } // Inicializa na carga da página initNpcCopyLinks(); // Também inicializa quando o conteúdo da wiki é atualizado if (typeof mw !== 'undefined' && mw.hook) { mw.hook('wikipage.content').add(function() { initNpcCopyLinks(); }); } });