Skip to content

Commit b71145b

Browse files
author
robot
committed
feat: debug vis
1 parent 3e594f5 commit b71145b

File tree

2 files changed

+324
-35
lines changed

2 files changed

+324
-35
lines changed

src/contentScript.js

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
bjwd,
1010
getStorage,
1111
setCloundStorage,
12-
addStyle,
12+
// addStyle,
1313
} from "./utils";
1414
import zenAble from "./zen/zenMode";
1515

@@ -208,6 +208,119 @@ function getProviedTestCases(includeArray = true) {
208208
// content: "复制成功~",
209209
// });
210210
// }
211+
212+
function goToVisDebug() {
213+
const language = document.querySelector("#lang-select").innerText;
214+
const supportedLanguages = ["Python3", "JavaScript", "C++"];
215+
const languageMap = {
216+
Python3: "3",
217+
Java: "java",
218+
JavaScript: "js",
219+
"C++": "cpp_g%2B%2B9.3.0",
220+
C: "c_gcc9.3.0",
221+
};
222+
const prefixMap = {
223+
Python3: "",
224+
JavaScript: "",
225+
java: "",
226+
"C++": `
227+
#include <algorithm>
228+
#include <bitset>
229+
#include <complex>
230+
#include <deque>
231+
#include <exception>
232+
#include <fstream>
233+
#include <functional>
234+
#include <iomanip>
235+
#include <ios>
236+
#include <iosfwd>
237+
#include <iostream>
238+
#include <istream>
239+
#include <iterator>
240+
#include <limits>
241+
#include <list>
242+
#include <locale>
243+
#include <map>
244+
#include <memory>
245+
#include <new>
246+
#include <numeric>
247+
#include <ostream>
248+
#include <queue>
249+
#include <set>
250+
#include <sstream>
251+
#include <stack>
252+
#include <stdexcept>
253+
#include <streambuf>
254+
#include <string>
255+
#include <typeinfo>
256+
#include <utility>
257+
#include <valarray>
258+
#include <vector>
259+
260+
#if __cplusplus >= 201103L
261+
#include <array>
262+
#include <atomic>
263+
#include <chrono>
264+
#include <condition_variable>
265+
#include <forward_list>
266+
#include <future>
267+
#include <initializer_list>
268+
#include <mutex>
269+
#include <random>
270+
#include <ratio>
271+
#include <regex>
272+
#include <scoped_allocator>
273+
#include <system_error>
274+
#include <thread>
275+
#include <tuple>
276+
#include <typeindex>
277+
#include <type_traits>
278+
#include <unordered_map>
279+
#include <unordered_set>
280+
#endif
281+
282+
using namespace std;
283+
`,
284+
C: "",
285+
};
286+
const suffixMap = {
287+
Python3: `
288+
# 替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试
289+
Solution().xxx(yyy)
290+
`,
291+
JavaScript: `
292+
// 替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试
293+
xxx(yyy)
294+
`,
295+
"C++": `
296+
int main()
297+
{
298+
Solution s;
299+
// 替换下方的 xxx 为主函数名, yyy 为测试用例参数开启调试
300+
s.xxx(yyy);
301+
return 0;
302+
}
303+
`,
304+
C: "",
305+
Java: "",
306+
};
307+
if (!supportedLanguages.includes(language))
308+
return message.warn({
309+
content: `当前仅支持 ${supportedLanguages.join(",")}`,
310+
});
311+
const code =
312+
(prefixMap[language] || "") +
313+
window?.monaco?.editor?.getModels()[0]?.getValue() +
314+
(suffixMap[language] || "");
315+
window.open(
316+
`https://pythontutor.com/visualize.html#code=${encodeURIComponent(
317+
code
318+
)}&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=${
319+
languageMap[language]
320+
}&rawInputLstJSON=%5B%5D&textReferences=false`
321+
);
322+
}
323+
211324
function insertButton() {
212325
const buttons = document.querySelectorAll("button");
213326
for (var i = 0; i < buttons.length; ++i) {
@@ -329,6 +442,15 @@ function insertButton() {
329442

330443
buttons[i].parentElement.prepend(writeSolutionButton);
331444
// ele.appendChild(writeSolutionButton);
445+
446+
const visDebugButton = document.createElement("a");
447+
visDebugButton.innerText = "可视化调试";
448+
visDebugButton.style["margin-right"] = "20px";
449+
visDebugButton.style["line-height"] = "32px";
450+
451+
visDebugButton.onclick = goToVisDebug;
452+
453+
buttons[i].parentElement.prepend(visDebugButton);
332454
return true;
333455
}
334456
}

0 commit comments

Comments
 (0)