-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontentScript.js
182 lines (167 loc) · 5.15 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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("=")) {
// 数组直接返回
// eslint-disable-next-line
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);
const writeSolutionButton = buttons[i].cloneNode(true);
writeSolutionButton.innerText = "写题解";
writeSolutionButton.style["margin-left"] = "10px";
writeSolutionButton.onclick = () => {
// d: "<a href="/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/">1579. 保证图可完全遍历</a>"
const d = document.querySelector(`[data-cypress="QuestionTitle"]`)
.innerHTML;
const title = d.match(/(\d+\. .+)(?=<)/)[1];
const link = "https://leetcode-cn.com" + d.match(/href="(.*?)"/)[1];
const language = document.querySelector("#lang-select").innerText;
// let code = document.querySelector(
// ".monaco-scrollable-element,.editor-scrollable"
// ).innerText;
// const desc = document.querySelector("#question-detail-main-tabs")
// .children[1].children[0].children[1].innerText;
window.open(
`https://leetcode-pp.github.io/leetcode-cheat/?link=${link}&title=${title}&language=${language}&tab=solution-template`
);
};
buttons[i].parentElement.prepend(writeSolutionButton);
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);