Skip to content

Commit 35c3dfe

Browse files
committed
feat: support insert button on en website
1 parent 5ddd4f6 commit 35c3dfe

File tree

4 files changed

+120
-84
lines changed

4 files changed

+120
-84
lines changed

‎src/contentScript.js

+36-32
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import {
1313
} from "./utils";
1414
import zenAble from "./zen/zenMode";
1515
import hideFailCases from "./submission/hideFailCases";
16-
16+
import { t as tt, lang } from "./locales";
17+
let documentLang = document.documentElement.lang;
18+
const t = (keypath, slotText) => tt(keypath, slotText, documentLang);
19+
// console.log("lang", lang);
20+
// lang = document.documentElement.lang
1721
// WTF! ant message didn't go well with chrome extension?
1822
const message = {
1923
success({ content }) {
@@ -222,7 +226,7 @@ function goToVisDebug() {
222226
};
223227
const prefixMap = {
224228
Python3: `
225-
# 如何你在调试链表题目,手动生成链表很麻烦,想快速生成链表可以注释如下方法,并使用如下方法,输入一个数组,返回一个链表
229+
# ${t("app.linkedListCommnet")}
226230
# eg:head = ListNodes([4,2,1,3]).head
227231
# class ListNodes:
228232
# def __init__(self, vals)->ListNode:
@@ -234,7 +238,7 @@ function goToVisDebug() {
234238
# self.head = dummy.next
235239
`,
236240
JavaScript: `
237-
// 如何你在调试链表题目,手动生成链表很麻烦,想快速生成链表可以注释如下方法,并使用如下方法,输入一个数组,返回一个链表
241+
// ${t("app.linkedListCommnet")}
238242
// eg:head = ListNodes([4,2,1,3]).head
239243
// function ListNodes(vals) {
240244
// let cur = new ListNode()
@@ -310,18 +314,18 @@ using namespace std;
310314
};
311315
const suffixMap = {
312316
Python3: `
313-
# 替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试
317+
# ${t("app.visualDebugComment")}
314318
Solution().xxx(yyy)
315319
`,
316320
JavaScript: `
317-
// 替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试
321+
// ${t("app.visualDebugComment")}
318322
xxx(yyy)
319323
`,
320324
"C++": `
321325
int main()
322326
{
323327
Solution s;
324-
// 替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试
328+
// ${t("app.visualDebugComment")}
325329
s.xxx(yyy);
326330
return 0;
327331
}
@@ -331,7 +335,7 @@ int main()
331335
};
332336
if (!supportedLanguages.includes(language))
333337
return message.warn({
334-
content: `当前仅支持 ${supportedLanguages.join(",")}`,
338+
content: `${t("app.visualDebugSupport")} ${supportedLanguages.join(",")}`,
335339
});
336340
const code =
337341
(prefixMap[language] || "") +
@@ -348,17 +352,17 @@ int main()
348352

349353
function getCodeLanguage() {
350354
const langMap = {
351-
"cpp": "C++",
352-
"python3": "Python3",
353-
"java": "Java",
354-
"c": "C",
355-
"javascript": "JavaScript",
356-
}
357-
const l =window?.monaco?.editor?.getModels()[0]?.getLanguageId() ||
358-
localStorage.getItem("global_lang")
359-
360-
return langMap[l.toLowerCase()]
355+
cpp: "C++",
356+
python3: "Python3",
357+
java: "Java",
358+
c: "C",
359+
javascript: "JavaScript",
360+
};
361+
const l =
362+
window?.monaco?.editor?.getModels()[0]?.getLanguageId() ||
363+
localStorage.getItem("global_lang");
361364

365+
return langMap[l.toLowerCase()];
362366
}
363367
function insertButton() {
364368
const customBtnStyle = {
@@ -369,10 +373,7 @@ function insertButton() {
369373
const buttons = document.querySelectorAll("button");
370374

371375
for (var i = 0; i < buttons.length; ++i) {
372-
if (buttons[i].innerText.includes("运行")) {
373-
374-
// 停止观察器
375-
// observer.disconnect();
376+
if (buttons[i].innerText.includes(t("Locale.app.run"))) {
376377

377378
// const container = document.createElement("div");
378379

@@ -400,7 +401,7 @@ function insertButton() {
400401
// };
401402
// buttons[i].parentElement.prepend(copyButton);
402403
const writeSolutionButton = document.createElement("a");
403-
writeSolutionButton.innerText = "写题解";
404+
writeSolutionButton.innerText = t("Locale.app.wirteSolution");
404405
Object.assign(writeSolutionButton.style, customBtnStyle);
405406
writeSolutionButton.className = buttons[i].className;
406407

@@ -413,7 +414,7 @@ function insertButton() {
413414

414415
if (!desc) {
415416
return message.warn({
416-
content: "获取题目描述失败,请先切换到题目描述标签",
417+
content: t("app.getProblemError"),
417418
});
418419
}
419420
const title = document.title;
@@ -426,7 +427,7 @@ function insertButton() {
426427

427428
// const desc = document.querySelector("#question-detail-main-tabs")?.children[1]?.children[0]?.children[1]?.innerText;
428429

429-
const hide = message.loading("正在存储题目信息,请稍后~", 0);
430+
const hide = message.loading(t("app.savingProblem"), 0);
430431
writeSolutionButton.setAttribute("disabled", true);
431432
// Dismiss manually and asynchronously
432433
setTimeout(() => {
@@ -466,8 +467,7 @@ function insertButton() {
466467
);
467468
} else {
468469
message.warn({
469-
content:
470-
"使用 Github API 失败,已为您切换为普通模式,普通模式仅可��动带入题目名称,题目地址以及题解语言。",
470+
content: t("app.githubAPIError"),
471471
});
472472
setTimeout(() => {
473473
window.open(
@@ -484,20 +484,24 @@ function insertButton() {
484484
};
485485

486486
// ReactDOM.render(<SolutionButton />, writeSolutionButton);
487-
488-
buttons[i].parentElement.parentElement.prepend(writeSolutionButton);
489487
// ele.appendChild(writeSolutionButton);
490488

491489
const visDebugButton = document.createElement("a");
492-
visDebugButton.innerText = "可视化调试";
490+
visDebugButton.innerText = t("Locale.app.visualizationDebug");
493491
Object.assign(visDebugButton.style, customBtnStyle);
494492
visDebugButton.className = buttons[i].className;
495493

496494
visDebugButton.onclick = goToVisDebug;
495+
if (documentLang === "en") {
496+
buttons[i].parentElement.prepend(visDebugButton);
497+
buttons[i].parentElement.prepend(writeSolutionButton);
498+
} else {
499+
buttons[i].parentElement.parentElement.prepend(writeSolutionButton);
497500

498-
buttons[i].parentElement.parentElement.prepend(visDebugButton);
501+
buttons[i].parentElement.parentElement.prepend(visDebugButton);
502+
}
499503
inserted = true;
500-
} else if (buttons[i].innerText.includes("提交")) {
504+
} else if (buttons[i].innerText.includes(t("app.submit"))) {
501505
const click = buttons[i].onclick;
502506
buttons[i].onclick = (...args) => {
503507
click.call(buttons[i], ...args);
@@ -536,7 +540,7 @@ const timerId = setInterval(() => {
536540
if (inserted && submitProxied) return clearInterval(timerId);
537541
if (retried > MAX_TRY) {
538542
clearInterval(timerId);
539-
return console.error("初始化 chrome 插件 content script 失败");
543+
return console.error(t("app.initializeContentScriptFailed"));
540544
}
541545
insertButton();
542546

‎src/locales/en.js

+58-43
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,61 @@
11
export const en = {
2-
app: {
3-
back: "Back to Home page",
4-
viewSolution: "View solution",
5-
viewInHandpickCollection:"This question has been collected in the handpick collection《{slotText}》Click to view",
6-
notCollected: "This question has not been collected by LeetCode Plus, click to view all collected questions",
7-
allCollected: "All collected questions",
8-
writingExplanation: "Writing explanation",
9-
goToTheWebsiteToUse: "Go to the website to use",
10-
checkTips:
11-
"Generally, as long as you turn on automatic updates, chrome will automatically update within five hours after the plug-in is updated. If you want to update as soon as possible, or if you disable automatic updates, you can check the latest version here.",
12-
checkBtn: "Check for updates",
13-
selfIntroduction:
14-
"The author is a front-end architect with 40K stars on Github, the author of the leetcode-cheatsheet leetcode plugin, master all kinds of algorithm routines, and wrote hundreds of thousands of words of algorithm brushing e-books. Reply to the public account [电子书] to get.",
15-
allSolutions: {
16-
columns: {
17-
title: "Title",
18-
tag: "Tag",
19-
},
2+
app: {
3+
back: "Back to Home page",
4+
viewSolution: "View solution",
5+
viewInHandpickCollection:
6+
"This question has been collected in the handpick collection《{slotText}》Click to view",
7+
notCollected:
8+
"This question has not been collected by LeetCode Plus, click to view all collected questions",
9+
allCollected: "All collected questions",
10+
writingExplanation: "Writing explanation",
11+
goToTheWebsiteToUse: "Go to the website to use",
12+
checkTips:
13+
"Generally, as long as you turn on automatic updates, chrome will automatically update within five hours after the plug-in is updated. If you want to update as soon as possible, or if you disable automatic updates, you can check the latest version here.",
14+
checkBtn: "Check for updates",
15+
selfIntroduction:
16+
"The author is a front-end architect with 40K stars on Github, the author of the leetcode-cheatsheet leetcode plugin, master all kinds of algorithm routines, and wrote hundreds of thousands of words of algorithm brushing e-books. Reply to the public account [电子书] to get.",
17+
allSolutions: {
18+
columns: {
19+
title: "Title",
20+
tag: "Tag",
2021
},
2122
},
22-
codeTemplate: {
23-
name: "Code Template",
24-
},
25-
dataStructureVisualization: {
26-
name: "Data Structure Visualization",
27-
},
28-
29-
explanationTemplate: {
30-
name: "Explanation Template",
31-
},
32-
complexityQuickCheck: {
33-
name: "Complexity Quick Check",
34-
},
35-
learningRoute: {
36-
name: "Learning Route",
37-
},
38-
checkForUpdates: {
39-
name: "Check for Updates",
40-
},
41-
aboutMe: {
42-
name: "About Me",
43-
},
44-
};
45-
export default en;
46-
23+
initializeContentScriptFailed:"Failed to initialize the chrome plugin content script",
24+
run: "Run",
25+
submit: "Submit",
26+
wirteSolution: "Write Solution",
27+
visualizationDebug: "Visualization Debug",
28+
linkedListCommnet:
29+
"If you are debugging linked list questions, it is very troublesome to generate linked lists manually. If you want to quickly generate linked lists, you can comment out the following method and use the following method to enter an array and return a linked list.",
30+
visualDebugComment:
31+
"Replace xxx below with the main function name, yyy with the test case parameter, and turn on debugging",
32+
getProblemError:
33+
"Failed to get the problem description, please switch to the problem description tab first",
34+
savingProblem: "Saving problem information, please wait~",
35+
githubAPIError:
36+
"Failed to use Github API, has been switched to normal mode, normal mode can only automatically bring in the problem name, problem address and solution language.",
37+
},
38+
codeTemplate: {
39+
name: "Code Template",
40+
},
41+
dataStructureVisualization: {
42+
name: "Data Structure Visualization",
43+
},
44+
45+
explanationTemplate: {
46+
name: "Explanation Template",
47+
},
48+
complexityQuickCheck: {
49+
name: "Complexity Quick Check",
50+
},
51+
learningRoute: {
52+
name: "Learning Route",
53+
},
54+
checkForUpdates: {
55+
name: "Check for Updates",
56+
},
57+
aboutMe: {
58+
name: "About Me",
59+
},
60+
};
61+
export default en;

‎src/locales/index.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import cn from "./cn";
1+
import zh from "./zh";
22
import en from "./en";
33
// import { getStorage, setStorage } from "../utils";
44
// const STORAGE_LANG_KEY = "LEETCODE_CHEAT_LANG";
55
const LEETCODE_URL_CN = "https://leetcode.cn";
66
const LEETCODE_URL_EN = "https://leetcode.com";
7-
const DEFAULT_LANG = "cn";
7+
const DEFAULT_LANG = "zh";
88

9-
let lang = DEFAULT_LANG;
9+
export let lang = DEFAULT_LANG;
1010

1111
const ALL_LANGS = {
12-
cn,
12+
zh,
1313
en,
1414
};
1515

1616
export const ALL_LANG_OPTIONS = {
17-
cn: "简体中文",
17+
zh: "简体中文",
1818
en: "English",
1919
};
2020

2121
export const AllLangs = Object.keys(ALL_LANGS);
2222

2323
export const initLang = async (currentUrl) => {
2424
const isCnHref = currentUrl.includes(LEETCODE_URL_CN);
25-
setLang(isCnHref ? "cn" : "en");
25+
setLang(isCnHref ? "zh" : "en");
2626
};
2727

2828
export const setLang = (_lang) => {
@@ -38,9 +38,12 @@ const getForPath = (obj, path) => {
3838
return result;
3939
};
4040

41-
export const t = (keypath, slotText) => {
42-
const langData ={ Locale: ALL_LANGS[lang] }
41+
export const t = (keypath, slotText,l) => {
42+
const langData ={ Locale: ALL_LANGS[l||lang] }
4343
if (!keypath) return "";
44+
if (!keypath.includes("Locale")) {
45+
keypath = "Locale." + keypath
46+
}
4447
let content = getForPath(langData, keypath);
4548
if (slotText) {
4649
if (Array.isArray(slotText)) {

‎src/locales/cn.js renamed to ‎src/locales/zh.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const cn = {
22
app: {
33
back: "返回主页",
44
viewSolution: "查看本题题解",
5-
viewInHandpickCollection:"该题已被收录到精选合集《{slotText}》点击查看",
5+
viewInHandpickCollection: "该题已被收录到精选合集《{slotText}》点击查看",
66
notCollected: "本题暂未被力扣加加收录,点击查看所有已收录题目~",
77
allCollected: "所有已收录的题目",
88
writingExplanation: "正在撰写题解...",
@@ -18,6 +18,20 @@ export const cn = {
1818
tag: "标签",
1919
},
2020
},
21+
initializeContentScriptFailed:"初始化 chrome 插件 content script 失败",
22+
run: "运行",
23+
submit: "提交",
24+
wirteSolution: "写题解",
25+
visualizationDebug: "可视化调试",
26+
linkedListCommnet:
27+
"如果你在调试链表题目,手动生成链表很麻烦,想快速生成链表可以注释如下方法,并使用如下方法,输入一个数组,返回一个链表",
28+
visualDebugComment:
29+
"替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试",
30+
visualDebugSupport: "当前仅支持",
31+
getProblemError: "获取题目描述失败,请先切换到题目描述标签页",
32+
savingProblem: "正在存储题目信息,请稍后~",
33+
githubAPIError:
34+
"使用 Github API 失败,已为您切换为普通模式,普通模式仅可自动带入题目名称,题目地址以及题解语言。",
2135
},
2236
codeTemplate: {
2337
name: "代码模板",

0 commit comments

Comments
 (0)