crafty-4/app/frontend/static/assets/js/motd.js

104 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-08-28 22:48:30 +00:00
var obfuscators = [];
var styleMap = {
'§0': 'color:#000000',
'§1': 'color:#0000AA',
'§2': 'color:#00AA00',
'§3': 'color:#00AAAA',
'§4': 'color:#AA0000',
'§5': 'color:#AA00AA',
'§6': 'color:#FFAA00',
'§7': 'color:#AAAAAA',
'§8': 'color:#555555',
'§9': 'color:#5555FF',
'§a': 'color:#55FF55',
'§b': 'color:#55FFFF',
'§c': 'color:#FF5555',
'§d': 'color:#FF55FF',
'§e': 'color:#FFFF55',
2022-10-04 17:31:37 +00:00
'§f': 'color:var(--white)',
2021-08-28 22:48:30 +00:00
'§l': 'font-weight:bold',
'§m': 'text-decoration:line-through',
'§n': 'text-decoration:underline',
'§o': 'font-style:italic',
};
function obfuscate(string, elem) {
2022-06-04 00:13:00 +00:00
var magicSpan;
if (string.indexOf('<br>') > -1) {
2021-08-28 22:48:30 +00:00
elem.innerHTML = string;
2022-06-04 00:13:00 +00:00
elem.childNodes.array.forEach(currNode => {
if (currNode.nodeType === 3) {
2021-08-28 22:48:30 +00:00
magicSpan = document.createElement('span');
magicSpan.innerHTML = currNode.nodeValue;
elem.replaceChild(magicSpan, currNode);
init(magicSpan);
}
2022-06-04 00:13:00 +00:00
});
2021-08-28 22:48:30 +00:00
} else {
init(elem, string);
}
function init(el, str) {
var i = 0,
obsStr = str || el.innerHTML,
len = obsStr.length;
2022-06-04 00:13:00 +00:00
obfuscators.push(window.setInterval(function () {
if (i >= len) i = 0;
2021-08-28 22:48:30 +00:00
obsStr = replaceRand(obsStr, i);
el.innerHTML = obsStr;
i++;
2022-06-04 00:13:00 +00:00
}, 0));
2021-08-28 22:48:30 +00:00
}
function randInt(min, max) {
2022-06-04 00:13:00 +00:00
return Math.floor(Math.random() * (max - min + 1)) + min;
}
2021-08-28 22:48:30 +00:00
function replaceRand(string, i) {
2022-06-04 00:13:00 +00:00
var randChar = String.fromCharCode(randInt(64, 95));
2021-08-28 22:48:30 +00:00
return string.substr(0, i) + randChar + string.substr(i + 1, string.length);
}
}
function applyCode(string, codes) {
var elem = document.createElement('span'),
obfuscated = false;
2022-06-04 00:13:00 +00:00
codes.forEach(code => {
elem.style.cssText += styleMap[code] + ';';
if (code === '§k') {
2021-08-28 22:48:30 +00:00
obfuscate(string, elem);
obfuscated = true;
}
2022-06-04 00:13:00 +00:00
});
if (!obfuscated) elem.innerHTML = string;
2021-08-28 22:48:30 +00:00
return elem;
}
function parseStyle(string) {
2022-06-04 00:13:00 +00:00
var final = document.createDocumentFragment();
console.log("STRING : " + string)
2021-08-28 22:48:30 +00:00
string = string.replace(/\n|\\n/g, '<br>');
2022-06-04 00:13:00 +00:00
string = string.split('§r');
string.forEach(item => {
var apply = [];
if (item.length > 0) {
apply = item.match(/§.{1}/g) || [];
apply.forEach(code => {
item = item.replace(code, '')
});
final.appendChild(applyCode(item, apply));
2021-08-28 22:48:30 +00:00
}
2022-06-04 00:13:00 +00:00
});
2021-08-28 22:48:30 +00:00
return final;
}
function clearObfuscators() {
2022-06-04 00:20:45 +00:00
obfuscators.slice().reverse().forEach(item => {
clearInterval(item);
});
2021-08-28 22:48:30 +00:00
obfuscators = [];
}
function initParser(input, output) {
clearObfuscators();
var input = document.getElementById(input),
2021-08-28 22:51:34 +00:00
output = document.getElementById(output);
if (input != null && output != null) {
2024-01-31 22:04:22 +00:00
var parsed = parseStyle(input.innerText);
2021-08-28 22:51:34 +00:00
output.innerHTML = '';
output.appendChild(parsed);
}
2021-08-28 22:48:30 +00:00
}