-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcontent.js
72 lines (61 loc) · 2.2 KB
/
content.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
chrome.runtime.onMessage.addListener(function (message, sender, callback) {
if (message.functiontoInvoke != "onClick") {
return;
}
const examples = onClick();
chrome.storage.sync.get(["outer", "inner"], function (items) {
const method = examples.map(e =>
items.inner
.replace(/{{\s*NAME*\s}}/g, e.name)
.replace(/{{\s*INPUT\s*}}/g, e.input)
.replace(/{{\s*OUTPUT\s*}}/g, e.output)
).join("\n");
const code = items.outer.replace(/{{\s*METHOD\s*}}/g, method);
callback(code);
});
return true;
});
function onClick() {
let name = null;
let input = null;
let output = null;
let examples = [];
const sections = document.querySelectorAll("#task-statement section");
for (const section of sections) {
let h3 = section.querySelector("h3");
let pre = section.querySelector("pre");
// SECTION の中に H3 タグがある場合(ABC033_D)と、
// SECTION の直前に H3 タグがある場合(ARC014_A)がある
if (h3 == null) {
let prev = section.previousElementSibling;
if (prev != null > 0 && prev.tagName == "H3") {
h3 = prev;
}
}
if (h3 != null && pre != null && (h3.offsetWidth || h3.offsetHeight)) {
let header = h3.firstChild.textContent.trim();
let example = pre.textContent;
// シンタックスハイライトされている場合、リスト形式に
// なっているので、一行ずつ取り出す (ABC007_3、など)
let pretty = pre.getElementsByTagName("li");
if (pretty.length > 0) {
example = "";
for (let j = 0; j < pretty.length; j++) {
example += pretty[j].textContent;
example += "\n";
}
}
if (header.indexOf("入力例") == 0 || header.indexOf("Sample Input") == 0) {
name = header.replace("入力例", "sample").replace(/\s+/g, "");
input = example.trim();
} else if (header.indexOf("出力例") == 0 || header.indexOf("Sample Output") == 0) {
output = example.trim();
}
}
if (name != null && input != null && output != null) {
examples.push({ name: name, input: input, output: output });
name = input = output = null;
}
}
return examples;
}