Skip to content

Commit cea894f

Browse files
author
lucifer
committed
feat: 使用 RapidAPI 无需使用 access token
1 parent 2df819e commit cea894f

File tree

5 files changed

+202
-145
lines changed

5 files changed

+202
-145
lines changed

‎public/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "leetcode cheatsheet",
44
"description": "刷题小助手,made by 力扣加加",
5-
"version": "0.8.4",
5+
"version": "0.8.5",
66
"browser_action": {
77
"default_popup": "index.html",
88
"default_title": "力扣加加"

‎src/codeTemplates/preSum.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const pre2dPythonCode = `
2626
pre[i][j] = pre[i-1][j]+ pre[i][j-1] - pre[i-1][j-1] + matrix[i-1][j-1]
2727
2828
# 使用,等价于以(x1,y1)为矩阵左上角以(x2,y2)为矩阵右下角的所有格子的和
29-
pre[x2][y2] + pre[x1-1][y1-1] - pre[x1-1][y2] - pre[x2][y1-1]
29+
pre[x2+1][y2+1] + pre[x1][y1] - pre[x1][y2+1] - pre[x2+1][y1]
3030
`;
3131

3232
module.exports = {

‎src/contentScript.js

+119-109
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,61 @@
11
import React, { PureComponent } from "react";
2-
import ReactDOM from "react-dom";
2+
// import ReactDOM from "react-dom";
33
import { message, Button, Dropdown, Menu } from "antd";
44
// import "./content.css";
5-
import { copyToClipboard, 不讲武德, getStorage } from "./utils";
5+
import {
6+
copyToClipboard,
7+
不讲武德,
8+
getStorage,
9+
setCloundStorage,
10+
} from "./utils";
611
import zenAble from "./zen/zenMode";
7-
import AccessToken from "./components/AccessToken";
12+
// import AccessToken from "./components/AccessToken";
813

9-
class SolutionButton extends PureComponent {
10-
static propTypes = {};
14+
// class SolutionButton extends PureComponent {
15+
// static propTypes = {};
1116

12-
static defaultProps = {};
13-
constructor() {
14-
super();
15-
this.state = {
16-
modalVisible: false,
17-
};
18-
}
19-
20-
render() {
21-
return (
22-
<>
23-
<AccessToken
24-
visible={this.state.modalVisible}
25-
onOk={() =>
26-
this.setState({
27-
modalVisible: false,
28-
})
29-
}
30-
onCancel={() =>
31-
this.setState({
32-
modalVisible: false,
33-
})
34-
}
35-
/>
36-
<Dropdown
37-
style={{ marginLeft: "10px" }}
38-
type="link"
39-
onClick={() => {
40-
// d: "<a href="/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/">1579. 保证图可完全遍历</a>"
41-
const d = document.querySelector(`[data-cypress="QuestionTitle"]`)
42-
.innerHTML;
43-
const title = d.match(/(\d+\. .+)(?=<)/)[1];
44-
const link = window.location.origin + d.match(/href="(.*?)"/)[1];
45-
const language = document.querySelector("#lang-select").innerText;
46-
let code = document.querySelector(
47-
".monaco-scrollable-element,.editor-scrollable"
48-
).innerText;
17+
// static defaultProps = {};
18+
// constructor() {
19+
// super();
20+
// this.state = {
21+
// modalVisible: false,
22+
// };
23+
// }
4924

50-
const desc = document.querySelector("#question-detail-main-tabs")
51-
.children[1].children[0].children[1].innerText;
52-
getStorage("leetcode-cheatsheet-token")
53-
.then((res) => res.result.value)
54-
.then((res) => {
55-
if (!res.raw) throw new Error("whatever");
56-
return res;
57-
})
58-
.catch(() => ({
59-
raw: "e574bf60b50d8d2d2db2320ee83aba3cd29cecf2",
60-
}))
61-
.then((res) => {
62-
const t = res.raw;
63-
fetch(
64-
"https://api.github.com/repos/azl397985856/stash/issues",
65-
{
66-
method: "POST",
67-
headers: {
68-
"Content-Type": "application/json",
69-
Authorization: `token ${t}`,
70-
},
71-
body: JSON.stringify({
72-
title: `此 issue 由 leetcode-cheatsheet 插件自动生成于 ${new Date().toDateString()}`,
73-
body: JSON.stringify({
74-
title,
75-
link,
76-
language,
77-
code,
78-
desc,
79-
}),
80-
}),
81-
}
82-
)
83-
.then((res) => res.json())
84-
.then((res) => {
85-
if (res.number) {
86-
window.open(
87-
`https://leetcode-pp.github.io/leetcode-cheat/?issue_number=${res.number}&tab=solution-template`
88-
);
89-
} else {
90-
message.warn({
91-
content:
92-
"使用 Github API 失败,已为您切换为普通模式,普通模式仅可自动带入题目名称,题目地址以及题解语言。",
93-
});
94-
setTimeout(() => {
95-
window.open(
96-
`https://leetcode-pp.github.io/leetcode-cheat/?title=${title}&link=${link}&language=${language}&tab=solution-template`
97-
);
98-
}, 2000);
99-
}
100-
});
101-
});
102-
}}
103-
overlay={
104-
<Menu onClick={() => this.setState({ modalVisible: true })}>
105-
<Menu.Item key="1">填入 access token</Menu.Item>
106-
</Menu>
107-
}
108-
>
109-
<Button>写题解</Button>
110-
</Dropdown>
111-
</>
112-
);
113-
}
114-
}
25+
// render() {
26+
// return (
27+
// <>
28+
// <AccessToken
29+
// visible={this.state.modalVisible}
30+
// onOk={() =>
31+
// this.setState({
32+
// modalVisible: false,
33+
// })
34+
// }
35+
// onCancel={() =>
36+
// this.setState({
37+
// modalVisible: false,
38+
// })
39+
// }
40+
// />
41+
// <Dropdown
42+
// style={{ marginLeft: "10px" }}
43+
// type="link"
44+
// onClick={() => {
45+
// //
46+
// }}
47+
// overlay={
48+
// <Menu onClick={() => this.setState({ modalVisible: true })}>
49+
// <Menu.Item key="1">填入 access token</Menu.Item>
50+
// </Menu>
51+
// }
52+
// >
53+
// <Button>写题解</Button>
54+
// </Dropdown>
55+
// </>
56+
// );
57+
// }
58+
// }
11559

11660
// if (testCase[i] === '"') {
11761
// while (i < testCase.length && testCase[i] !== '"') {
@@ -237,9 +181,75 @@ function insertButton() {
237181
});
238182
};
239183
buttons[i].parentElement.prepend(copyButton);
240-
const writeSolutionButton = document.createElement("div");
241184

242-
ReactDOM.render(<SolutionButton />, writeSolutionButton);
185+
// const writeSolutionButton = document.createElement("div");
186+
const writeSolutionButton = buttons[i].cloneNode(true);
187+
writeSolutionButton.innerText = "写题解";
188+
writeSolutionButton.style["margin-left"] = "10px";
189+
190+
writeSolutionButton.onclick = () => {
191+
// d: "<a href="/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/">1579. 保证图可完全遍历</a>"
192+
const ele = document.querySelector(`[data-cypress="QuestionTitle"]`);
193+
if (!ele) {
194+
return message.warn({
195+
content: "获取题目描述失败,请先切换到题目描述标签",
196+
});
197+
}
198+
const d = document.querySelector(`[data-cypress="QuestionTitle"]`)
199+
.innerHTML;
200+
const title = d.match(/(\d+\. .+)(?=<)/)[1];
201+
const link = window.location.origin + d.match(/href="(.*?)"/)[1];
202+
const language = document.querySelector("#lang-select").innerText;
203+
let code = document.querySelector(
204+
".monaco-scrollable-element,.editor-scrollable"
205+
).innerText;
206+
207+
const desc = document.querySelector("#question-detail-main-tabs")
208+
.children[1].children[0].children[1].innerText;
209+
getStorage("leetcode-cheatsheet-token")
210+
.then((res) => res.result.value)
211+
.then((res) => {
212+
if (!res.raw) throw new Error("whatever");
213+
return res;
214+
})
215+
.catch(() => ({
216+
raw: "e574bf60b50d8d2d2db2320ee83aba3cd29cecf2",
217+
}))
218+
.then((res) => {
219+
const t = res.raw;
220+
setCloundStorage(
221+
{
222+
title,
223+
link,
224+
language,
225+
code,
226+
desc,
227+
},
228+
229+
{
230+
token: t,
231+
}
232+
).then((res) => {
233+
if (res.id) {
234+
window.open(
235+
`https://leetcode-pp.github.io/leetcode-cheat/?issue_number=${res.id}&tab=solution-template`
236+
);
237+
} else {
238+
message.warn({
239+
content:
240+
"使用 Github API 失败,已为您切换为普通模式,普通模式仅可自动带入题目名称,题目地址以及题解语言。",
241+
});
242+
setTimeout(() => {
243+
window.open(
244+
`https://leetcode-pp.github.io/leetcode-cheat/?title=${title}&link=${link}&language=${language}&tab=solution-template`
245+
);
246+
}, 2000);
247+
}
248+
});
249+
});
250+
};
251+
252+
// ReactDOM.render(<SolutionButton />, writeSolutionButton);
243253

244254
buttons[i].parentElement.prepend(writeSolutionButton);
245255
return true;

‎src/solutionTemplate/index.jsx

+24-34
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import {
2020
getStorage,
2121
setStorage,
2222
debounce,
23+
getCloundStorage,
2324
} from "../utils.js";
2425

2526
import "katex/dist/katex.min.css";
2627
import CodeBlock from "../components/CodeBlock";
27-
import AccessToken from "../components/AccessToken";
28+
// import AccessToken from "../components/AccessToken";
2829

2930
const { TextArea } = Input;
3031
const { Option } = Select;
@@ -223,45 +224,34 @@ export default function SolutionTemplate() {
223224
}))
224225
.then((res) => {
225226
const t = res.raw;
226-
fetch(
227-
`https://api.github.com/repos/azl397985856/stash/issues/${getUrlParameter(
228-
"issue_number"
229-
)}`,
230-
{
231-
headers: {
232-
accept: "application/json",
233-
"Content-Type": "application/json",
234-
Authorization: `token ${t}`,
235-
},
236-
}
237-
)
238-
.then((res) => res.json())
239-
.then((res) => JSON.parse(res.body))
240-
.then((res) => {
241-
const { link, title, code, language, desc } = res;
242-
setLanguage(language?.toLowerCase());
243-
setTemplate(
244-
getTemplate({
245-
desc,
246-
time,
247-
space,
248-
language: language?.toLowerCase(),
249-
link,
250-
title,
251-
code,
252-
})
253-
);
254-
});
227+
228+
getCloundStorage(getUrlParameter("issue_number"), {
229+
token: t,
230+
}).then((res) => {
231+
const { link, title, code, language, desc } = res;
232+
setLanguage(language?.toLowerCase());
233+
setTemplate(
234+
getTemplate({
235+
desc,
236+
time,
237+
space,
238+
language: language?.toLowerCase(),
239+
link,
240+
title,
241+
code,
242+
})
243+
);
244+
});
255245
});
256246
});
257247

258248
return (
259249
<>
260-
<AccessToken
250+
{/* <AccessToken
261251
visible={modalVisible}
262252
onOk={() => setModalVisible(false)}
263253
onCancel={() => setModalVisible(false)}
264-
/>
254+
/> */}
265255
{!isInExtension() ? (
266256
<>
267257
<div className="line">
@@ -435,15 +425,15 @@ export default function SolutionTemplate() {
435425
<li>
436426
题解每五秒备份一次,如果你不小心刷新了浏览器可以点击下方的恢复按钮还原。由于是覆盖式备份,因此仅会保存最后一次编辑的内容。
437427
</li>
438-
<li>
428+
{/* <li>
439429
自动带入功能使用了 Github API,如果题目信息没有自动带入可能是 Github
440430
API 调用次数限制,大家可以通过
441431
<Button type="link" onClick={() => setModalVisible(true)}>
442432
填写 Access Token
443433
</Button>
444434
解决(注意此网站和力扣数据是隔离,因此填写 token
445435
也是独立,互不影响的)。后期考虑搞个服务器给大家存放。
446-
</li>
436+
</li> */}
447437
<li>目前公式无法预览,原因暂时不明,不过后期会支持。</li>
448438
<li>后续考虑提供更多题解模板。</li>
449439
<li>后续考虑支持更多主题,以及用户自定义主题。</li>

0 commit comments

Comments
 (0)