Utilisateur:Alu/chat link search.js

De Guild Wars 2 Wiki
Aller à la navigation Aller à la recherche

Note : après avoir publié vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou appuyez sur Ctrl + F5 ou Ctrl + R (⌘ + R sur un Mac).
  • Google Chrome : appuyez sur Ctrl + Maj + R (⌘ + Shift + R sur un Mac).
  • Internet Explorer / Edge : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl + F5.
  • Opera : appuyez sur Ctrl + F5.
/**
 * GW2W Chat link search
 *
 * Decodes Guild Wars 2 chat links in the search panel, and tries to find the
 * corresponding article using the SMW property `ID en jeu`.
 *
 * by Patrick Westerhoff [User:Poke]
 */
(function chatLinkSearch (window, document, $) {
    var searchBar = document.getElementById('powerSearchText') || document.getElementById('searchText');
    if (!searchBar || !window.atob) {
        return;
    }
    var mwApi;

    function smwAskArticle (type, id, callback) {
        var apiData = { action: 'ask', query: '?Nom|limit=1|[[ID en jeu::' + id + ']]' };
        if (type == 'item') {
            apiData.query += '[[Rareté::+]]';
        }
        else if (type == 'map') {
            apiData.query += '';
        }
        else if (type == 'skill') {
            apiData.query += '';
        }
        else if (type == 'recipe') {
            apiData.query = '?Nom|limit=1';
        }
        mwApi.get(apiData)
        .done(function (data) {
            if (data.query.results.length === 0) {
                callback(null);
            }
            else {
                for (var title in data.query.results) {
                    callback(title, data.query.results[title].printouts['Nom'][0]);
                    return;
                }
            }
        })
        .fail(function (data) {
            callback(null);
        });
    }

    function decodeChatLink (code) {
        var binary = window.atob(code);
        var octets = new Array(binary.length);
        for (var i = 0; i < binary.length; i++) {
            octets[i] = binary.charCodeAt(i);
        }
        return octets;
    }

    function display (code, listItem) {
        var data = decodeChatLink(code);
        var id = data[2] << 8 | data[1];
        var type;
        if (data[0] == 2) {
            type = 'item';
            id = data[3] << 8 | data[2];
        }
        else if (data[0] == 4) {
            type = 'map';
        }
        else if (data[0] == 7) {
            type = 'skill';
        }
        else if (data[0] == 10) {
            type = 'recipe';
        }
        else {
            var span = document.createElement('span');
            span.innerHTML = 'This type of chat links is currently not supported.';
            $(span).fadeIn(1000).appendTo(listItem);
            return;
        }
        smwAskArticle(type, id, function (title, canonicalName) {
            var span = document.createElement('span');
            if (title) {
                var link = document.createElement('a');
                link.href = '/wiki/' + title.replace(' ', '_');
                link.title = title;
                link.innerHTML = canonicalName || title;
                span.appendChild(link);
                if (type != 'map') {
                    span.appendChild(document.createTextNode(' (' + type + ')'));
                }
            }
            else {
                var msg = 'Il n\'y a pour le moment aucun article avec cet ID (' + id + ').';
//                var msg = 'There is no article linked with this ID (' + id + ') yet.';
                if (type == 'item' || type == 'skill' || type == 'recipe') {
                    msg += ' If you know what ' + type + ' this chat link links to, please add the ID to the article or create it if it does not exist yet.';
                }
                else if (type == 'map') {
                    msg += ' Nous n\'avons pour le moment pas d\'article pour tous les emplacements de la carte.';
//                    msg += ' We currently don’t have articles for every location on the map; we do however have a list of <a href="/wiki/Chat_link_format/0x04_codes" title="Chat link format/0x04 codes">all location chat links</a>.';
                }
                span.innerHTML = msg;
            }
            $(span).fadeIn(1000).appendTo(listItem);
        });
    }

    function init () {
        // find chat links
        var ul = document.createElement('ul');
        var expr = /\[&([A-Za-z0-9+/]+=*)\]/g;
        var match;
        while ((match = expr.exec(searchBar.value))) {
            var li = document.createElement('li');
            li.innerHTML = '<tt>' + match[0] + '</tt>';
            ul.appendChild(li);
            display(match[1], li);
        }

        // display results
        if (ul.children.length) {
            var div = document.createElement('div');
            div.className = 'gw2w-chat-link-search';
			div.innerHTML = 'Les <a href="/wiki/Chat_link" title="Chat link">chat links</a> suivant étaient dans votre recherche :';
            div.appendChild(ul);

            var topTable = document.getElementById('mw-search-top-table');
            $(div).hide().insertAfter(topTable).show('fast');
        }
    }

    window.mw.loader.using('mediawiki.api', function() {
        mwApi = new window.mw.Api();
        init();
    });
})(window, document, $);