@@ -20,6 +20,9 @@ const languages = {
20
20
Oracle : '.sql' ,
21
21
} ;
22
22
23
+ /* Default commit message */
24
+ const defaultMsg = 'Create README - LeetHub' ;
25
+
23
26
/* Difficulty of most recenty submitted question */
24
27
let difficulty = '' ;
25
28
@@ -42,13 +45,13 @@ function findLanguage() {
42
45
}
43
46
44
47
/* Main function for uploading code to GitHub repo */
45
- const upload = ( token , hook , code , directory , filename , sha ) => {
48
+ const upload = ( token , hook , code , directory , filename , sha , msg ) => {
46
49
// To validate user, load user object from GitHub.
47
50
const URL = `https://api.github.com/repos/${ hook } /contents/${ directory } /${ filename } ` ;
48
51
49
52
/* Define Payload */
50
53
let data = {
51
- message : `working commit - Created using LeetHub` ,
54
+ message : msg ,
52
55
content : code ,
53
56
} ;
54
57
if ( sha !== null ) {
@@ -99,7 +102,7 @@ const upload = (token, hook, code, directory, filename, sha) => {
99
102
xhr . send ( data ) ;
100
103
} ;
101
104
102
- function uploadGit ( code , problemName , filename ) {
105
+ function uploadGit ( code , problemName , fileName , msg = defaultMsg ) {
103
106
/* Get necessary payload data */
104
107
chrome . storage . sync . get ( 'leethub_token' , ( t ) => {
105
108
const token = t . leethub_token ;
@@ -114,7 +117,7 @@ function uploadGit(code, problemName, filename) {
114
117
/* Get SHA, if it exists */
115
118
116
119
/* to get unique key */
117
- const filePath = problemName + filename ;
120
+ const filePath = problemName + fileName ;
118
121
chrome . storage . sync . get ( 'stats' , ( s ) => {
119
122
const { stats } = s ;
120
123
let sha = null ;
@@ -127,7 +130,15 @@ function uploadGit(code, problemName, filename) {
127
130
sha = stats . sha [ filePath ] ;
128
131
}
129
132
/* Upload to git. */
130
- upload ( token , hook , code , problemName , filename , sha ) ;
133
+ upload (
134
+ token ,
135
+ hook ,
136
+ code ,
137
+ problemName ,
138
+ fileName ,
139
+ sha ,
140
+ msg ,
141
+ ) ;
131
142
} ) ;
132
143
}
133
144
} ) ;
@@ -168,9 +179,9 @@ function parseQuestion() {
168
179
const qbody = questionElem [ 0 ] . innerHTML ;
169
180
170
181
// Problem title.
171
- let qtitle = document . getElementsByClassName ( 'css-v3d350' ) [ 0 ] ;
182
+ let qtitle = document . getElementsByClassName ( 'css-v3d350' ) ;
172
183
if ( checkElem ( qtitle ) ) {
173
- qtitle = qtitle . innerHTML ;
184
+ qtitle = qtitle [ 0 ] . innerHTML ;
174
185
} else {
175
186
qtitle = 'unknown-problem' ;
176
187
}
@@ -192,9 +203,25 @@ function parseQuestion() {
192
203
return markdown ;
193
204
}
194
205
206
+ /* Parser function for time/space stats */
207
+ function parseStats ( ) {
208
+ const probStats = document . getElementsByClassName ( 'data__HC-i' ) ;
209
+ if ( ! checkElem ( probStats ) ) {
210
+ return null ;
211
+ }
212
+ const time = probStats [ 0 ] . textContent ;
213
+ const timePercentile = probStats [ 1 ] . textContent ;
214
+ const space = probStats [ 2 ] . textContent ;
215
+ const spacePercentile = probStats [ 3 ] . textContent ;
216
+
217
+ // Format commit message
218
+ return `Time: ${ time } (${ timePercentile } ), Space: ${ space } (${ spacePercentile } ) - LeetHub` ;
219
+ }
220
+
195
221
const loader = setInterval ( ( ) => {
196
222
let code = null ;
197
223
let probStatement = null ;
224
+ let probStats = null ;
198
225
199
226
const successTag = document . getElementsByClassName ( 'success__3Ai7' ) ;
200
227
if (
@@ -204,8 +231,9 @@ const loader = setInterval(() => {
204
231
) {
205
232
code = parseCode ( ) ;
206
233
probStatement = parseQuestion ( ) ;
234
+ probStats = parseStats ( ) ;
207
235
}
208
- if ( code !== null && probStatement !== null ) {
236
+ if ( code !== null && probStatement !== null && probStats !== null ) {
209
237
clearTimeout ( loader ) ;
210
238
const problemName = window . location . pathname . split ( '/' ) [ 2 ] ; // must be true.
211
239
const language = findLanguage ( ) ;
@@ -214,16 +242,33 @@ const loader = setInterval(() => {
214
242
btoa ( unescape ( encodeURIComponent ( code ) ) ) ,
215
243
problemName ,
216
244
problemName + language ,
245
+ probStats ,
217
246
) ; // Encode `code` to base64
218
247
219
- /* @TODO : Change this setTimeout to Promise */
220
- setTimeout ( function ( ) {
221
- uploadGit (
222
- btoa ( unescape ( encodeURIComponent ( probStatement ) ) ) ,
223
- problemName ,
224
- 'README.md' ,
225
- ) ;
226
- } , 2000 ) ;
248
+ /* Only create README if not already created */
249
+ chrome . storage . sync . get ( 'stats' , ( s ) => {
250
+ const { stats } = s ;
251
+ const filePath = problemName + problemName + language ;
252
+ let sha = null ;
253
+ if (
254
+ stats !== undefined &&
255
+ stats . sha !== undefined &&
256
+ stats . sha [ filePath ] !== undefined
257
+ ) {
258
+ sha = stats . sha [ filePath ] ;
259
+ }
260
+
261
+ if ( sha === null ) {
262
+ /* @TODO : Change this setTimeout to Promise */
263
+ setTimeout ( function ( ) {
264
+ uploadGit (
265
+ btoa ( unescape ( encodeURIComponent ( probStatement ) ) ) ,
266
+ problemName ,
267
+ 'README.md' ,
268
+ ) ;
269
+ } , 2000 ) ;
270
+ }
271
+ } ) ;
227
272
}
228
273
}
229
274
} , 1000 ) ;
0 commit comments