-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathoptions.js
363 lines (301 loc) · 9.22 KB
/
options.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
async function load(outer, inner) {
document.getElementById("outerCode").value = outer;
document.getElementById("innerCode").value = inner;
await save();
}
async function loadPython() {
const outer = `
import sys
import os
from io import StringIO
import unittest
def resolve():
pass
class TestClass(unittest.TestCase):
{{ METHOD }}
def judge(self, input, expected):
stdout, stdin = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(input)
resolve()
sys.stdout.seek(0)
actual = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, stdin
self.assertEqual(expected, actual)
if __name__ == "__main__":
if "ATCODER" in os.environ:
resolve()
else:
unittest.main(verbosity=2)
`.replace(/^\n/g, "");
const inner = `
def test_{{ NAME }}(self):
input = """{{ INPUT }}"""
expected = """{{ OUTPUT }}"""
self.judge(input, expected)
`.replace(/^\n/g, "");
await load(outer, inner);
}
async function loadJava() {
const outer = `
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class MainTest {
{{ METHOD }}
private void judge(String input, String output) throws Exception {
ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
Main.main(new String[0]);
Assertions.assertEquals(output + System.lineSeparator(), out.toString());
}
}
`.replace(/^\n/g, "");
const inner = `
@Test
public void {{ NAME }}() throws Exception {
String input = """
{{ INPUT }}""";
String output = """
{{ OUTPUT }}""";
judge(input, output);
}
`.replace(/^\n/g, "");
await load(outer, inner);
}
async function loadKotlin() {
const outer = `
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.PrintStream
class MainTest {
{{ METHOD }}
private fun judge(input: String, output: String) {
val sysIn = ByteArrayInputStream(input.toByteArray())
System.setIn(sysIn)
val sysOut = ByteArrayOutputStream()
System.setOut(PrintStream(sysOut))
main()
Assertions.assertEquals(output + System.lineSeparator(), sysOut.toString())
}
}
`.replace(/^\n/g, "");
const inner = `
@Test
fun {{ NAME }}() {
val input = """
{{ INPUT }}""".trimMargin()
val output = """
{{ OUTPUT }}""".trimMargin()
judge(input, output)
}
`.replace(/^\n/g, "");
await load(outer, inner);
}
async function loadCSharp() {
const outer = `
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
namespace AtCoder
{
[TestClass]
public class ProgramTest
{
{{ METHOD }}
private void Judge(string input, string output)
{
StringReader reader = new StringReader(input);
Console.SetIn(reader);
StringWriter writer = new StringWriter();
Console.SetOut(writer);
Program.Main(new string[0]);
Assert.AreEqual(output + Environment.NewLine, writer.ToString());
}
}
}
`.replace(/^\n/g, "");
const inner = `
[TestMethod]
public void {{ NAME }}()
{
string input =
@"{{ INPUT }}";
string output =
@"{{ OUTPUT }}";
Judge(input, output);
}
`.replace(/^\n/g, "");
await load(outer, inner);
}
async function loadGo() {
const outer = `
package main
import (
"bytes"
"os"
"testing"
)
{{ METHOD }}
func judge(t *testing.T, input, output string) {
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("os.Pipe(): %v", err)
}
if n, err := w.Write([]byte(input)); err != nil {
t.Fatalf("input is %v bytes, but only %v byte written", len(input), n)
}
stdin, stdout := os.Stdin, os.Stdout
os.Stdin, os.Stdout = r, w
main()
os.Stdin, os.Stdout = stdin, stdout
w.Close()
var buf bytes.Buffer
if _, err := buf.ReadFrom(r); err != nil {
t.Fatalf("can't read from reader: %v", err)
}
r.Close()
got := buf.String()
if got != output {
t.Errorf("got: %v, want: %v", got, output)
}
}
`.replace(/^\n/g, "");
const inner = `
func Test_{{ NAME }}(t *testing.T) {
judge(t, \`{{ INPUT }}\`+"\\n", \`{{ OUTPUT }}\`+"\\n")
}
`.replace(/^\n/g, "");
await load(outer, inner);
}
async function loadRuby() {
const outer = `
def main()
end
unless defined?(RSpec)
main()
exit
end
RSpec.describe do
ARGV.clear
{{ METHOD }}
def judge(input, output)
$stdin = StringIO.new(input)
$stdout = StringIO.new
main()
actual = $stdout.string
$stdin = STDIN
$stdout = STDOUT
expect(actual).to eq output
end
end
`.replace(/^\n/g, "");
const inner = `
it "{{ NAME }}" do
judge('{{ INPUT }}' + "\\n", '{{ OUTPUT }}' + "\\n")
end
`.replace(/^\n/g, "");
await load(outer, inner);
}
async function save() {
await chrome.storage.sync.set({
outer: document.getElementById("outerCode").value,
inner: document.getElementById("innerCode").value
});
console.debug("Saved.")
}
async function copy(e) {
await navigator.clipboard.writeText(e.target.textContent);
e.preventDefault();
const tooltip = bootstrap.Tooltip.getOrCreateInstance(e.target);
tooltip.show();
setTimeout(() => tooltip.hide(), 1500);
}
async function initialize() {
document.querySelectorAll('[data-i18n]').forEach(e => {
e.innerHTML = chrome.i18n.getMessage(e.dataset.i18n);
});
if (chrome.i18n.getUILanguage() == "ja") {
document.querySelectorAll('[data-i18n-lang="ja"]').forEach(e => e.classList.remove("d-none"))
} else {
document.querySelectorAll('[data-i18n-lang="en"]').forEach(e => e.classList.remove("d-none"))
}
const setTheme = function () {
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-bs-theme', 'dark')
} else {
document.documentElement.setAttribute('data-bs-theme', 'light')
}
};
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', setTheme);
setTheme();
document.getElementById("loadPython").addEventListener("click", loadPython);
document.getElementById("loadJava").addEventListener("click", loadJava);
document.getElementById("loadKotlin").addEventListener("click", loadKotlin);
document.getElementById("loadCSharp").addEventListener("click", loadCSharp);
document.getElementById("loadGo").addEventListener("click", loadGo);
document.getElementById("loadRuby").addEventListener("click", loadRuby);
document.getElementById("outerCode").addEventListener("change", save);
document.getElementById("innerCode").addEventListener("change", save);
Array.from(document.getElementsByClassName("mustache")).forEach(e => e.addEventListener("click", copy));
const isMajorOrMinorUpdate = function (prev, current) {
// v1.2.3 => {major: 1, minor: 2, patch: 3}
const [prevMajor, prevMinor] = prev.split(",").map(v => parseInt(v));
const [major, minor] = current.split(",").map(v => parseInt(v));
return (prevMajor < major) || (prevMajor == major && prevMinor < minor)
}
let updated = false;
let installed = false;
const manifest = chrome.runtime.getManifest();
const items = await chrome.storage.sync.get(null)
if (items.language !== undefined) {
// upgrade from v1
switch (items.language) {
case "Java":
await loadJava();
break;
case "Kotlin":
await loadKotlin();
break;
case "CSharp":
await loadCSharp();
break;
case "Python3":
await loadPython();
break;
}
await chrome.storage.sync.remove("language");
updated = true;
} else if (items.outer === undefined || items.inner === undefined) {
await loadPython();
installed = true;
} else {
if (items.version === undefined || isMajorOrMinorUpdate(items.version, manifest.version)) {
await chrome.storage.sync.set({ "version": manifest.version });
updated = true;
}
await load(items.outer, items.inner);
}
const permissions = { "origins": manifest.host_permissions };
if (!await chrome.permissions.contains(permissions)) {
const modal = new bootstrap.Modal('#initModal');
const button = document.getElementById("btnPermission");
button.addEventListener("click", async (e) => {
if (await chrome.permissions.request(permissions)) {
modal.hide();
document.getElementById("tutorial").click();
}
});
modal.show();
} else if (updated) {
document.getElementById("release").click();
} else if (installed) {
document.getElementById("tutorial").click();
}
}
document.addEventListener('DOMContentLoaded', initialize);