Skip to content

Commit d484494

Browse files
author
lucifer
committed
feat: 支持用户手动输入 token
1 parent 4dba047 commit d484494

File tree

12 files changed

+257
-92
lines changed

12 files changed

+257
-92
lines changed
Binary file not shown.

‎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.1",
5+
"version": "0.8.3",
66
"browser_action": {
77
"default_popup": "index.html",
88
"default_title": "力扣加加"

‎src/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { LEETCODE_CN_URL } from "./constant/index";
1111
// import TestCase from "./testCase";
1212
import ProblemDetail from "./Detail";
1313
import Roadmap from "./roadmap/roadmap.jsx";
14-
import TagOrLink from "./TagOrLink";
14+
import TagOrLink from "./components/TagOrLink";
1515
import tempaltes from "./codeTemplates/index";
1616
import checkUpdate from "./checkUpdates";
1717

‎src/Detail.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
} from "./constant/index";
1111
import db from "./db/db";
1212

13-
import Codes from "./codes";
14-
import TagOrLink from "./TagOrLink";
13+
import Codes from "./components/codes";
14+
import TagOrLink from "./components/TagOrLink";
1515

1616
const { TabPane } = Tabs;
1717
const { Panel } = Collapse;

‎src/codeTemplates/codeTemplate.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import { Button, Collapse, Tabs } from "antd";
33

4-
import Codes from "../codes";
4+
import Codes from "../components/codes";
55

66
import {
77
LEETCODE_CN_URL,

‎src/components/AccessToken.jsx

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import React, { PureComponent } from "react";
2+
import { Button, Modal, Input } from "antd";
3+
import PropTypes from "prop-types";
4+
5+
import { setStorage, getStorage } from "../utils";
6+
7+
export default class AccessToken extends PureComponent {
8+
static propTypes = {
9+
visible: PropTypes.bool,
10+
onOk: PropTypes.func,
11+
onCancel: PropTypes.func,
12+
};
13+
14+
static defaultProps = {};
15+
constructor() {
16+
super();
17+
this.state = {
18+
token: "",
19+
};
20+
21+
getStorage("leetcode-cheatsheet-token")
22+
.then((res) => res.result.value)
23+
.then((res) => {
24+
if (res.raw === null) return;
25+
this.setState({
26+
token: res.raw,
27+
});
28+
});
29+
}
30+
31+
render() {
32+
return (
33+
<Modal
34+
title="完善 token 信息,提升稳定性"
35+
visible={this.props.visible}
36+
onOk={() => {
37+
// save
38+
setStorage("leetcode-cheatsheet-token", {
39+
raw: this.state.token,
40+
});
41+
this.props.onOk();
42+
}}
43+
onCancel={this.props.onCancel}
44+
>
45+
不填写 token
46+
有时候可能无法成功带过去信息(比如题目描述,代码等)。强烈建议大家填写!请放心,你的
47+
token 只会存在于你的本地,不会被上传到服务端哦。
48+
<Button
49+
type="link"
50+
target="_blank"
51+
href="https://docs.github.com/cn/github/authenticating-to-github/creating-a-personal-access-token"
52+
>
53+
不知道如何创建?点击这里!(只需要勾选 repo 中的 public_repo 即可)
54+
</Button>
55+
Personal Access Token:
56+
<Input
57+
placeholder=""
58+
value={this.state.token}
59+
onChange={(e) => this.setState({ token: e.target.value })}
60+
/>
61+
<Button
62+
style={{ marginTop: 10 }}
63+
size="small"
64+
type="link"
65+
onClick={() => {
66+
setStorage("leetcode-cheatsheet-token", {
67+
raw: void 0,
68+
});
69+
this.setState({
70+
token: "",
71+
});
72+
this.props.onOk();
73+
}}
74+
>
75+
清除 token(使用默认 token)
76+
</Button>
77+
</Modal>
78+
);
79+
}
80+
}
File renamed without changes.
File renamed without changes.

‎src/codes.jsx renamed to ‎src/components/codes.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Button, message, Collapse } from "antd";
44
import marked from "marked";
55
import hljs from "highlight.js";
66

7-
import { copy } from "./utils";
7+
import { copy } from "../utils";
88

99
const formatCodeToMarkDown = (code, lang) => `\`\`\`${lang}\n${code}\`\`\`\n`;
1010

‎src/contentScript.js

+103-42
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,105 @@
1-
import { message } from "antd";
1+
import React, { PureComponent } from "react";
2+
import ReactDOM from "react-dom";
3+
import { message, Button, Dropdown, Menu } from "antd";
24
// import "./content.css";
3-
import { copyToClipboard, 不讲武德 } from "./utils";
5+
import { copyToClipboard, 不讲武德, getStorage } from "./utils";
46
import zenAble from "./zen/zenMode";
7+
import AccessToken from "./components/AccessToken";
8+
9+
class SolutionButton extends PureComponent {
10+
static propTypes = {};
11+
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;
49+
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+
window.open(
86+
`https://leetcode-pp.github.io/leetcode-cheat/?issue_number=${res.number}&tab=solution-template`
87+
);
88+
});
89+
});
90+
}}
91+
overlay={
92+
<Menu onClick={() => this.setState({ modalVisible: true })}>
93+
<Menu.Item key="1">填入 access token</Menu.Item>
94+
</Menu>
95+
}
96+
>
97+
<Button>写题解</Button>
98+
</Dropdown>
99+
</>
100+
);
101+
}
102+
}
5103

6104
// if (testCase[i] === '"') {
7105
// while (i < testCase.length && testCase[i] !== '"') {
@@ -127,46 +225,9 @@ function insertButton() {
127225
});
128226
};
129227
buttons[i].parentElement.prepend(copyButton);
130-
const writeSolutionButton = buttons[i].cloneNode(true);
131-
writeSolutionButton.innerText = "写题解";
132-
writeSolutionButton.style["margin-left"] = "10px";
133-
writeSolutionButton.onclick = () => {
134-
// d: "<a href="/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/">1579. 保证图可完全遍历</a>"
135-
const d = document.querySelector(`[data-cypress="QuestionTitle"]`)
136-
.innerHTML;
137-
const title = d.match(/(\d+\. .+)(?=<)/)[1];
138-
const link = window.location.origin + d.match(/href="(.*?)"/)[1];
139-
const language = document.querySelector("#lang-select").innerText;
140-
let code = document.querySelector(
141-
".monaco-scrollable-element,.editor-scrollable"
142-
).innerText;
143-
144-
const desc = document.querySelector("#question-detail-main-tabs")
145-
.children[1].children[0].children[1].innerText;
146-
fetch("https://api.github.com/repos/azl397985856/stash/issues", {
147-
method: "POST",
148-
headers: {
149-
"Content-Type": "application/json",
150-
Authorization: "token f4fed02e90310c97be2c51b5b97b1f5b3ad91a23",
151-
},
152-
body: JSON.stringify({
153-
title: `此 issue 由 leetcode-cheatsheet 插件自动生成于 ${new Date().toDateString()}`,
154-
body: JSON.stringify({
155-
title,
156-
link,
157-
language,
158-
code,
159-
desc,
160-
}),
161-
}),
162-
})
163-
.then((res) => res.json())
164-
.then((res) => {
165-
window.open(
166-
`https://leetcode-pp.github.io/leetcode-cheat/?issue_number=${res.number}&tab=solution-template`
167-
);
168-
});
169-
};
228+
const writeSolutionButton = document.createElement("div");
229+
230+
ReactDOM.render(<SolutionButton />, writeSolutionButton);
170231

171232
buttons[i].parentElement.prepend(writeSolutionButton);
172233
return true;

‎src/roadmap/roadmap.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from "react";
22
import { Radio, Image } from "antd";
33

4-
import Codes from "../codes";
5-
import TagOrLink from "../TagOrLink";
4+
import Codes from "../components/codes";
5+
import TagOrLink from "../components/TagOrLink";
66

77
const dpSingleCode = `
88
dp = [0] * (n + 1)

0 commit comments

Comments
 (0)