-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontent.js
124 lines (110 loc) · 5.13 KB
/
content.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
console.log('👨💻 Author: Saurav Hathi \n🌟 GitHub: https://github.com/sauravhathi');
if (window.location.href.includes("codetantra.com/secure/topic-details1")) {
const myModal = document.getElementById("myModal");
const waitForModalToShow = () => {
return new Promise((resolve) => {
const checkModal = () => {
const isModalShown = myModal.getAttribute("aria-modal") === "true";
if (isModalShown) {
resolve();
} else {
setTimeout(checkModal, 100);
}
};
checkModal();
});
};
waitForModalToShow()
.then(() => {
const ctButtonBar = document.querySelector('div#ct-button-bar div#buttonsDiv');
const createButton = (innerText, title, clickHandler) => {
const button = document.createElement("span");
button.classList.add("btn", "btn-outline-info", "btn-sm", "mx-1");
button.setAttribute("type", "button");
button.setAttribute("title", title);
button.innerText = innerText;
button.addEventListener("click", clickHandler);
return button;
};
const useToastr = (message, type = "success") => {
const div = document.createElement("div");
const text = document.createTextNode(message);
div.appendChild(text);
const styleList = [
"position: fixed",
"bottom: 1%",
"left: 1%",
"transform: translateY(100%)",
"padding: 1rem", ,
"background-color: " + (type === "success" ? "#350054" : "#ff0000"),
"color: " + (type === "success" ? "#fff" : "#fff"),
"border-radius: 0.25rem",
"box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.5)",
"opacity: 0",
"transition: all 0.5s ease-in-out",
"z-index: 9999",
];
div.style.cssText = styleList.join(";");
document.body.appendChild(div);
setTimeout(() => {
div.style.opacity = "1";
div.style.transform = "translateY(0)";
}, 1);
setTimeout(() => {
div.style.opacity = "0";
div.style.transform = "translateY(-100%)";
setTimeout(() => {
document.body.removeChild(div);
}, 500);
}, 2000);
};
const copy = (text, successMessage) => {
navigator.clipboard.writeText(text);
useToastr(successMessage);
};
const copyTestCasesButton = createButton("Copy 🧪", "Click to copy test cases", () => {
try {
const testCases = document.querySelector('#compilationErrorsDiv #executionResultTablesContainerDiv');
if (testCases === null) {
throw new Error("Test cases not found!");
}
const testCasesText = testCases.innerText;
copy(testCasesText, "Test cases copied successfully!");
} catch (error) {
useToastr(error.message, "error");
}
});
const copyCodeButton = createButton("Copy 🧑💻", "Click to copy your code", () => {
try {
const labelElement = document.querySelector('div.toggle.btn.btn-xs');
if (labelElement.classList.contains('btn-primary')) {
labelElement.click();
}
const code = document.querySelector('div.tab-content div.tab-pane.active div.ace_layer.ace_text-layer');
if (code === null) {
throw new Error("Code not found!");
}
setTimeout(() => {
const codeText = code.innerText;
copy(codeText, "Code copied successfully!");
}, 1);
} catch (error) {
useToastr(error.message, "error");
}
});
const copyQuestionButton = createButton("Copy 📝", "Click to copy question", () => {
try {
const question = document.getElementById('questionInfoDiv');
if (question === null) {
throw new Error("Question not found!");
}
copy(question.innerText, "Question copied successfully!");
} catch (error) {
useToastr(error.message, "error");
}
});
ctButtonBar.insertBefore(copyTestCasesButton, ctButtonBar.firstChild);
ctButtonBar.insertBefore(copyCodeButton, ctButtonBar.firstChild);
ctButtonBar.insertBefore(copyQuestionButton, ctButtonBar.firstChild);
});
}