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}}
Notes
- The javascript embedded by this widget has been minified. The full un-minified code is provided below:
// Note on ES6: Although usually "var" is fine to use in wiki javascript - as most instances of javascript on the wiki have a large single invocation, when a function is being called multiple times on the same page, due to var being "function scoped", variables can leak across iterations if the previous one isn't finished running! This is particularly evident with small optimised functions called dozens of times such as in [[Widget:Game link]]. We really do need to use "const" (one off definitions, object contents can however be modified) and "let" (safe loop variables) here, which are "block scoped".
(function (elementId) {
const link = document.getElementById(elementId);
if (!link) {
return;
}
function encodeChatLink2(typeRaw, idRaw) {
const linkTypes = {
item: 2,
text: 3,
map: 4,
skill: 6,
trait: 7,
recipe: 9,
skin: 10,
outfit: 11
};
if (!typeRaw) {
return 'invalid type (blank)';
}
const type = linkTypes[typeRaw.trim().toLowerCase()] || 0;
if (!type) {
return 'invalid type';
}
let id = +idRaw;
const 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
let binary = '';
for (let 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) + ']';
}
const chatLink = encodeChatLink2(link.getAttribute('data-type'), link.getAttribute('data-id'));
const 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 () {
link.style.visibility = 'hidden';
input.style.display = 'inline-block';
input.focus();
input.select();
document.execCommand('Copy');
}, false);
input.addEventListener('blur', function () {
input.style.display = null;
link.style.visibility = null;
}, false);
})('gamelink-<!--{$GameLink_counter}-->');