Skip to content

Commit 7e43acd

Browse files
authored
Merge pull request QasimWani#64 from mahbubcseju/main
Full solution not being committed, bottom portion is being cropped out. QasimWani#62(Solved)
2 parents aa70284 + 75b63e0 commit 7e43acd

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

scripts/leetcode.js

+72-6
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,74 @@ function uploadGit(
210210
});
211211
}
212212

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[\dA-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+
213281
/* Main parser function for the code */
214282
function parseCode() {
215283
const e = document.getElementsByClassName('CodeMirror-code');
@@ -330,16 +398,14 @@ const loader = setInterval(() => {
330398
successTag.length > 0 &&
331399
successTag[0].innerText.trim() === 'Success'
332400
) {
333-
code = parseCode();
334401
probStatement = parseQuestion();
335402
probStats = parseStats();
336403
}
337-
if (code !== null && probStatement !== null && probStats !== null) {
404+
if (probStatement !== null && probStats !== null) {
338405
clearTimeout(loader);
339406
const problemName = window.location.pathname.split('/')[2]; // must be true.
340407
const language = findLanguage();
341408
if (language !== null) {
342-
343409
chrome.storage.sync.get('stats', (s) => {
344410
const { stats } = s;
345411
const filePath = problemName + problemName + language;
@@ -367,8 +433,8 @@ const loader = setInterval(() => {
367433

368434
/* Upload code to Git */
369435
setTimeout(function () {
370-
uploadGit(
371-
btoa(unescape(encodeURIComponent(code))),
436+
findCode(
437+
uploadGit,
372438
problemName,
373439
problemName + language,
374440
probStats,
@@ -377,4 +443,4 @@ const loader = setInterval(() => {
377443
}, 2000);
378444
}
379445
}
380-
}, 1000);
446+
}, 1000);

0 commit comments

Comments
 (0)