|
1 | 1 | import React, { PureComponent } from "react";
|
2 |
| -import ReactDOM from "react-dom"; |
| 2 | +// import ReactDOM from "react-dom"; |
3 | 3 | import { message, Button, Dropdown, Menu } from "antd";
|
4 | 4 | // import "./content.css";
|
5 |
| -import { copyToClipboard, 不讲武德, getStorage } from "./utils"; |
| 5 | +import { |
| 6 | + copyToClipboard, |
| 7 | + 不讲武德, |
| 8 | + getStorage, |
| 9 | + setCloundStorage, |
| 10 | +} from "./utils"; |
6 | 11 | import zenAble from "./zen/zenMode";
|
7 |
| -import AccessToken from "./components/AccessToken"; |
| 12 | +// import AccessToken from "./components/AccessToken"; |
8 | 13 |
|
9 |
| -class SolutionButton extends PureComponent { |
10 |
| - static propTypes = {}; |
| 14 | +// class SolutionButton extends PureComponent { |
| 15 | +// static propTypes = {}; |
11 | 16 |
|
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 | +// } |
49 | 24 |
|
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 | +// } |
115 | 59 |
|
116 | 60 | // if (testCase[i] === '"') {
|
117 | 61 | // while (i < testCase.length && testCase[i] !== '"') {
|
@@ -237,9 +181,75 @@ function insertButton() {
|
237 | 181 | });
|
238 | 182 | };
|
239 | 183 | buttons[i].parentElement.prepend(copyButton);
|
240 |
| - const writeSolutionButton = document.createElement("div"); |
241 | 184 |
|
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); |
243 | 253 |
|
244 | 254 | buttons[i].parentElement.prepend(writeSolutionButton);
|
245 | 255 | return true;
|
|
0 commit comments