Skip to content

Commit

Permalink
Merge pull request QasimWani#65 from QasimWani/main
Browse files Browse the repository at this point in the history
Set dev to head
  • Loading branch information
QasimWani authored Jan 26, 2021
2 parents d0aa40c + 7e43acd commit 13db71a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<p align="center">
<a href="https://github.com/QasimWani/LeetHub/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="license"/></a>
<a href="https://discord.gg/42kzTWwk"><img src="https://img.shields.io/discord/781373810251137074" alt="discord"></a>
<a href="https://discord.gg/anXT9vErxu"><img src="https://img.shields.io/discord/781373810251137074" alt="discord"></a>
<a href="https://chrome.google.com/webstore/detail/leethub/aciombdipochlnkbpcbgdpjffcfdbggi"><img src="https://img.shields.io/chrome-web-store/v/aciombdipochlnkbpcbgdpjffcfdbggi.svg" alt="chrome-webstore"/></a>
<a href="https://chrome.google.com/webstore/detail/leethub/aciombdipochlnkbpcbgdpjffcfdbggi"><img src="https://img.shields.io/chrome-web-store/d/aciombdipochlnkbpcbgdpjffcfdbggi.svg" alt="users"></a>
<a href="https://twitter.com/intent/tweet?text=LeetHub%20-%20Automatically%20sync%20your%20code%20to%20GitHub%20after%20solving%20any%20leetcode%20problems!&url=https://github.com/QasimWani/LeetHub&hashtags=javascript,github,leetcode,coding,interview,chrome"> <img src="https://img.shields.io/twitter/url/http/shields.io.svg?style=social"> </a>
Expand Down Expand Up @@ -36,7 +36,7 @@


## How fast is it really?
### THIS FAST!
### THIS FAST (~400ms, 530x speed improvement)!

![](assets/extension/output.gif)

Expand Down
Binary file modified assets/extension/output.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name" : "LeetHub",
"description" : "Automatically integrate your code with LeetCode and GitHub",
"homepage_url": "https://github.com/QasimWani/LeetHub",
"version" : "0.1.0",
"version" : "0.1.1",
"author" : "Qasim Wani",
"browser_action": {
"default_icon": "assets/thumbnail.png",
Expand Down
110 changes: 89 additions & 21 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,24 +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) {
uploadGit(
btoa(unescape(encodeURIComponent(probStatement))),
problemName,
'README.md',
readmeMsg,
'upload',
);

/* Only create README if not already created */
chrome.storage.sync.get('stats', (s) => {
const { stats } = s;
const filePath = problemName + problemName + language;
Expand All @@ -360,19 +418,29 @@ const loader = setInterval(() => {
sha = stats.sha[filePath];
}

/* Only create README if not already created */
if (sha === null) {
/* @TODO: Change this setTimeout to Promise */
setTimeout(function () {
uploadGit(
btoa(unescape(encodeURIComponent(code))),
problemName,
problemName + language,
probStats,
'upload',
); // Encode `code` to base64
}, 2000);
uploadGit(
btoa(unescape(encodeURIComponent(probStatement))),
problemName,
'README.md',
readmeMsg,
'upload',
);
}
});

/* Upload code to Git */
setTimeout(function () {
findCode(
uploadGit,
problemName,
problemName + language,
probStats,
'upload',
); // Encode `code` to base64
}, 2000);
}
}
}, 1000);
}, 1000);
1 change: 1 addition & 0 deletions scripts/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const linkStatusCode = (status, name) => {
bool = true;
break;
}
$('#unlink').show();
return bool;
};

Expand Down

0 comments on commit 13db71a

Please sign in to comment.