Skip to content

Commit 59fcb64

Browse files
author
robot
committed
feat: 提交后默认隐藏失败的测试用例
1 parent 0c57a11 commit 59fcb64

File tree

3 files changed

+107
-7
lines changed

3 files changed

+107
-7
lines changed

‎public/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "leetcode cheatsheet",
44
"description": "刷题小助手,made by 力扣加加",
5-
"version": "0.9.5",
5+
"version": "0.10.0",
66
"browser_action": {
77
"default_popup": "index.html",
88
"default_title": "力扣加加"

‎src/contentScript.js

+56-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
// addStyle,
1313
} from "./utils";
1414
import zenAble from "./zen/zenMode";
15+
import hideFailCases from "./submission/hideFailCases";
1516

1617
// WTF! ant message didn't go well with chrome extension?
1718
const message = {
@@ -339,8 +340,7 @@ int main()
339340
window.open(
340341
`https://pythontutor.com/visualize.html#code=${encodeURIComponent(
341342
code
342-
)}&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=${
343-
languageMap[language]
343+
)}&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=${languageMap[language]
344344
}&rawInputLstJSON=%5B%5D&textReferences=false`
345345
);
346346
}
@@ -349,6 +349,7 @@ function insertButton() {
349349
const buttons = document.querySelectorAll("button");
350350
for (var i = 0; i < buttons.length; ++i) {
351351
if (buttons[i].innerText.includes("执行代码")) {
352+
352353
// const container = document.createElement("div");
353354

354355
// buttons[i].parentElement.prepend(container);
@@ -475,13 +476,33 @@ function insertButton() {
475476
visDebugButton.onclick = goToVisDebug;
476477

477478
buttons[i].parentElement.prepend(visDebugButton);
478-
return true;
479+
inserted = true;
480+
} else if (buttons[i].innerText.includes("提交")) {
481+
const click = buttons[i].onclick
482+
buttons[i].onclick = (...args) => {
483+
click.call(buttons[i], ...args);
484+
485+
// try to hide failed test cases
486+
let tries = 0;
487+
const hideFailCasesJob = setInterval(() => {
488+
if (hideFailCases()) {
489+
clearInterval(hideFailCasesJob);
490+
}
491+
// 300 times means 30s
492+
if (tries > 300) return;
493+
tries++;
494+
}, 100)
495+
}
496+
497+
498+
submitProxied = true
479499
}
480500
}
481501
return false;
482502
}
483503
let inserted = false;
484504
let retried = 0;
505+
let submitProxied = false
485506
const MAX_TRY = 10;
486507

487508
// 去除智能提示
@@ -492,14 +513,14 @@ const MAX_TRY = 10;
492513
// }
493514
// `);
494515
const timerId = setInterval(() => {
495-
if (inserted) return clearInterval(timerId);
516+
if (inserted && submitProxied) return clearInterval(timerId);
496517
if (retried > MAX_TRY) {
497518
clearInterval(timerId);
498519
return console.error("初始化 chrome 插件 content script 失败");
499520
}
500-
if (insertButton()) {
521+
insertButton()
522+
if (inserted && submitProxied) {
501523
window.location.title = "";
502-
inserted = true;
503524
// 可进入禅定模式
504525
zenAble();
505526
}
@@ -521,3 +542,32 @@ const timerId = setInterval(() => {
521542
// document.body.appendChild(app);
522543

523544
// ReactDOM.render(<Main />, app);
545+
546+
547+
548+
549+
// history.pushState = (f => function pushState() {
550+
// var ret = f.apply(this, arguments);
551+
// window.dispatchEvent(new Event('pushstate'));
552+
// window.dispatchEvent(new Event('locationchange'));
553+
// return ret;
554+
// })(history.pushState);
555+
556+
// history.replaceState = (f => function replaceState() {
557+
// var ret = f.apply(this, arguments);
558+
// window.dispatchEvent(new Event('replacestate'));
559+
// window.dispatchEvent(new Event('locationchange'));
560+
// return ret;
561+
// })(history.replaceState);
562+
563+
// window.addEventListener('popstate', () => {
564+
// window.dispatchEvent(new Event('locationchange'))
565+
// });
566+
567+
// window.addEventListener('locationchange', function (e) {
568+
// const url = e.target.location.href;
569+
// console.log('hideFailCases')
570+
// if (url.endsWith("submissions/")) {
571+
572+
// }
573+
// })

‎src/submission/hideFailCases.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
function hideFailCases() {
3+
const submissionDOM = document.querySelector("div[data-key=submissions-content]")
4+
const submmisionTabVisible = submissionDOM?.children.length > 0;
5+
if (submmisionTabVisible) {
6+
const result = submissionDOM.querySelector("div[data-cypress=SubmissionSuccess]")
7+
debugger
8+
if (!result) {
9+
return false
10+
}
11+
const hasError = result.getAttribute("type") === "error"
12+
if (!hasError) return true
13+
const input = submissionDOM.children[0].children[0].children[0].children[0].children[2]
14+
const output = submissionDOM.children[0].children[0].children[0].children[0].children[3]
15+
const expect = submissionDOM.children[0].children[0].children[0].children[0].children[4]
16+
input.style.display = "none"
17+
output.style.display = "none"
18+
expect.style.display = "none"
19+
const showFailCases = document.createElement("div");
20+
const showFailCasesTip = document.createElement("span")
21+
showFailCasesTip.innerText = "请先尝试自己解决,如果实在无法解决再尝试显示失败的测试用例!";
22+
showFailCasesTip.style["margin-right"] = "20px";
23+
showFailCasesTip.style["line-height"] = "32px";
24+
25+
const showFailCasesButton = document.createElement("a")
26+
showFailCasesButton.innerText = "点击显示失败的测试用例"
27+
showFailCasesButton.style["margin-right"] = "20px";
28+
showFailCasesButton.style["line-height"] = "32px";
29+
30+
showFailCasesButton.onclick = () => {
31+
input.style.display = "block"
32+
output.style.display = "block"
33+
expect.style.display = "block"
34+
35+
};
36+
37+
showFailCases.appendChild(showFailCasesTip);
38+
showFailCases.appendChild(showFailCasesButton);
39+
40+
submissionDOM.children[0].children[0].children[0].children[0].insertBefore(showFailCases, input)
41+
return true
42+
}
43+
return false
44+
}
45+
46+
47+
48+
export default function run() {
49+
return hideFailCases();
50+
}

0 commit comments

Comments
 (0)