This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
325 lines (282 loc) · 9.93 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var codepage = "λƛ¬∧⟑∨⟇÷׫»°•ß†€"
codepage += "½∆ø↔¢⌐æʀʁɾɽÞƈ∞¨␠"
codepage += "!\"#$%&'()*+,-./01"
codepage += "23456789:;<=>?@A"
codepage += "BCDEFGHIJKLMNOPQ"
codepage += "RSTUVWXYZ[\\]`^_abc"
codepage += "defghijklmnopqrs"
codepage += "tuvwxyz{|}~↑↓∴∵›"
codepage += "‹∷¤ð→←βτȧḃċḋėḟġḣ"
codepage += "ḭŀṁṅȯṗṙṡṫẇẋẏż√⟨⟩"
codepage += "‛₀₁₂₃₄₅₆₇₈¶⁋§ε¡"
codepage += "∑¦≈µȦḂĊḊĖḞĠḢİĿṀṄ"
codepage += "ȮṖṘṠṪẆẊẎŻ₌₍⁰¹²∇⌈"
codepage += "⌊¯±₴…□↳↲⋏⋎꘍ꜝ℅≤≥"
codepage += "≠⁼ƒɖ∪∩⊍£¥⇧⇩ǍǎǏǐǑ"
codepage += "ǒǓǔ⁽‡≬⁺↵⅛¼¾Π„‟"
search = window
glyphQuery = String.fromCharCode(0162, 105, 0143, 107)
this.prevQuery = ""
secret = "dQw4"
secret += secret[2]
temp = "9WgXc"
secret += temp + secret[1]
temp = "out"
temp += temp[1] + "."
temp += "be"
temp = "y" + temp
temp = codepage[47] + temp
temp = codepage[115] + codepage[58] + temp[0] + temp
secret = "tp" + temp + "/" + secret
secret = "h" + codepage[116] + secret
var og_keyboard_html
var selectedBox = 'code' //whether 'header', 'code', or 'footer' are selected
function resizeCodeBox(id) {
// Resize the code box with the given id
var element = document.getElementById(id);
element.style.height = ""
element.style.height = element.scrollHeight + 4 + "px"
}
function updateCount() {
var byte_box = document.getElementById("code-count")
var code = e_code.getValue()
if ([...code].every(x => (codepage + ' ' + '\n').includes(x))) {
byte_box.innerText = `Code: ${code.length} byte` + "s".repeat(code.length != 1)
} else {
var x = new Blob([code]).size
byte_box.innerText = `Code: ${x} byte${"s".repeat(x != 1)}` + ' (UTF-8)'
}
}
function encode(obj) {
return btoa(unescape(encodeURIComponent(JSON.stringify(obj))));
}
function decode(str) {
if (str){
return JSON.parse(decodeURIComponent(escape(atob(str))));
} else {
return [];
}
}
function generateURL() {
var flags = document.getElementById("flag").value
var code = e_code.doc.getValue()
var inputs = document.getElementById("inputs").value
var header = e_header.doc.getValue()
var footer = e_footer.doc.getValue()
var url = [flags, header, code, footer, inputs];
return location.origin + "/#" + encode(url)
}
function shareOptions(shareType) {
var code = e_code.doc.getValue()
var url = generateURL()
var flags = document.getElementById("flag").value
var flag_appendage = ","
if (flags) {
flag_appendage = " `" + flags + "`,"
}
var output = ""
var utfable = [...code].every(x => (codepage + ' ' + '\n').includes(x))
var len = utfable ? code.length : new Blob([code]).size
switch (shareType) {
case "permalink":
output = url
break
case "cmc":
output = `[Vyxal, ${len} byte${"s".repeat(code.length != 1)}${utfable ? '' : ' (UTF-8)'}](${url})`
break
case "post-template":
output = `# [Vyxal](https://github.com/Vyxal/Vyxal)${flag_appendage} ${len} byte${"s".repeat(len != 1)}${utfable ? '' : ' (UTF-8)'}
\`\`\`
${code}
\`\`\`
[Try it Online!](${url})`;
break
case "markdown":
output = `[Try it Online!](${url})`
break
}
var outputBox = document.getElementById("output")
outputBox.value = output
copyToClipboard("output")
resizeCodeBox("output")
expandBoxes()
}
function decodeURL() {
var [flags, header, code, footer, inputs] = decode(window.location.hash.substring(1));
var flag_box = document.getElementById("flag")
var inputs_box = document.getElementById("inputs")
var queryIsNonEmpty = code || flags || inputs || header || footer
var allBoxesAreEmpty = !(flag_box.value
|| e_header.getValue() || e_code.getValue()
|| e_footer.getValue() || inputs_box.value)
if (queryIsNonEmpty && allBoxesAreEmpty) {
flag_box.value = flags
e_code.doc.setValue(code)
inputs_box.value = inputs
e_header.doc.setValue(header)
e_footer.doc.setValue(footer)
e_header.refresh()
e_footer.refresh()
run_button.click()
} else {
expandBoxes()
}
}
function expandBoxes() {
["flag", "inputs", "output", "extra"].forEach(function (n) {
var boxToExpand = document.getElementById(n + "-detail")
var actualBox = document.getElementById(n)
if (actualBox.value) {
boxToExpand.open = true
} else {
boxToExpand.open = false
}
resizeCodeBox(n)
})
if (e_header.getValue()) {
document.getElementById("header-detail").open = true
e_header.refresh()
}
if (e_footer.getValue()) {
document.getElementById("footer-detail").open = true
e_footer.refresh()
}
}
function replaceHTMLChar(char) {
return char === "" ? "\n" :
char === "␠" ? " " :
char === "<" ? "<" :
char === ">" ? ">" :
char === "&" ? "&" : char
}
function copyToClipboard(arg) {
var el = document.getElementById(arg)
// navigator.clipboard.writeText(el)
el.select()
document.execCommand("copy")
}
window.addEventListener("DOMContentLoaded", e => {
const run = document.getElementById("run_button")
const session = $("session-code")[0].innerHTML
const stdin = document.getElementById("inputs")
const flags = document.getElementById("flag")
const output = document.getElementById("output")
const extra = document.getElementById("extra")
const filter = document.getElementById("filterBox")
function execute() {
return Vyxal.execute(
e_header.doc.getValue() + e_code.doc.getValue() + e_footer.doc.getValue(),
flags.value,
stdin.value,
out => output.value += out,
warning => extra.value += warning,
error => extra.value += error
)
}
function do_run() {
run.blur()
if (!run.innerHTML.includes("fa-spin")) {
run.innerHTML =
`<svg class="fa-spin" style="width:24px;height:24px" viewBox="0 0 24 24">
<path fill="currentColor" d="M12,4V2A10,10 0 0,0 2,12H4A8,8 0 0,1 12,4Z"/>
</svg>`;
if (flags.value.includes('E') && !flags.value.includes("h")) {
alert('Please read and ensure you 100% trust the JavaScript code which is about to be evaluated. The code is (see next alert):')
alert(res.stdout)
if (confirm('Do you want to execute it? If you are remotely unsure, click Cancel!')) {
try {
execute()
} catch (e) {
res.stdout = e
}
}
} else {
execute()
}
run.innerHTML =
`<i class="fas fa-play-circle"></i>
`;
if (e_code.doc.getValue() == 'lyxal') {
location.href = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
}
if (flags.value.includes('Ḣ') && !flags.value.includes("h")) {
const container = document.getElementById("html-rendered-output")
const iframe = document.createElement("iframe")
iframe.srcdoc = res.stdout
container.innerHTML = iframe.outerHTML
container.hidden = false
} else {
document.getElementById("html-rendered-output").hidden = true
}
expandBoxes()
} else {
$.post("/kill", { session: session }, res => 0)
}
}
$("#run_button").on("click", do_run)
$("#clear").on("click", e => {
e_code.doc.setValue('')
stdin.value = ""
output.value = ""
extra.value = ""
e_footer.doc.setValue('')
e_header.doc.setValue('')
updateCount()
flags.value = ""
filter.value = ""
glyphSearch()
expandBoxes()
})
})
document.addEventListener('keydown', (event) => {
if ((event.ctrlKey || event.metaKey) && event.key == 'Enter') {
document.getElementById("run_button").onclick()
}
})
// Codemirror stuff begins here
function initCodeMirror() {
const $$$ = x => document.querySelector(x)
//Get the corresponding codemirror textarea for any of 'code', 'header', and 'footer'
function getCodeMirrorTextArea(boxId) {
return $('#' + boxId).parent().children('div').children().not('[class]').children()[0]
}
function resize(elem) {
var dummy = $$$("#dummy")
dummy.style.fontFamily = getComputedStyle($$$('.CodeMirror.cm-s-default')).fontFamily
dummy.style.fontSize = '15px'
dummy.style.lineHeight = '24px'
dummy.value = elem.doc.getValue()
elem.setSize(
null,
(elem.lineCount() * 22) + 24
)
elem.refresh();
dummy.value = ""
// Make sure e_code is not null
if ("e_code" in globalThis) {
updateCount()
}
}
let mode = {
mode: 'vyxal',
lineWrapping: true,
autofocus: true,
}
for (const boxId of ['header', 'code', 'footer']) {
console.log(boxId)
globalThis['e_' + boxId] = CodeMirror.fromTextArea($$$('#' + boxId), mode)
globalThis['e_' + boxId].on('change', cm => {
resize(globalThis['e_' + boxId])
globalThis['e_' + boxId].value = cm.getValue()
})
resize(globalThis['e_' + boxId])
var box = getCodeMirrorTextArea(boxId)
if (box) {
const capturedId = boxId
box.addEventListener('focusin', event => selectedBox = capturedId)
}
}
}
function repr(str) {
return str.replace(/'/g, "'").replace(/"/g, """)
}