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
Linha 1: Linha 1:
// /navi clicável para spans gerados por {{Navi|map|x|y}}
// Navi clicável - baseado na estrutura do bROWiki
mw.loader.using('mediawiki.util').then(function () {
mw.loader.using('mediawiki.util').then(function () {
    document.addEventListener('click', function (e) {
        var el = e.target.closest('.navi-copy');
        if (!el) return;


         // pega dados do span
    function copiarTexto(text) {
         var map = el.getAttribute('data-map');
        if (navigator.clipboard && navigator.clipboard.writeText) {
        var x  = el.getAttribute('data-x');
            return navigator.clipboard.writeText(text);
         var = el.getAttribute('data-y');
        }
         // fallback antigo
         return new Promise(function (resolve, reject) {
            try {
                var temp = document.createElement('textarea');
                temp.value = text;
                document.body.appendChild(temp);
                temp.select();
                document.execCommand('copy');
                document.body.removeChild(temp);
                resolve();
            } catch (e) {
                reject(e);
            }
        });
    }
 
    function montarNavi(div) {
         var spans = div.getElementsByTagName('span');
        if (spans.length < 3) return;


         var text = '/navi ' + map + ' ' + x + ' ' + y;
         var mapa = spans[0].textContent.trim();
        var x   = spans[1].textContent.trim();
        var y   = spans[2].textContent.trim();


         // tentativa moderna
         // Limpa o conteúdo original
         if (navigator.clipboard && navigator.clipboard.writeText) {
         while (div.firstChild) {
             navigator.clipboard.writeText(text).then(function () {
             div.removeChild(div.firstChild);
                 mw.notify('Comando copiado: ' + text);
        }
             }, function () {
 
                 alert('Comando: ' + text);
        // Texto clicável
        var texto = document.createElement('div');
        texto.className = 'navi-copy-text';
        texto.textContent = '(' + mapa + ' ' + x + ', ' + y + ')';
 
        // Mensagem "copiado"
        var aviso = document.createElement('div');
        aviso.className = 'navi-copied';
        aviso.textContent = 'Comando copiado!';
        aviso.style.display = 'none';
 
        div.appendChild(texto);
        div.appendChild(aviso);
 
        texto.addEventListener('click', function () {
            var cmd = '/navi ' + mapa + ' ' + x + ' ' + y;
 
            copiarTexto(cmd).then(function () {
                 aviso.style.display = 'inline';
                setTimeout(function () {
                    aviso.style.display = 'none';
                }, 1500);
 
                if (mw && mw.notify) {
                    mw.notify('Comando copiado: ' + cmd);
                }
             }).catch(function () {
                 alert('Comando: ' + cmd);
             });
             });
         } else {
         });
            // fallback antigo
    }
            var temp = document.createElement('textarea');
 
            temp.value = text;
    // Aplica em todos os .navi-copy da página
            document.body.appendChild(temp);
    document.querySelectorAll('.navi-copy').forEach(montarNavi);
            temp.select();
            document.execCommand('copy');
            document.body.removeChild(temp);
            mw.notify('Comando copiado: ' + text);
        }
    });
});
});

Edição das 11h41min de 6 de novembro de 2025

// Navi clicável - baseado na estrutura do bROWiki
mw.loader.using('mediawiki.util').then(function () {

    function copiarTexto(text) {
        if (navigator.clipboard && navigator.clipboard.writeText) {
            return navigator.clipboard.writeText(text);
        }
        // fallback antigo
        return new Promise(function (resolve, reject) {
            try {
                var temp = document.createElement('textarea');
                temp.value = text;
                document.body.appendChild(temp);
                temp.select();
                document.execCommand('copy');
                document.body.removeChild(temp);
                resolve();
            } catch (e) {
                reject(e);
            }
        });
    }

    function montarNavi(div) {
        var spans = div.getElementsByTagName('span');
        if (spans.length < 3) return;

        var mapa = spans[0].textContent.trim();
        var x    = spans[1].textContent.trim();
        var y    = spans[2].textContent.trim();

        // Limpa o conteúdo original
        while (div.firstChild) {
            div.removeChild(div.firstChild);
        }

        // Texto clicável
        var texto = document.createElement('div');
        texto.className = 'navi-copy-text';
        texto.textContent = '(' + mapa + ' ' + x + ', ' + y + ')';

        // Mensagem "copiado"
        var aviso = document.createElement('div');
        aviso.className = 'navi-copied';
        aviso.textContent = 'Comando copiado!';
        aviso.style.display = 'none';

        div.appendChild(texto);
        div.appendChild(aviso);

        texto.addEventListener('click', function () {
            var cmd = '/navi ' + mapa + ' ' + x + ' ' + y;

            copiarTexto(cmd).then(function () {
                aviso.style.display = 'inline';
                setTimeout(function () {
                    aviso.style.display = 'none';
                }, 1500);

                if (mw && mw.notify) {
                    mw.notify('Comando copiado: ' + cmd);
                }
            }).catch(function () {
                alert('Comando: ' + cmd);
            });
        });
    }

    // Aplica em todos os .navi-copy da página
    document.querySelectorAll('.navi-copy').forEach(montarNavi);
});