Clicking a users name copies it to the users clipboard and also displays an alert to the user that this has happened.

This commit is contained in:
Keir 2015-12-13 19:12:32 +00:00
parent a1f1667647
commit 50753e30e6

View File

@ -6,14 +6,51 @@ for (var i = 0; i < elements.length; i++)
{
var element = elements.item(i);
// TODO: This is temporary, hopefully we'll have a better solution, some sort of hover tooltip
element.addEventListener("click", (function(element)
{
return function(e)
{
var uuid = element.getAttribute("data-player-uuid");
alert("UUID: " + uuid);
copy(uuid);
alertElement('<b>Copied UUID to clipboard.</b>', element, 1500);
}
})(element));
}
/**
* Attempts to copy the supplied text into the users clipboard.
* If this fails they are presented with a box allowing them to copy and paste the text.
* This will only work on a supported browser and if this action is caused by some sort of player input.
*
* @param text the text to put in the users clipboard
*/
function copy(text)
{
var copyElement = document.createElement('input');
copyElement.setAttribute('type', 'text');
copyElement.setAttribute('value', text);
copyElement = document.body.appendChild(copyElement);
copyElement.select();
try
{
document.execCommand('copy');
}
catch (e)
{
console.log("document.execCommand('copy'); is not supported by this browser.");
prompt('Copy the value below. (CTRL + C, ENTER)', text);
}
finally
{
copyElement.remove();
}
}
function alertElement(html, element, millis)
{
var original = element.innerHTML;
element.innerHTML = html;
setTimeout(function() { element.innerHTML = original; }, millis);
}