-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontentScript.js
159 lines (146 loc) · 4 KB
/
contentScript.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
import { message } from "antd";
// import "./content.css";
import { copyToClipboard, 不讲武德 } from "./utils";
import zenAble from "./zen/zenMode";
// if (testCase[i] === '"') {
// while (i < testCase.length && testCase[i] !== '"') {
// stack.push(testCase[i]);
// }
// stack.push("\n");
// } else
// testcase eg: `bottom = "BCD", allowed = ["BCG", "CDE", "GEA", "FFF"], c = [1,2,3], d = 2`
function normalize(testCase) {
testCase = testCase.trim().replace(/\n/g, "").replace(" ", "");
// 单一参数
// console.log(testCase);
if (!testCase.includes("=")) {
// 数组直接返回
if (testCase.includes("[") || testCase.includes('"')) {
return testCase;
} else {
// 输入: 3, 2, 0, 0
// 输入: 0.0625
const parts = testCase.split(",");
if (parts.length == 0) return parts.join("");
return parts.join("\n");
}
}
let stack = [];
let i = 0;
while (i < testCase.length) {
while (i < testCase.length && testCase[i] !== "=") {
i += 1;
}
// skip =
i += 1;
while (i < testCase.length && testCase[i] !== "[" && testCase[i] != ",") {
stack.push(testCase[i]);
i += 1;
}
if (testCase[i] == ",") {
// skip ,
i += 1;
stack.push("\n");
} else {
// cnt 左括号[ 与 右括号] 个数的差值
let cnt = 0;
while (i < testCase.length) {
stack.push(testCase[i]);
cnt += testCase[i] === "[";
cnt -= testCase[i] === "]";
i += 1;
if (cnt == 0) {
if (i !== testCase.length) {
stack.push("\n");
}
break;
}
}
}
}
return stack.join("");
}
function extractTestCase(text, prefix) {
const possiblePrefixs = [
"输出",
"返回",
"Output",
"output",
"Return",
"return",
"",
];
for (let tag of possiblePrefixs) {
const testCase = text.match(new RegExp(`${prefix}(.*)${tag}`, "s"));
if (testCase && testCase.length > 1) {
return testCase;
}
}
return [];
}
function getProviedTestCases() {
const possibleTags = ["pre", "p"];
const possiblePrefixs = ["输入:", "输入:", "Input:", "input:"];
const ans = [];
for (let tag of possibleTags) {
const pres = document.querySelectorAll(tag);
for (let prefix of possiblePrefixs) {
for (var i = 0; i < pres.length; ++i) {
if (pres[i].innerText.includes(prefix)) {
const testcase = extractTestCase(pres[i].innerText, prefix);
if (!testcase || testcase.length <= 1) {
不讲武德();
return [];
}
ans.push(normalize(testcase[1]));
}
}
if (ans.length > 0) return ans;
}
}
return ans;
}
function insertButton() {
const buttons = document.querySelectorAll("button");
for (var i = 0; i < buttons.length; ++i) {
if (buttons[i].innerText.includes("执行代码")) {
const copyButton = buttons[i].cloneNode(true);
copyButton.innerText = "复制所有内置用例";
copyButton.style["margin-left"] = "10px";
copyButton.onclick = () => {
const cases = getProviedTestCases();
if (cases.filter(Boolean).length === 0) return 不讲武德();
copyToClipboard(cases.join("\n"));
message.success({
content: "复制成功~",
});
};
buttons[i].parentElement.prepend(copyButton);
return true;
}
}
return false;
}
let inserted = false;
const timerId = setInterval(() => {
if (inserted) return clearInterval(timerId);
if (insertButton()) {
window.location.title = "";
inserted = true;
// 可进入禅定模式
zenAble();
}
}, 1000);
// class Main extends React.Component {
// render() {
// return (
// <div className={"my-extension"}>
// <h1 onClick={t}>Hello world - My first Extension</h1>
// </div>
// );
// }
// }
// const app = document.createElement("div");
// app.id = "my-extension-root";
// document.body.appendChild(app);
// ReactDOM.render(<Main />, app);