MediaWiki:Common.js: mudanças entre as edições

De Grimopedia
Ir para navegação Ir para pesquisar
Sem resumo de edição
Sem resumo de edição
Etiqueta: Reversão manual
 
(6 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
(Sem diferença)

Edição atual tal como às 15h27min de 15 de julho de 2026

// Navi clicável: {{Navi|mapa|x|y}} -> copia /navi mapa x y
mw.loader.using('mediawiki.util').then(function () {
    document.addEventListener('click', function (e) {
        var el = e.target.closest('.navi-copy');
        if (!el) return;

        var map = el.getAttribute('data-map');
        var x   = el.getAttribute('data-x');
        var y   = el.getAttribute('data-y');

        var text = '/navi ' + map + ' ' + x + ' ' + y;

        if (navigator.clipboard && navigator.clipboard.writeText) {
            navigator.clipboard.writeText(text).then(function () {
                mw.notify('Comando copiado: ' + text);
            }, function () {
                alert('Comando: ' + text);
            });
        } else {
            var temp = document.createElement('textarea');
            temp.value = text;
            document.body.appendChild(temp);
            temp.select();
            document.execCommand('copy');
            document.body.removeChild(temp);
            mw.notify('Comando copiado: ' + text);
        }
    });
});

// ------------------------------------------------------------
// Ícone automático para links do ItemDB do site
// - Procura links que contenham /information/itemdb/{id}
// - Injeta um ícone (24x24) antes do texto do link
// - Usa imagens locais do site: /img/item/{id}.png
//   (se não existir, cai no unknown.png)
// ------------------------------------------------------------
mw.loader.using('mediawiki.util').then(function () {

    function applyItemIcons(root) {
        root = root || document;

        // Pega todos os links para o ItemDB do site
        var links = root.querySelectorAll('a[href*="/information/itemdb/"]:not(.aur-item-link)');

        links.forEach(function (a) {
            // evita duplicar
            if (a.getAttribute('data-aur-noicon') === '1') return;

            if (a.dataset && a.dataset.aurItemIconApplied) return;

            // tenta extrair o ID do item
            var m = a.href.match(/\/information\/itemdb\/(\d+)/);
            if (!m) return;

            var id = m[1];

            // marca como aplicado
            if (a.dataset) a.dataset.aurItemIconApplied = "1";

            // NÃO força layout aqui — deixa o Common.css controlar o estilo do "pill"
            // Só garantimos que o ícone exista antes do texto.
            var icon = document.createElement("span");
            icon.style.width = "24px";
            icon.style.height = "24px";
            icon.style.flex = "0 0 24px";
            icon.style.display = "inline-block";
            icon.style.backgroundSize = "contain";
            icon.style.backgroundRepeat = "no-repeat";
            icon.style.borderRadius = "6px";
            icon.style.backgroundColor = "rgba(0,0,0,.04)";

            // tenta imagem real do item
            var url = "https://grimopedia.com.br/img/item/" + id + ".png";
            var fallback = "https://grimopedia.com.br/img/item/unknown.png";

            icon.style.backgroundImage = "url('" + url + "')";

            // fallback se a imagem não existir
            var test = new Image();
            test.onload = function () {};
            test.onerror = function () {
                icon.style.backgroundImage = "url('" + fallback + "')";
            };
            test.src = url;

            // injeta antes do texto do link
            a.prepend(icon);
        });
    }

    // aplica no carregamento
    document.addEventListener("DOMContentLoaded", function () {
        applyItemIcons(document);
    });

    // aplica também quando conteúdo é carregado via ajax/VE
    if (mw && mw.hook) {
        mw.hook('wikipage.content').add(function ($content) {
            applyItemIcons($content[0] || document);
        });
    }
});