|
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"; |
2 | 4 | // import "./content.css";
|
3 |
| -import { copyToClipboard, 不讲武德 } from "./utils"; |
| 5 | +import { copyToClipboard, 不讲武德, getStorage } from "./utils"; |
4 | 6 | 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 | +} |
5 | 103 |
|
6 | 104 | // if (testCase[i] === '"') {
|
7 | 105 | // while (i < testCase.length && testCase[i] !== '"') {
|
@@ -127,46 +225,9 @@ function insertButton() {
|
127 | 225 | });
|
128 | 226 | };
|
129 | 227 | 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); |
170 | 231 |
|
171 | 232 | buttons[i].parentElement.prepend(writeSolutionButton);
|
172 | 233 | return true;
|
|
0 commit comments