-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontentScript.js
323 lines (297 loc) · 9.11 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
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
// import "./content.css";
// import { message } from "antd";
import { copyToClipboard, bjwd, getStorage, setCloundStorage } from "./utils";
import zenAble from "./zen/zenMode";
// WTF! ant message didn't go well with chrome extension?
const message = {
success({ content }) {
//
},
warn({ content }) {
window.alert(content);
},
loading(content) {
//
return () => null;
},
};
// import AccessToken from "./components/AccessToken";
// class SolutionButton extends PureComponent {
// static propTypes = {};
// static defaultProps = {};
// constructor() {
// super();
// this.state = {
// modalVisible: false,
// };
// }
// render() {
// return (
// <>
// <AccessToken
// visible={this.state.modalVisible}
// onOk={() =>
// this.setState({
// modalVisible: false,
// })
// }
// onCancel={() =>
// this.setState({
// modalVisible: false,
// })
// }
// />
// <Dropdown
// style={{ marginLeft: "10px" }}
// type="link"
// onClick={() => {
// //
// }}
// overlay={
// <Menu onClick={() => this.setState({ modalVisible: true })}>
// <Menu.Item key="1">填入 access token</Menu.Item>
// </Menu>
// }
// >
// <Button>写题解</Button>
// </Dropdown>
// </>
// );
// }
// }
// 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) {
bjwd();
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 bjwd();
copyToClipboard(cases.join("\n"));
message.success({
content: "复制成功~",
});
};
buttons[i].parentElement.prepend(copyButton);
// const writeSolutionButton = document.createElement("div");
const writeSolutionButton = document.createElement("a");
writeSolutionButton.innerText = "去写题解";
writeSolutionButton.style["margin-right"] = "20px";
writeSolutionButton.style["line-height"] = "32px";
writeSolutionButton.onclick = () => {
// d: "<a href="/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/">1579. 保证图可完全遍历</a>"
const ele = document.querySelector(`[data-cypress="QuestionTitle"]`);
if (!ele) {
return message.warn({
content: "获取题目描述失败,请先切换到题目描述标签",
});
}
const d = ele.innerHTML;
const title = d.match(/(\d+\. .+)(?=<)/)[1];
const link = window.location.origin + d.match(/href="(.*?)"/)[1];
const language = document.querySelector("#lang-select").innerText;
// let code = document.querySelector(
// ".monaco-scrollable-element,.editor-scrollable"
// ).innerText;
const code = window?.monaco?.editor?.getModels()[0]?.getValue();
const desc = document.querySelector("#question-detail-main-tabs")
?.children[1]?.children[0]?.children[1]?.innerText;
if (!desc) {
return message.warn({
content: "获取题目描述失败,请先切换到题目描述标签",
});
}
const hide = message.loading("正在存储题目信息,请稍后~", 0);
writeSolutionButton.setAttribute("disabled", true);
// Dismiss manually and asynchronously
setTimeout(() => {
hide();
writeSolutionButton.removeAttribute("disabled");
}, 30000); // 超时 30s 都没好,那就别转了
getStorage("leetcode-cheatsheet-token")
.then((res) => res.result.value)
.then((res) => {
if (!res.raw) throw new Error("whatever");
return res;
})
.catch(() => ({
raw: "e574bf60b50d8d2d2db2320ee83aba3cd29cecf2",
}))
.then((res) => {
const t = res.raw;
setCloundStorage(
{
title,
link,
language,
code,
desc,
},
{
token: t,
}
)
.then((res) => {
hide();
writeSolutionButton.removeAttribute("disabled");
if (res.id) {
window.open(
`https://leetcode-pp.github.io/leetcode-cheat/?issue_number=${res.id}&tab=solution-template`
);
} else {
message.warn({
content:
"使用 Github API 失败,已为您切换为普通模式,普通模式仅可自动带入题目名称,题目地址以及题解语言。",
});
setTimeout(() => {
window.open(
`https://leetcode-pp.github.io/leetcode-cheat/?title=${title}&link=${link}&language=${language}&tab=solution-template`
);
}, 2000);
}
})
.catch(() => {
hide();
writeSolutionButton.removeAttribute("disabled");
});
});
};
// ReactDOM.render(<SolutionButton />, writeSolutionButton);
buttons[i].parentElement.prepend(writeSolutionButton);
// ele.appendChild(writeSolutionButton);
return true;
}
}
return false;
}
let inserted = false;
let retried = 0;
const MAX_TRY = 10;
const timerId = setInterval(() => {
if (inserted) return clearInterval(timerId);
if (retried > MAX_TRY) {
clearInterval(timerId);
return console.error("初始化 chrome 插件 content script 失败");
}
if (insertButton()) {
window.location.title = "";
inserted = true;
// 可进入禅定模式
zenAble();
}
retried++;
}, 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);