Skip to content

Commit 4dba047

Browse files
author
lucifer
committed
feat: 代码和题目描述自动带入
1 parent 95e6de2 commit 4dba047

File tree

2 files changed

+63
-19
lines changed

2 files changed

+63
-19
lines changed

‎src/contentScript.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,35 @@ function insertButton() {
137137
const title = d.match(/(\d+\. .+)(?=<)/)[1];
138138
const link = window.location.origin + d.match(/href="(.*?)"/)[1];
139139
const language = document.querySelector("#lang-select").innerText;
140-
// let code = document.querySelector(
141-
// ".monaco-scrollable-element,.editor-scrollable"
142-
// ).innerText;
140+
let code = document.querySelector(
141+
".monaco-scrollable-element,.editor-scrollable"
142+
).innerText;
143143

144-
// const desc = document.querySelector("#question-detail-main-tabs")
145-
// .children[1].children[0].children[1].innerText;
146-
window.open(
147-
`https://leetcode-pp.github.io/leetcode-cheat/?link=${link}&title=${title}&language=${language}&tab=solution-template`
148-
);
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+
});
149169
};
150170

151171
buttons[i].parentElement.prepend(writeSolutionButton);

‎src/solutionTemplate/index.jsx

+35-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useState, useEffect } from "react";
22
import {
33
Radio,
44
Row,
@@ -182,10 +182,6 @@ const formulas = [
182182
logo: require("../imgs/formula/sqrt.svg"),
183183
},
184184
];
185-
const link = getUrlParameter("link") || "";
186-
const title = getUrlParameter("title") || "";
187-
const code = getUrlParameter("code") || "";
188-
const initialLanguage = getUrlParameter("language")?.toLowerCase() || "python3";
189185

190186
const saveDraft = debounce(
191187
(v) =>
@@ -196,7 +192,7 @@ const saveDraft = debounce(
196192
);
197193

198194
export default function SolutionTemplate() {
199-
const [language, setLanguage] = useState(initialLanguage);
195+
const [language, setLanguage] = useState("");
200196
const [time, setTime] = useState("n");
201197
const [space, setSpace] = useState("n");
202198
const [isLucifer, setIsLucifer] = useState(false);
@@ -205,11 +201,39 @@ export default function SolutionTemplate() {
205201
time,
206202
space,
207203
language,
208-
link,
209-
title,
210-
code,
211204
})
212205
);
206+
useEffect(() => {
207+
fetch(
208+
`https://api.github.com/repos/azl397985856/stash/issues/${getUrlParameter(
209+
"issue_number"
210+
)}`,
211+
{
212+
headers: {
213+
accept: "application/json",
214+
"Content-Type": "application/json",
215+
Authorization: "token f4fed02e90310c97be2c51b5b97b1f5b3ad91a23",
216+
},
217+
}
218+
)
219+
.then((res) => res.json())
220+
.then((res) => JSON.parse(res.body))
221+
.then((res) => {
222+
const { link, title, code, language, desc } = res;
223+
setLanguage(language?.toLowerCase());
224+
setTemplate(
225+
getTemplate({
226+
desc,
227+
time,
228+
space,
229+
language: language?.toLowerCase(),
230+
link,
231+
title,
232+
code,
233+
})
234+
);
235+
});
236+
}, []);
213237

214238
return (
215239
<>
@@ -220,8 +244,8 @@ export default function SolutionTemplate() {
220244
题解每五秒备份一次,如果你不小心刷新了浏览器可以点击下方的恢复按钮还原。由于是覆盖式备份,因此仅会保存最后一次编辑的内容。
221245
</li>
222246
<li>
223-
由于浏览器 url
224-
传参限制,题目信息和代码暂时不会自动带过来,后续考虑使用服务器转存实现自动带入的功能
247+
自动带入功能使用了 Github API,如果题目信息没有自动带入可能是
248+
Github API 调用次数限制。后���考虑搞个服务器给大家存放
225249
</li>
226250
<li>目前公式无法预览,原因暂时不明,不过后期会支持。</li>
227251
<li>后续考虑提供更多题解模板。</li>

0 commit comments

Comments
 (0)