Skip to content

Commit ab6aec0

Browse files
committed
Merge branch 'fix-contentScript'
2 parents 37a8256 + 4087ea4 commit ab6aec0

File tree

4 files changed

+120
-85
lines changed

4 files changed

+120
-85
lines changed

src/contentScript.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {
1212
} from "./utils";
1313
// import zenAble from "./zen/zenMode";
1414
import hideFailCases from "./submission/hideFailCases";
15+
import { t as tt } from "./locales";
16+
let documentLang = document.documentElement.lang;
17+
const t = (keypath, slotText) => tt(keypath, slotText, documentLang);
1518

1619
// WTF! ant message didn't go well with chrome extension?
1720
const message = {
@@ -221,7 +224,7 @@ function goToVisDebug() {
221224
};
222225
const prefixMap = {
223226
Python3: `
224-
# 如何你在调试链表题目,手动生成链表很麻烦,想快速生成链表可以注释如下方法,并使用如下方法,输入一个数组,返回一个链表
227+
# ${t("app.linkedListCommnet")}
225228
# eg:head = ListNodes([4,2,1,3]).head
226229
# class ListNodes:
227230
# def __init__(self, vals)->ListNode:
@@ -233,7 +236,7 @@ function goToVisDebug() {
233236
# self.head = dummy.next
234237
`,
235238
JavaScript: `
236-
// 如何你在调试链表题目,手动生成链表很麻烦,想快速生成链表可以注释如下方法,并使用如下方法,输入一个数组,返回一个链表
239+
// ${t("app.linkedListCommnet")}
237240
// eg:head = ListNodes([4,2,1,3]).head
238241
// function ListNodes(vals) {
239242
// let cur = new ListNode()
@@ -309,18 +312,18 @@ using namespace std;
309312
};
310313
const suffixMap = {
311314
Python3: `
312-
# 替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试
315+
# ${t("app.visualDebugComment")}
313316
Solution().xxx(yyy)
314317
`,
315318
JavaScript: `
316-
// 替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试
319+
// ${t("app.visualDebugComment")}
317320
xxx(yyy)
318321
`,
319322
"C++": `
320323
int main()
321324
{
322325
Solution s;
323-
// 替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试
326+
// ${t("app.visualDebugComment")}
324327
s.xxx(yyy);
325328
return 0;
326329
}
@@ -330,7 +333,7 @@ int main()
330333
};
331334
if (!supportedLanguages.includes(language))
332335
return message.warn({
333-
content: `当前仅支持 ${supportedLanguages.join(",")}`,
336+
content: `${t("app.visualDebugSupport")} ${supportedLanguages.join(",")}`,
334337
});
335338
const code =
336339
(prefixMap[language] || "") +
@@ -347,17 +350,17 @@ int main()
347350

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

363+
return langMap[l.toLowerCase()];
361364
}
362365
function insertButton() {
363366
const customBtnStyle = {
@@ -368,10 +371,7 @@ function insertButton() {
368371
const buttons = document.querySelectorAll("button");
369372

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

376376
// const container = document.createElement("div");
377377

@@ -399,7 +399,7 @@ function insertButton() {
399399
// };
400400
// buttons[i].parentElement.prepend(copyButton);
401401
const writeSolutionButton = document.createElement("a");
402-
writeSolutionButton.innerText = "写题解";
402+
writeSolutionButton.innerText = t("Locale.app.wirteSolution");
403403
Object.assign(writeSolutionButton.style, customBtnStyle);
404404
writeSolutionButton.className = buttons[i].className;
405405

@@ -412,7 +412,7 @@ function insertButton() {
412412

413413
if (!desc) {
414414
return message.warn({
415-
content: "获取题目描述失败,请先切换到题目描述标签",
415+
content: t("app.getProblemError"),
416416
});
417417
}
418418
const title = document.title;
@@ -425,7 +425,7 @@ function insertButton() {
425425

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

428-
const hide = message.loading("正在存储题目信息,请稍后~", 0);
428+
const hide = message.loading(t("app.savingProblem"), 0);
429429
writeSolutionButton.setAttribute("disabled", true);
430430
// Dismiss manually and asynchronously
431431
setTimeout(() => {
@@ -465,8 +465,7 @@ function insertButton() {
465465
);
466466
} else {
467467
message.warn({
468-
content:
469-
"使用 Github API 失败,已为您切换为普通模式,普通模式仅可自动带入题目名称,题目地址以及题解语言。",
468+
content: t("app.githubAPIError"),
470469
});
471470
setTimeout(() => {
472471
window.open(
@@ -483,20 +482,24 @@ function insertButton() {
483482
};
484483

485484
// ReactDOM.render(<SolutionButton />, writeSolutionButton);
486-
487-
buttons[i].parentElement.parentElement.prepend(writeSolutionButton);
488485
// ele.appendChild(writeSolutionButton);
489486

490487
const visDebugButton = document.createElement("a");
491-
visDebugButton.innerText = "可视化调试";
488+
visDebugButton.innerText = t("Locale.app.visualizationDebug");
492489
Object.assign(visDebugButton.style, customBtnStyle);
493490
visDebugButton.className = buttons[i].className;
494491

495492
visDebugButton.onclick = goToVisDebug;
496-
497-
buttons[i].parentElement.parentElement.prepend(visDebugButton);
493+
if (documentLang === "en") {
494+
buttons[i].parentElement.prepend(visDebugButton);
495+
buttons[i].parentElement.prepend(writeSolutionButton);
496+
} else {
497+
buttons[i].parentElement.parentElement.prepend(writeSolutionButton);
498+
499+
buttons[i].parentElement.parentElement.prepend(visDebugButton);
500+
}
498501
inserted = true;
499-
} else if (buttons[i].innerText.includes("提交")) {
502+
} else if (buttons[i].innerText.includes(t("app.submit"))) {
500503
const click = buttons[i].onclick;
501504
const originalFn = buttons[i]
502505
buttons[i].onclick = (...args) => {
@@ -536,7 +539,7 @@ const timerId = setInterval(() => {
536539
if (inserted && submitProxied) return clearInterval(timerId);
537540
if (retried > MAX_TRY) {
538541
clearInterval(timerId);
539-
return console.error("初始化 chrome 插件 content script 失败");
542+
return console.error(t("app.initializeContentScriptFailed"));
540543
}
541544
insertButton();
542545

src/locales/en.js

Lines changed: 58 additions & 43 deletions
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

Lines changed: 12 additions & 9 deletions
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";
6-
// const LEETCODE_URL_EN = "https://leetcode.com";
7-
const DEFAULT_LANG = "cn";
6+
const LEETCODE_URL_EN = "https://leetcode.com";
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

Lines changed: 15 additions & 1 deletion
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)