Widget:Game link

From Guild Wars 2 Wiki
Jump to navigationJump to search

Description

This widget takes a link type and a game ID to generate a chat link. The widget is used by Template:Game link.

Parameters

type
The link type. Possible values: item, map, skill, trait, recipe, skin, outfit, text
id
The game id.

Examples

:{{skill icon|Drop Antidote}}: {{#Widget:Game link|type=skill|id=6000}}
Throw Antidote.png Drop Antidote:

Notes

  • The javascript embedded by this widget has been minified. The full un-minified code is provided below:
(function (elementId) {
    var link = document.getElementById(elementId);
    if (!link) {
        return;
    }
    
    function encodeChatLink2(type, id) {
        if (!type) {
            return 'invalid type';
        }
        
        var linkTypes = { item: 2, text: 3, map: 4, skill: 6, trait: 7, recipe: 9, skin: 10, outfit: 11 };
        type = linkTypes[type.trim().toLowerCase()] || 0;
        if (!type) {
            return 'invalid type';
        }

        var data = [];
        while (id > 0) {
            data.push(id & 255);
            id = id >> 8;
        }
        while (data.length < 4 || data.length % 2 != 0) {
            data.push(0);
        }

        if (type == 2) {
            data.unshift(1);
        }
        data.unshift(type);

        // Encode data
        var binary = '';
        for (var i = 0; i < data.length; i++) {
            binary += String.fromCharCode(data[i]);
        }
        
        // Note "btoa" function isn't supported in IE, however Mediawiki removes
        //  all javascript in IE anyway, so no polyfill required.
        return '[&' + btoa(binary) + ']';
    }
    
    var chatLink = encodeChatLink2(link.getAttribute('data-type'), +link.getAttribute('data-id'));
    var input = document.createElement('input');
    input.className = 'chatlink';
    input.type = 'text';
    input.value = chatLink;
    input.readOnly = true;
    input.spellcheck = false;

    link.appendChild(document.createTextNode(chatLink));
    link.parentNode.insertBefore(input, link);

    link.addEventListener('click', function () {
        this.style.visibility = 'hidden';
        input.style.display = 'inline-block';
        input.focus();
        input.select();
        document.execCommand('Copy');
    }, false);
    input.addEventListener('blur', function () {
        this.style.display = null;
        link.style.visibility = null;
    }, false);
})('gamelink-<!--{$GameLink_counter}-->');