-
Notifications
You must be signed in to change notification settings - Fork 1
/
trainer-get-tabs-code.js
46 lines (40 loc) · 1.19 KB
/
trainer-get-tabs-code.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
function getCodeEditors() {
const result = [];
const codeEditors = document.querySelectorAll('.CodeMirror');
codeEditors.forEach(editor => {
result.push(editor.CodeMirror);
});
return result;
}
function getTabCode(editor) {
return editor.getDoc().getValue();
}
function getTabIndex() {
return window.tabIndex;
}
function saveTabCodeToHiddenDiv(tabCode, index) {
const hiddenDiv = document.createElement('div');
hiddenDiv.style.display = "none";
hiddenDiv.classList.add("tab-code-" + index);
hiddenDiv.appendChild(document.createTextNode(tabCode));
document.body.appendChild(hiddenDiv);
}
function cleanUp() {
window.tabIndex = undefined;
document.querySelectorAll('.injectScript').forEach(e => e.remove());
}
function execute() {
const index = getTabIndex();
const codeEditors = getCodeEditors();
if (index !== undefined) {
const tabCode = getTabCode(codeEditors[index]);
saveTabCodeToHiddenDiv(tabCode, index);
} else {
codeEditors.forEach((editor, index) => {
const tabCode = getTabCode(editor);
saveTabCodeToHiddenDiv(tabCode, index);
});
}
cleanUp();
}
execute();