forked from QasimWani/LeetHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfg.js
187 lines (168 loc) · 5.17 KB
/
gfg.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Enum for languages supported by GeeksForGeeks. */
// const gfgLanguages = {
// Python3: '.py',
// 'C++': '.cpp',
// Java: '.java',
// Javascript: '.js',
// };
// console.log("started gfg script");
/* Commit messages */
const README_MSG = 'Create README - LeetHub';
const SUBMIT_MSG = 'Added solution - LeetHub';
const UPDATE_MSG = 'Updated solution - LeetHub';
let START_MONITOR = true;
const toKebabCase = (string) => {
return string
.replace(/[^a-zA-Z0-9\. ]/g, '') //remove special chars
.replace(/([a-z])([A-Z])/g, '$1-$2') // get all lowercase letters that are near to uppercase ones
.replace(/[\s_]+/g, '-') // replace all spaces and low dash
.toLowerCase(); // convert to lower case
};
function findGfgLanguage() {
const ele = document.getElementsByClassName('filter-option')[0]
.innerText;
let lang = ele.split('(')[0].trim();
if (lang.length > 0 && languages[lang]) {
return languages[lang];
}
return null;
}
function findTitle() {
const ele = document.getElementsByClassName('problem-tab__name')[0]
.innerText;
if (ele != null) {
return ele;
}
return '';
}
function findDifficulty() {
const ele = document.getElementsByClassName(
'problem-tab__problem-level',
)[0].innerText;
if (ele != null) {
return ele;
}
return '';
}
function getProblemStatement() {
const ele = document.getElementsByClassName('problem-statement')[0];
return `${ele.outerHTML}`;
}
function getCode() {
let hackyScriptContent = `
console.log("trying to get editor content");
var editorContent = editor.getValue();
// console.log(editorContent);
var para = document.createElement("pre");
para.innerText+=editorContent;
para.setAttribute("id","codeDataLeetHub")
document.body.appendChild(para);
// console.log(para);
`;
// console.log(hackyScriptContent);
var script = document.createElement('script');
script.id = 'tmpScript';
script.appendChild(document.createTextNode(hackyScriptContent));
(
document.body ||
document.head ||
document.documentElement
).appendChild(script);
const text = document.getElementById('codeDataLeetHub').innerText;
let nodeDeletionScript = `
document.body.removeChild(para)
`;
var script = document.createElement('script');
script.id = 'tmpScript';
script.appendChild(document.createTextNode(nodeDeletionScript));
(
document.body ||
document.head ||
document.documentElement
).appendChild(script);
return text ? text : '';
}
const gfgLoader = setInterval(() => {
let code = null;
let problemStatement = null;
let title = null;
let language = null;
let difficulty = null;
if (
window.location.href.includes(
'practice.geeksforgeeks.org/problems',
)
) {
let submitBtn = document.getElementById('run');
// console.log("listening to events");
submitBtn.addEventListener('click', function () {
START_MONITOR = true;
const submission = setInterval(() => {
let output = document.getElementsByClassName('out')[0]
.innerText;
if (output.includes('Problem Solved Successfully') && START_MONITOR) {
// clear timeout
START_MONITOR = false;
clearInterval(gfgLoader);
clearInterval(submission);
// get data
title = findTitle().trim();
difficulty = findDifficulty();
problemStatement = getProblemStatement();
code = getCode();
language = findGfgLanguage();
// format data
let probName = title + ` - GFG`;
problemStatement =
`# ${title}\n## ${difficulty}\n` + problemStatement;
// if language was found
if (language !== null) {
chrome.storage.local.get('stats', (s) => {
const { stats } = s;
const filePath =
probName + toKebabCase(title + language);
let sha = null;
if (
stats !== undefined &&
stats.sha !== undefined &&
stats.sha[filePath] !== undefined
) {
sha = stats.sha[filePath];
}
// Only create README if not already created
// if (sha === null) {
uploadGit(
btoa(unescape(encodeURIComponent(problemStatement))),
probName,
'README.md',
README_MSG,
'upload',
);
// }
if (code !== '') {
setTimeout(function () {
uploadGit(
btoa(unescape(encodeURIComponent(code))),
probName,
toKebabCase(title + language),
SUBMIT_MSG,
'upload',
);
}, 1000);
}
});
}
} else if (output.includes('Compilation Error')) {
// clear timeout and do nothing
clearInterval(submission);
} else if (
!START_MONITOR &&
(output.includes('Compilation Error') ||
output.includes('Correct Answer'))
) {
clearInterval(submission);
}
}, 1000);
});
}
}, 1000);