-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcformat.js
205 lines (171 loc) · 6.52 KB
/
mcformat.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*! mcformat v1.3.0 | MIT License | github.com/yusshu/mcformat */
const MCFormat = function ({ colorChar = '&', allowMagic = true }) {
if (allowMagic) {
/*
* Object binding char width to the characters
* that have that width.
*/
const charsByLength = {
100: '¸',
200: 'i.,:;!¡|',
300: 'íl·•\'',
400: 'Iît[] ¤¸',
500: 'fk*(){}"¢><',
600: 'AÄÅÆBCÇDEFGHJKLMNÑOØPQRSTUÜVWXYZaáâàåæbcçdeêëghjmnñoóôöòøpqrsuúûüùvwxyÿzΩ0123456789?¿#/\\-—_«»$ƒ£+÷=±¬∑%♀&§^⌂',
700: '~@¶®',
800: '≈∞♂♠♣♥♦',
900: '√☺☻☼'
};
/*
* Similar to 'charsByLength', but reversed, it
* binds the characters to their length
*/
const lengthByChars = {};
// copy information from charsByLength to lengthByCHars
for (const length in charsByLength) {
const chars = charsByLength[length];
for (const c of chars) {
lengthByChars[c] = length;
}
}
/**
* Obfuscates the text elements inside the
* given node
* @param {Node} node
*/
function obfuscate(node) {
for (const child of node.childNodes) {
if (child.nodeType === Node.TEXT_NODE) {
const content = child.textContent;
let newContent = [];
for (let i = 0; i < content.length; i++) {
const char = content.charAt(i);
const length = lengthByChars[char] || 400;
const chars = charsByLength[length];
newContent.push(chars.charAt(Math.floor(Math.random() * chars.length)));
}
child.textContent = newContent.join('');
} else if (child.nodeType === Node.ELEMENT_NODE) {
obfuscate(child);
}
}
}
// finally set the interval that updates all the "magic" elements
setInterval(() => {
for (const element of document.getElementsByClassName("mc-magic")) {
obfuscate(element);
}
}, 50);
}
/**
* Object containing all color characters
* and their format
*/
const COLORS = {
'0': { name: 'black' },
'1': { name: 'dark-blue' },
'2': { name: 'dark-green' },
'3': { name: 'dark-aqua' },
'4': { name: 'dark-red' },
'5': { name: 'dark-purple' },
'6': { name: 'gold' },
'7': { name: 'gray' },
'8': { name: 'dark-gray' },
'9': { name: 'blue' },
'a': { name: 'green' },
'b': { name: 'aqua' },
'c': { name: 'red' },
'd': { name: 'light-purple' },
'e': { name: 'yellow' },
'f': { name: 'white' },
'r': { name: 'reset' },
// formats (they don't override previous element format)
'm': { name: 'strikethrough', pass: true },
'o': { name: 'italic', pass: true },
'l': { name: 'bold', pass: true },
'n': { name: 'underline', pass: true },
'k': { name: 'magic', pass: true },
};
const HEX_CHARS = "0123456789abcdef";
this.format = function (input) {
/** The root element, will contain all the other elements */
const root = document.createElement("span");
root.classList.add("mc-root"); // add class
let content = [];
let next = undefined; // the next element to append to root
let active = root; // the last used element
for (let i = 0; i < input.length; i++) {
const current = input.charAt(i);
if (current !== colorChar) {
// not a color char, just push it
content.push(current);
continue;
}
const nextIndex = i + 1;
if (nextIndex >= input.length) {
// last character, just push it
content.push(current);
break;
}
const code = input.charAt(nextIndex);
const format = COLORS[code];
if (format) {
const element = document.createElement("span");
element.classList.add(`mc-${format.name}`);
active.innerText = content.join(''); // set the content to the previous element
content = []; // clear the content
if (format.pass) {
active.appendChild(element);
} else {
if (next !== undefined && next !== root) {
root.appendChild(next);
}
next = element;
}
active = element;
i++;
} else if (code === "#"
&& (nextIndex + 6 < input.length)) {
const color = ['#'];
let found = true;
// check for hex colors
for (let j = 0; j < 6; j++) {
const index = nextIndex + j + 1;
const char = input.charAt(index).toLowerCase();
const value = HEX_CHARS.indexOf(char);
if (value === -1) {
found = false;
break;
}
// push the color
color.push(char);
}
if (found) {
const element = document.createElement("span");
element.style.color = color.join('');
element.classList.add('mc-hex');
active.innerText = content.join(''); // set the content to the previous element
content = []; // clear the content
if (next !== undefined && next !== root) {
root.appendChild(next);
}
next = element;
active = element;
// process next
i = nextIndex + 6;
}
} else {
// not a code, push
content.push(current);
}
}
// check for remaining content
if (next !== undefined && next !== root) {
root.appendChild(next);
}
if (content.length > 0) {
active.innerText += content.join('');
}
return root;
}
};