-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (87 loc) · 2.35 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
let btn = document.createElement("button");
btn.innerText = "Format Code";
let isBtnPresent = false;
let monacoEditor = {};
function getMonaco() {
let monaco = window.monaco;
if (monaco === undefined || monaco === null) {
// codemirror not found on page
return;
}
monacoEditor = monaco.editor;
if (monacoEditor === undefined) {
// codeMirror not found
return;
}
}
const mutationObserver = new MutationObserver(() => {
getMonaco();
let btnParentDiv = document.querySelectorAll(".shrink-0");
let btnDiv2 =
btnParentDiv[btnParentDiv.length - 1].firstChild.firstChild.lastChild;
if (btnDiv2 === undefined || btnDiv2 === null) {
return;
}
let btnDiv = btnDiv2.childNodes[btnDiv2.childNodes.length - 2];
if (btnDiv === undefined || btnDiv === null) {
// buttons div not rendered yet
return;
}
let parent = btnDiv.parentElement;
let languageDiv = Array.from(document.querySelectorAll(".relative")).at(-1);
if (languageDiv === undefined || languageDiv === null) {
// language div not rendered yet
return;
}
if (
languageDiv.innerText === "Python" ||
languageDiv.innerText === "Python3"
) {
if (!isBtnPresent) {
parent.prepend(btn);
btn.className = btnDiv.className;
isBtnPresent = true;
}
} else {
if (isBtnPresent) {
let childList = parent.childNodes;
if (childList === undefined || childList === null) {
// children not loaded
return;
}
childList[0].remove();
isBtnPresent = false;
}
}
});
mutationObserver.observe(document, {
childList: true,
attributes: false,
subtree: true,
});
btn.addEventListener("click", () => {
const monacoModel = monacoEditor.getModels()[0];
const code = monacoModel.getValue();
const getFormattedCode = async () => {
try {
let formattedCode = await getData(code);
monacoModel.setValue(formattedCode);
} catch (e) {
console.log("Error while fetching formatted code");
console.log(e);
}
};
getFormattedCode();
});
const getData = async (finalCode) => {
let data = await fetch("https://leetcode-code-formatter.vercel.app/code", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ code: finalCode }),
});
data = await data.json();
return data.formattedCode;
};