-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMinecraftColorCodes.3.7.js
More file actions
130 lines (127 loc) · 4.54 KB
/
MinecraftColorCodes.3.7.js
File metadata and controls
130 lines (127 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var obfuscators = [];
var styleMap = {
'§4': 'font-weight:normal;text-decoration:none;color:#be0000',
'§c': 'font-weight:normal;text-decoration:none;color:#fe3f3f',
'§6': 'font-weight:normal;text-decoration:none;color:#d9a334',
'§e': 'font-weight:normal;text-decoration:none;color:#fefe3f',
'§2': 'font-weight:normal;text-decoration:none;color:#00be00',
'§a': 'font-weight:normal;text-decoration:none;color:#3ffe3f',
'§b': 'font-weight:normal;text-decoration:none;color:#3ffefe',
'§3': 'font-weight:normal;text-decoration:none;color:#00bebe',
'§1': 'font-weight:normal;text-decoration:none;color:#0000be',
'§9': 'font-weight:normal;text-decoration:none;color:#3f3ffe',
'§d': 'font-weight:normal;text-decoration:none;color:#fe3ffe',
'§5': 'font-weight:normal;text-decoration:none;color:#be00be',
'§f': 'font-weight:normal;text-decoration:none;color:#ffffff',
'§7': 'font-weight:normal;text-decoration:none;color:#bebebe',
'§8': 'font-weight:normal;text-decoration:none;color:#3f3f3f',
'§0': 'font-weight:normal;text-decoration:none;color:#000000',
'§l': 'font-weight:bold',
'§n': 'text-decoration:underline;text-decoration-skip:spaces',
'§o': 'font-style:italic',
'§m': 'text-decoration:line-through;text-decoration-skip:spaces',
};
function obfuscate(string, elem) {
var magicSpan,
currNode,
len = elem.childNodes.length;
if(string.indexOf('<br>') > -1) {
elem.innerHTML = string;
for(var j = 0; j < len; j++) {
currNode = elem.childNodes[j];
if(currNode.nodeType === 3) {
magicSpan = document.createElement('span');
magicSpan.innerHTML = currNode.nodeValue;
elem.replaceChild(magicSpan, currNode);
init(magicSpan);
}
}
} else {
init(elem, string);
}
function init(el, str) {
var i = 0,
obsStr = str || el.innerHTML,
len = obsStr.length;
obfuscators.push( window.setInterval(function () {
if(i >= len) i = 0;
obsStr = replaceRand(obsStr, i);
el.innerHTML = obsStr;
i++;
}, 0) );
}
function randInt(min, max) {
return Math.floor( Math.random() * (max - min + 1) ) + min;
}
function replaceRand(string, i) {
var randChar = String.fromCharCode( randInt(64,90) ); /*Numbers: 48-57 Al:64-90*/
return string.substr(0, i) + randChar + string.substr(i + 1, string.length);
}
}
function applyCode(string, codes) {
var len = codes.length;
var elem = document.createElement('span'),
obfuscated = false;
for(var i = 0; i < len; i++) {
elem.style.cssText += styleMap[codes[i]] + ';';
if(codes[i] === '§k') {
obfuscate(string, elem);
obfuscated = true;
}
}
if(!obfuscated) elem.innerHTML = string;
return elem;
}
function parseStyle(string) {
var codes = string.match(/§.{1}/g) || [],
indexes = [],
apply = [],
tmpStr,
indexDelta,
noCode,
final = document.createDocumentFragment(),
len = codes.length,
string = string.replace(/\n|\\n/g, '<br>');
for(var i = 0; i < len; i++) {
indexes.push( string.indexOf(codes[i]) );
string = string.replace(codes[i], '\x00\x00');
}
if(indexes[0] !== 0) {
final.appendChild( applyCode( string.substring(0, indexes[0]), [] ) );
}
for(var i = 0; i < len; i++) {
indexDelta = indexes[i + 1] - indexes[i];
if(indexDelta === 2) {
while(indexDelta === 2) {
apply.push ( codes[i] );
i++;
indexDelta = indexes[i + 1] - indexes[i];
}
apply.push ( codes[i] );
} else {
apply.push( codes[i] );
}
if( apply.lastIndexOf('§r') > -1) {
apply = apply.slice( apply.lastIndexOf('§r') + 1 );
}
tmpStr = string.substring( indexes[i], indexes[i + 1] );
final.appendChild( applyCode(tmpStr, apply) );
}
return final;
}
function clearObfuscators() {
var i = obfuscators.length;
for(;i--;) {
clearInterval(obfuscators[i]);
}
obfuscators = [];
}
String.prototype.replaceColorCodes = function() {
clearObfuscators();
var outputString = parseStyle(String(this));
return outputString;
};
/////////////////////////////////////////////////
function cutString(str, cutStart, cutEnd){
return str.substr(0,cutStart) + str.substr(cutEnd+1);
}