Skip to content

Commit

Permalink
Merge pull request QasimWani#64 from mahbubcseju/main
Browse files Browse the repository at this point in the history
Full solution not being committed, bottom portion is being cropped out. QasimWani#62(Solved)
  • Loading branch information
QasimWani authored Jan 26, 2021
2 parents aa70284 + 75b63e0 commit 7e43acd
Showing 1 changed file with 72 additions and 6 deletions.
78 changes: 72 additions & 6 deletions scripts/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,74 @@ function uploadGit(
});
}

/* Function for finding and parsing the full code. */
/* - At first find the submission details url. */
/* - Then send a request for the details page. */
/* - Finally, parse the code from the html reponse. */
function findCode(uploadGit, problemName, fileName, msg, action) {
const e = document.getElementsByClassName('status-column__3SUg');
if (e != undefined && e.length > 1) {
/* Get the submission details url from the submission page. */
const submissionRef = e[1].innerHTML.split(' ')[1];
const submissionURL = submissionRef.split('=')[1].slice(1, -1);
/* Request for the submission details page */
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
/* received submission details as html reponse. */
var doc = new DOMParser().parseFromString(
this.responseText,
'text/html',
);
/* the response has a js object called pageData. */
/* Pagedata has the details data with code about that submission */
var scripts = doc.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
var text = scripts[i].innerText;
if (text.includes('pageData')) {
/* Considering the pageData as text and extract the sbustring
which has the full code */
var firstIndex = text.indexOf('submissionCode');
var lastIndex = text.indexOf('editCodeUrl');
var sclicedText = text.slice(firstIndex, lastIndex);
/* slicedText has code as like as. (submissionCode: 'Details code'). */
/* So finding the index of first and last single inverted coma. */
var firstInverted = sclicedText.indexOf("'");
var lastInverted = sclicedText.lastIndexOf("'");
/* Extract only the code */
var codeUnicoded = sclicedText.slice(
firstInverted + 1,
lastInverted,
);
/* The code has some unicode. Replacing all unicode with actual characters */
var code = codeUnicoded.replace(
/\\u[\dA-F]{4}/gi,
function (match) {
return String.fromCharCode(
parseInt(match.replace(/\\u/g, ''), 16),
);
},
);
if (code != null) {
setTimeout(function () {
uploadGit(
btoa(unescape(encodeURIComponent(code))),
problemName,
fileName,
msg,
action,
);
}, 2000);
}
}
}
}
};
xhttp.open('GET', `https://leetcode.com${submissionURL}`, true);
xhttp.send();
}
}

/* Main parser function for the code */
function parseCode() {
const e = document.getElementsByClassName('CodeMirror-code');
Expand Down Expand Up @@ -330,16 +398,14 @@ const loader = setInterval(() => {
successTag.length > 0 &&
successTag[0].innerText.trim() === 'Success'
) {
code = parseCode();
probStatement = parseQuestion();
probStats = parseStats();
}
if (code !== null && probStatement !== null && probStats !== null) {
if (probStatement !== null && probStats !== null) {
clearTimeout(loader);
const problemName = window.location.pathname.split('/')[2]; // must be true.
const language = findLanguage();
if (language !== null) {

chrome.storage.sync.get('stats', (s) => {
const { stats } = s;
const filePath = problemName + problemName + language;
Expand Down Expand Up @@ -367,8 +433,8 @@ const loader = setInterval(() => {

/* Upload code to Git */
setTimeout(function () {
uploadGit(
btoa(unescape(encodeURIComponent(code))),
findCode(
uploadGit,
problemName,
problemName + language,
probStats,
Expand All @@ -377,4 +443,4 @@ const loader = setInterval(() => {
}, 2000);
}
}
}, 1000);
}, 1000);

0 comments on commit 7e43acd

Please sign in to comment.