Skip to content

Commit 4d55301

Browse files
author
lucifer
committed
chore: 题解模板小优化
1 parent ef54032 commit 4d55301

File tree

2 files changed

+71
-46
lines changed

2 files changed

+71
-46
lines changed

‎src/contentScript.js

+39-21
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,23 @@ function insertButton() {
183183
buttons[i].parentElement.prepend(copyButton);
184184

185185
// const writeSolutionButton = document.createElement("div");
186-
const writeSolutionButton = buttons[i].cloneNode(true);
187-
writeSolutionButton.innerText = "写题解";
188-
writeSolutionButton.style["margin-left"] = "10px";
186+
const writeSolutionButton = document.createElement("a");
187+
writeSolutionButton.innerText = "去写题解";
188+
writeSolutionButton.style["margin-right"] = "20px";
189+
writeSolutionButton.style["line-height"] = "32px";
190+
let ele = document.querySelector(`[data-cypress="QuestionTitle"]`);
189191

190192
writeSolutionButton.onclick = () => {
191193
// d: "<a href="/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/">1579. 保证图可完全遍历</a>"
192-
const ele = document.querySelector(`[data-cypress="QuestionTitle"]`);
194+
if (!ele) {
195+
ele = document.querySelector(`[data-cypress="QuestionTitle"]`);
196+
}
193197
if (!ele) {
194198
return message.warn({
195199
content: "获取题目描述失败,请先切换到题目描述标签",
196200
});
197201
}
198-
const d = document.querySelector(`[data-cypress="QuestionTitle"]`)
199-
.innerHTML;
202+
const d = ele.innerHTML;
200203
const title = d.match(/(\d+\. .+)(?=<)/)[1];
201204
const link = window.location.origin + d.match(/href="(.*?)"/)[1];
202205
const language = document.querySelector("#lang-select").innerText;
@@ -206,6 +209,13 @@ function insertButton() {
206209

207210
const desc = document.querySelector("#question-detail-main-tabs")
208211
.children[1].children[0].children[1].innerText;
212+
const hide = message.loading("正在存储题目信息,请稍后~", 0);
213+
writeSolutionButton.setAttribute("disabled", true);
214+
// Dismiss manually and asynchronously
215+
setTimeout(() => {
216+
hide();
217+
writeSolutionButton.removeAttribute("disabled");
218+
}, 30000); // 超时 30s 都没好,那就别转了
209219
getStorage("leetcode-cheatsheet-token")
210220
.then((res) => res.result.value)
211221
.then((res) => {
@@ -229,29 +239,37 @@ function insertButton() {
229239
{
230240
token: t,
231241
}
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(() => {
242+
)
243+
.then((res) => {
244+
hide();
245+
writeSolutionButton.removeAttribute("disabled");
246+
if (res.id) {
243247
window.open(
244-
`https://leetcode-pp.github.io/leetcode-cheat/?title=${title}&link=${link}&language=${language}&tab=solution-template`
248+
`https://leetcode-pp.github.io/leetcode-cheat/?issue_number=${res.id}&tab=solution-template`
245249
);
246-
}, 2000);
247-
}
248-
});
250+
} else {
251+
message.warn({
252+
content:
253+
"使用 Github API 失败,已为您切换为普通模式,普通模式仅可自动带入题目名称,题目地址以及题解语言。",
254+
});
255+
setTimeout(() => {
256+
window.open(
257+
`https://leetcode-pp.github.io/leetcode-cheat/?title=${title}&link=${link}&language=${language}&tab=solution-template`
258+
);
259+
}, 2000);
260+
}
261+
})
262+
.catch(() => {
263+
hide();
264+
writeSolutionButton.removeAttribute("disabled");
265+
});
249266
});
250267
};
251268

252269
// ReactDOM.render(<SolutionButton />, writeSolutionButton);
253270

254271
buttons[i].parentElement.prepend(writeSolutionButton);
272+
// ele.appendChild(writeSolutionButton);
255273
return true;
256274
}
257275
}

‎src/solutionTemplate/index.jsx

+32-25
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ const { TextArea } = Input;
2828
const { Option } = Select;
2929

3030
const slogan = `
31-
更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。
31+
32+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
33+
34+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
35+
36+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
3237
3338
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
3439
@@ -60,7 +65,7 @@ function getTemplate({
6065
link = "",
6166
desc = "",
6267
pre = "",
63-
company = "",
68+
company = "暂无",
6469
language = "python3",
6570
code = "",
6671
keyword = "",
@@ -257,31 +262,33 @@ export default class SolutionTemplate extends PureComponent {
257262
this.setState({
258263
isloading: true,
259264
});
260-
getCloundStorage(getUrlParameter("issue_number"), {
261-
token: t,
262-
})
263-
.then((res) => {
264-
const { link, title, code, language, desc } = res;
265-
266-
this.setLanguage(language?.toLowerCase());
267-
this.setTemplate(
268-
getTemplate({
269-
desc,
270-
language: language?.toLowerCase(),
271-
link,
272-
title,
273-
code,
274-
})
275-
);
276-
this.setState({
277-
isloading: false,
278-
});
265+
if (getUrlParameter("issue_number")) {
266+
getCloundStorage(getUrlParameter("issue_number"), {
267+
token: t,
279268
})
280-
.catch(() =>
281-
this.setState({
282-
isloading: false,
269+
.then((res) => {
270+
const { link, title, code, language, desc } = res;
271+
272+
this.setLanguage(language?.toLowerCase());
273+
this.setTemplate(
274+
getTemplate({
275+
desc,
276+
language: language?.toLowerCase(),
277+
link,
278+
title,
279+
code,
280+
})
281+
);
282+
this.setState({
283+
isloading: false,
284+
});
283285
})
284-
);
286+
.catch(() =>
287+
this.setState({
288+
isloading: false,
289+
})
290+
);
291+
}
285292
});
286293
}
287294
render() {

0 commit comments

Comments
 (0)