-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
145 lines (108 loc) · 5.06 KB
/
script.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
function init() {
document.getElementById("output").value = "Ready!";
}
function copyOutput() {
let copyText = document.getElementById("output");
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
}
function charCount(s) {
return s.length;
}
function calculateCharsCode() {
let codeChars = document.getElementById("codeChars");
let codeCharsP = document.getElementById("codeCharsP");
if (codeCharsP.style.visibility === "hidden") {
codeCharsP.style.visibility = "visible";
}
codeChars.textContent = charCount(document.getElementById("code").value);
}
function displayStats() {
let codeChars = charCount(document.getElementById("code").value);
let golfChars = charCount(document.getElementById("output").value);
let diffChars = codeChars - golfChars;
let percentageReduction = (Number(((codeChars - golfChars) / codeChars) * 100).toFixed(2));
let negativePercentageReduction = - percentageReduction;
document.getElementById("codeChars").textContent = charCount(document.getElementById("code").value);
document.getElementById("golfChars").textContent = golfChars;
document.getElementById("charsDiff").textContent = diffChars;
document.getElementById("percentageReduction").textContent = (negativePercentageReduction < 0 ? "" : "+") + negativePercentageReduction;
document.getElementById("percentageDiff").textContent = 100 - percentageReduction;
document.getElementById("stats").style.visibility = "visible";
document.getElementById("copyButton").style.visibility = "visible";
}
function golfcode() {
const code = document.getElementById("code");
const minifyBefore = document.getElementById("minifyBefore");
const output = document.getElementById("output");
code.value = code.value.replace(/^\s*[\r\n]/gm, ""); // Blank lines
code.value = code.value.replace(/\s*$/gm, ""); // Trailling spaces at end of lines
let codeToGolf;
if (minifyBefore.checked) {
codeToGolf = minifyCode(code);
} else {
codeToGolf = code.value;
}
try {
if (codeToGolf.length % 2 !== 0) {
codeToGolf = codeToGolf.concat(" ");
}
if (codeToGolf.includes("+ ") || codeToGolf.includes(". ")) {
codeToGolf = codeToGolf.replace("+ "," +").replace(". "," .");
if (document.getElementById("errorAlertDiv").childElementCount === 0) {
let errorAlert = document.createElement("p");
let warning = document.createElement("p");
errorAlert.textContent = "Alert: Your code contains \"+ \" or \". \", and this causes a bug which reverses a part of the output. Consider removing this from your input code if the output does not work.";
warning.textContent = "Warning: Since these substrings were found in your code, they all got replaced from \"+ \" to \" +\" and from \". \" to \" .\". If the result code is not working as expected, consider refactoring your code.";
errorAlert.style.cssText = "font-size: 18px;color: red;";
warning.style.cssText = "font-size: 18px;color: orange;";
document.getElementById("errorAlertDiv").appendChild(errorAlert);
document.getElementById("errorAlertDiv").appendChild(warning);
}
} else {
let errorDiv = document.getElementById("errorAlertDiv");
errorDiv.querySelectorAll("*").forEach((n) => n.remove());
}
let buffer = new ArrayBuffer(codeToGolf.length);
let bufferView = new Uint16Array(buffer);
for (let i = 0, strLen = codeToGolf.length; i < strLen - 1; i += 2) {
let c1 = codeToGolf.charCodeAt(i);
let c2 = codeToGolf.charCodeAt(i + 1);
if ((c1 || c2) > 127) {
throw new Error("At least one of your code char is invalid.\nAll your chars in your code must be in the ASCII table.");
}
bufferView[i / 2] = c2 << 8 | c1;
}
output.value = "exec(bytes('" + String.fromCharCode.apply(String, bufferView) + "','u16')[2:])";
displayStats();
} catch (error) {
output.value = "An error occured. Refresh the page or check the logs.";
throw new Error("An error occured:\n" + error +
"\nPlease visit: https://github.com/clemg/pythongolfer/blob/main/README.md#qa" +
"\nMaybe the problem will be listed there. If not, you can open an issue."
);
}
}
function minifyCode(code) {
// Minify using python-minifier
let charsWon, minifiedCode;
let request = new XMLHttpRequest();
request.open("POST", "https://clemg.pythonanywhere.com/", false);
request.setRequestHeader('Content-Type', 'application/json');
request.send(JSON.stringify({
"code": code.value
}));
if (request.status === 200) {
let data = JSON.parse(request.response);
charsWon = data.chars_won;
minifiedCode = data.golfed_code;
return minifiedCode;
}
}
document.addEventListener("DOMContentLoaded", function() {
document.body.addEventListener("keydown", function(event) {
if(!(event.keyCode === 13 && (event.metaKey || event.ctrlKey))) { return; }
golfcode();
});
});