@@ -210,6 +210,74 @@ function uploadGit(
210
210
} ) ;
211
211
}
212
212
213
+ /* Function for finding and parsing the full code. */
214
+ /* - At first find the submission details url. */
215
+ /* - Then send a request for the details page. */
216
+ /* - Finally, parse the code from the html reponse. */
217
+ function findCode ( uploadGit , problemName , fileName , msg , action ) {
218
+ const e = document . getElementsByClassName ( 'status-column__3SUg' ) ;
219
+ if ( e != undefined && e . length > 1 ) {
220
+ /* Get the submission details url from the submission page. */
221
+ const submissionRef = e [ 1 ] . innerHTML . split ( ' ' ) [ 1 ] ;
222
+ const submissionURL = submissionRef . split ( '=' ) [ 1 ] . slice ( 1 , - 1 ) ;
223
+ /* Request for the submission details page */
224
+ const xhttp = new XMLHttpRequest ( ) ;
225
+ xhttp . onreadystatechange = function ( ) {
226
+ if ( this . readyState == 4 && this . status == 200 ) {
227
+ /* received submission details as html reponse. */
228
+ var doc = new DOMParser ( ) . parseFromString (
229
+ this . responseText ,
230
+ 'text/html' ,
231
+ ) ;
232
+ /* the response has a js object called pageData. */
233
+ /* Pagedata has the details data with code about that submission */
234
+ var scripts = doc . getElementsByTagName ( 'script' ) ;
235
+ for ( var i = 0 ; i < scripts . length ; i ++ ) {
236
+ var text = scripts [ i ] . innerText ;
237
+ if ( text . includes ( 'pageData' ) ) {
238
+ /* Considering the pageData as text and extract the sbustring
239
+ which has the full code */
240
+ var firstIndex = text . indexOf ( 'submissionCode' ) ;
241
+ var lastIndex = text . indexOf ( 'editCodeUrl' ) ;
242
+ var sclicedText = text . slice ( firstIndex , lastIndex ) ;
243
+ /* slicedText has code as like as. (submissionCode: 'Details code'). */
244
+ /* So finding the index of first and last single inverted coma. */
245
+ var firstInverted = sclicedText . indexOf ( "'" ) ;
246
+ var lastInverted = sclicedText . lastIndexOf ( "'" ) ;
247
+ /* Extract only the code */
248
+ var codeUnicoded = sclicedText . slice (
249
+ firstInverted + 1 ,
250
+ lastInverted ,
251
+ ) ;
252
+ /* The code has some unicode. Replacing all unicode with actual characters */
253
+ var code = codeUnicoded . replace (
254
+ / \\ u [ \d A - F ] { 4 } / gi,
255
+ function ( match ) {
256
+ return String . fromCharCode (
257
+ parseInt ( match . replace ( / \\ u / g, '' ) , 16 ) ,
258
+ ) ;
259
+ } ,
260
+ ) ;
261
+ if ( code != null ) {
262
+ setTimeout ( function ( ) {
263
+ uploadGit (
264
+ btoa ( unescape ( encodeURIComponent ( code ) ) ) ,
265
+ problemName ,
266
+ fileName ,
267
+ msg ,
268
+ action ,
269
+ ) ;
270
+ } , 2000 ) ;
271
+ }
272
+ }
273
+ }
274
+ }
275
+ } ;
276
+ xhttp . open ( 'GET' , `https://leetcode.com${ submissionURL } ` , true ) ;
277
+ xhttp . send ( ) ;
278
+ }
279
+ }
280
+
213
281
/* Main parser function for the code */
214
282
function parseCode ( ) {
215
283
const e = document . getElementsByClassName ( 'CodeMirror-code' ) ;
@@ -330,16 +398,14 @@ const loader = setInterval(() => {
330
398
successTag . length > 0 &&
331
399
successTag [ 0 ] . innerText . trim ( ) === 'Success'
332
400
) {
333
- code = parseCode ( ) ;
334
401
probStatement = parseQuestion ( ) ;
335
402
probStats = parseStats ( ) ;
336
403
}
337
- if ( code !== null && probStatement !== null && probStats !== null ) {
404
+ if ( probStatement !== null && probStats !== null ) {
338
405
clearTimeout ( loader ) ;
339
406
const problemName = window . location . pathname . split ( '/' ) [ 2 ] ; // must be true.
340
407
const language = findLanguage ( ) ;
341
408
if ( language !== null ) {
342
-
343
409
chrome . storage . sync . get ( 'stats' , ( s ) => {
344
410
const { stats } = s ;
345
411
const filePath = problemName + problemName + language ;
@@ -367,8 +433,8 @@ const loader = setInterval(() => {
367
433
368
434
/* Upload code to Git */
369
435
setTimeout ( function ( ) {
370
- uploadGit (
371
- btoa ( unescape ( encodeURIComponent ( code ) ) ) ,
436
+ findCode (
437
+ uploadGit ,
372
438
problemName ,
373
439
problemName + language ,
374
440
probStats ,
@@ -377,4 +443,4 @@ const loader = setInterval(() => {
377
443
} , 2000 ) ;
378
444
}
379
445
}
380
- } , 1000 ) ;
446
+ } , 1000 ) ;
0 commit comments