Skip to content

feat: 优化插入按钮样式 #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
"mkdirp": "^1.0.4",
"react-app-rewired": "^2.1.7"
}
}
}
61 changes: 35 additions & 26 deletions src/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,29 +340,40 @@ int main()
window.open(
`https://pythontutor.com/visualize.html#code=${encodeURIComponent(
code
)}&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=${languageMap[language]
)}&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=${
languageMap[language]
}&rawInputLstJSON=%5B%5D&textReferences=false`
);
}

function getCodeLanguage() {
const langMap = {
"cpp": "C++",
"python3": "Python3",
"java": "Java",
"c": "C",
"javascript": "JavaScript",
}
const l =window?.monaco?.editor?.getModels()[0]?.getLanguageId() ||
localStorage.getItem("global_lang")

return langMap[l.toLowerCase()]
cpp: "C++",
python3: "Python3",
java: "Java",
c: "C",
javascript: "JavaScript",
};
const l =
window?.monaco?.editor?.getModels()[0]?.getLanguageId() ||
localStorage.getItem("global_lang");

return langMap[l.toLowerCase()];
}
function insertButton() {
const customBtnStyle = {
"line-height": "1",
cursor: "pointer",
"vertical-align": "text-top",
};
const buttons = document.querySelectorAll("button");

for (var i = 0; i < buttons.length; ++i) {
if (buttons[i].innerText.includes("运行")) {

// 停止观察器
// observer.disconnect();

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

// buttons[i].parentElement.prepend(container);
Expand Down Expand Up @@ -390,8 +401,8 @@ function insertButton() {
// buttons[i].parentElement.prepend(copyButton);
const writeSolutionButton = document.createElement("a");
writeSolutionButton.innerText = "写题解";
writeSolutionButton.style["margin-right"] = "20px";
writeSolutionButton.style["line-height"] = "32px";
Object.assign(writeSolutionButton.style, customBtnStyle);
writeSolutionButton.className = buttons[i].className;

writeSolutionButton.onclick = () => {
// d: "<a href="/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/">1579. 保证图可完全遍历</a>"
Expand Down Expand Up @@ -474,17 +485,17 @@ function insertButton() {

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

buttons[i].parentElement.prepend(writeSolutionButton);
buttons[i].parentElement.parentElement.prepend(writeSolutionButton);
// ele.appendChild(writeSolutionButton);

const visDebugButton = document.createElement("a");
visDebugButton.innerText = "可视化调试";
visDebugButton.style["margin-right"] = "20px";
visDebugButton.style["line-height"] = "32px";
Object.assign(visDebugButton.style, customBtnStyle);
visDebugButton.className = buttons[i].className;

visDebugButton.onclick = goToVisDebug;

buttons[i].parentElement.prepend(visDebugButton);
buttons[i].parentElement.parentElement.prepend(visDebugButton);
inserted = true;
} else if (buttons[i].innerText.includes("提交")) {
const click = buttons[i].onclick;
Expand All @@ -500,18 +511,17 @@ function insertButton() {
// 300 times means 30s
if (tries > 300) return;
tries++;
}, 100)
}

}, 100);
};

submitProxied = true
submitProxied = true;
}
}
return false;
}
let inserted = false;
let retried = 0;
let submitProxied = false
let submitProxied = false;
const MAX_TRY = 10;

// 去除智能提示
Expand All @@ -521,13 +531,15 @@ const MAX_TRY = 10;
// visibility: hidden !important;
// }
// `);

const timerId = setInterval(() => {
if (inserted && submitProxied) return clearInterval(timerId);
if (retried > MAX_TRY) {
clearInterval(timerId);
return console.error("初始化 chrome 插件 content script 失败");
}
insertButton();

// if (inserted && submitProxied) {
// window.location.title = "";
// // 可进入禅定模式
Expand All @@ -552,9 +564,6 @@ const timerId = setInterval(() => {

// ReactDOM.render(<Main />, app);




// history.pushState = (f => function pushState() {
// var ret = f.apply(this, arguments);
// window.dispatchEvent(new Event('pushstate'));
Expand All @@ -579,4 +588,4 @@ const timerId = setInterval(() => {
// if (url.endsWith("submissions/")) {

// }
// })
// })