-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
40 lines (31 loc) · 1.03 KB
/
main.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
// Poor man's minifier
function minify(src, vars) {
return src
.replace(/\s+\/\/.+$/gm, '') // Trim line comments
.replace(/[\s\n]+(?!\w)/gm, '') // Trim all whitespace that are not followed by word character
.replace(/(\W)[\s\n]+/gm, '$1') // Trim all whitespace that follow a non-word character
.replace(/(\w+)/gm, (s, name) => vars[name] || name); // Shorten variables
}
onload = () => {
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
canvas.focus();
canvas.onkeydown = (e) => e.preventDefault();
const src = rawSrc.innerHTML;
const vars = eval(`(${rawVars.innerHTML})`);
const min = minify(src, vars);
bytes.innerHTML = min.length;
minified.innerHTML = new Option(min).innerHTML;
source.innerHTML = new Option(src).innerHTML;
//Prism.highlightElement(minified);
Prism.highlightElement(source);
const a = canvas;
const b = body;
const c = canvas.getContext('2d');
const d = document;
try {
eval(min);
} catch (e) {
error.innerHTML = e.message;
}
};