Skip to content

Commit

Permalink
Merge pull request QasimWani#147 from QasimWani/dev
Browse files Browse the repository at this point in the history
Update firefox and reload submissions
  • Loading branch information
QasimWani authored Dec 12, 2021
2 parents b5a6afa + e59e311 commit 83633d1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 51 deletions.
4 changes: 4 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"persistent": false
},
"permissions": [
"https://github.com/*",
"https://api.github.com/*",
"https://leetcode.com/*",
"https://practice.geeksforgeeks.org/*",
"tabs",
"activeTab",
"unlimitedStorage",
Expand Down
1 change: 1 addition & 0 deletions popup.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/popup.css" />
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
Expand Down
4 changes: 2 additions & 2 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ $('#authenticate').on('click', () => {
/* Get URL for welcome page */
$('#welcome_URL').attr(
'href',
`chrome-extension://${chrome.runtime.id}/welcome.html`,
chrome.runtime.getURL('welcome.html')
);
$('#hook_URL').attr(
'href',
`chrome-extension://${chrome.runtime.id}/welcome.html`,
chrome.runtime.getURL('welcome.html')
);

chrome.storage.local.get('leethub_token', (data) => {
Expand Down
4 changes: 2 additions & 2 deletions scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ function handleMessage(request) {
});

/* Go to onboarding for UX */
const urlOnboarding = `chrome-extension://${chrome.runtime.id}/welcome.html`;
chrome.tabs.create({ url: urlOnboarding, selected: true }); // creates new tab
const urlOnboarding = chrome.runtime.getURL('welcome.html');
chrome.tabs.create({ url: urlOnboarding, active: true }); // creates new tab
} else if (
request &&
request.closeWebPage === true &&
Expand Down
155 changes: 109 additions & 46 deletions scripts/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const readmeMsg = 'Create README - LeetHub';
const discussionMsg = 'Prepend discussion post - LeetHub';
const createNotesMsg = 'Create NOTES - LeetHub';

// problem types
const NORMAL_PROBLEM = 0;
const EXPLORE_SECTION_PROBLEM = 1;

/* Difficulty of most recenty submitted question */
let difficulty = '';

Expand Down Expand Up @@ -484,16 +488,22 @@ document.addEventListener('click', (event) => {
the note should be opened atleast once for this to work
this is because the dom is populated after data is fetched by opening the note */
function getNotesIfAny() {
// there are no notes on expore
if (document.URL.startsWith("https://leetcode.com/explore/")) return "";

notes = '';
notesdiv = document
.getElementsByClassName('notewrap__eHkN')[0]
.getElementsByClassName('CodeMirror-code')[0];
if (notesdiv) {
for (i = 0; i < notesdiv.childNodes.length; i++) {
if (notesdiv.childNodes[i].childNodes.length == 0) continue;
text = notesdiv.childNodes[i].childNodes[0].innerText;
if (text) {
notes = `${notes}\n${text.trim()}`;
if (checkElem(document.getElementsByClassName('notewrap__eHkN'))
&& checkElem(document.getElementsByClassName('notewrap__eHkN')[0].getElementsByClassName('CodeMirror-code'))) {
notesdiv = document
.getElementsByClassName('notewrap__eHkN')[0]
.getElementsByClassName('CodeMirror-code')[0];
if (notesdiv) {
for (i = 0; i < notesdiv.childNodes.length; i++) {
if (notesdiv.childNodes[i].childNodes.length == 0) continue;
text = notesdiv.childNodes[i].childNodes[0].innerText;
if (text) {
notes = `${notes}\n${text.trim()}`.trim();
}
}
}
}
Expand All @@ -504,23 +514,42 @@ const loader = setInterval(() => {
let code = null;
let probStatement = null;
let probStats = null;

let probType;
const successTag = document.getElementsByClassName('success__3Ai7');
const resultState = document.getElementById("result-state");
var success = false;
if (checkElem(successTag) && successTag[0].innerText.trim() === 'Success'){
// check success tag for a normal problem
if (checkElem(successTag) && successTag[0].className === 'success__3Ai7' && successTag[0].innerText.trim() === 'Success'){
console.log(successTag[0]);
success = true;
probType = NORMAL_PROBLEM;
}
else if(resultState && resultState.innerText=="Accepted"){

// check success state for a explore section problem
else if(resultState && resultState.className === "text-success" && resultState.innerText==="Accepted"){
success = true;
probType = EXPLORE_SECTION_PROBLEM;
}

if(success) {
probStatement = parseQuestion();
probStats = parseStats();
}

if (probStatement !== null) {
clearTimeout(loader);

switch(probType){
case NORMAL_PROBLEM:
successTag[0].classList.add('marked_as_success');
break;
case EXPLORE_SECTION_PROBLEM:
resultState.classList.add('marked_as_success');
break;
default:
console.error(`Unknown problem type ${probType}`);
return;
}

const problemName = getProblemNameSlug();
const language = findLanguage();
if (language !== null) {
Expand Down Expand Up @@ -552,20 +581,23 @@ const loader = setInterval(() => {
});

/* get the notes and upload it */
setTimeout(function () {
notes = getNotesIfAny();
if (notes != undefined && notes.length != 0) {
console.log('Create Notes');
// means we can upload the notes too
uploadGit(
btoa(unescape(encodeURIComponent(notes))),
problemName,
'NOTES.txt',
createNotesMsg,
'upload',
);
}
}, 500);
/* only upload notes if there is any */
notes = getNotesIfAny();
if (notes.length > 0) {
setTimeout(function () {
if (notes != undefined && notes.length != 0) {
console.log('Create Notes');
// means we can upload the notes too
uploadGit(
btoa(unescape(encodeURIComponent(notes))),
problemName,
'NOTES.txt',
createNotesMsg,
'upload',
);
}
}, 500);
}

/* Upload code to Git */
setTimeout(function () {
Expand Down Expand Up @@ -600,38 +632,69 @@ function startUploadCountDown() {
}
}, 10000);
}

/* we will need specific anchor element that is specific to the page you are in Eg. Explore */
function insertToAnchorElement(elem) {
if (document.URL.startsWith("https://leetcode.com/explore/")) {
// means we are in explore page
action = document.getElementsByClassName('action');
if (checkElem(action)
&& checkElem(action[0].getElementsByClassName('row'))
&& checkElem(action[0].getElementsByClassName('row')[0].getElementsByClassName('col-sm-6'))
&& action[0].getElementsByClassName('row')[0].getElementsByClassName('col-sm-6').length > 1) {
target = action[0].getElementsByClassName('row')[0].getElementsByClassName('col-sm-6')[1]
elem.className = "pull-left"
if (target.childNodes.length > 0)
target.childNodes[0].prepend(elem)
}
} else {
if (checkElem(document.getElementsByClassName('action__38Xc'))) {
target = document.getElementsByClassName('action__38Xc')[0]
elem.className = "runcode-wrapper__8rXm"
if (target.childNodes.length > 0)
target.childNodes[0].prepend(elem)
}
}
}

/* start upload will inject a spinner on left side to the "Run Code" button */
function startUpload() {
elem = document.getElementById('leethub_progress_anchor_element')
if (elem !== undefined) {
elem = document.createElement('span')
elem.id = "leethub_progress_anchor_element"
elem.className = "runcode-wrapper__8rXm"
elem.style = "margin-right: 20px;padding-top: 2px;"
}
elem.innerHTML = `<div id="leethub_progress_elem" class="leethub_progress"></div>`
target = document.getElementsByClassName('action__38Xc')[0]
if (target.childNodes.length > 0) {
target.childNodes[0].prepend(elem)
try {
elem = document.getElementById('leethub_progress_anchor_element')
if (!elem) {
elem = document.createElement('span')
elem.id = "leethub_progress_anchor_element"
elem.style = "margin-right: 20px;padding-top: 2px;"
}
elem.innerHTML = `<div id="leethub_progress_elem" class="leethub_progress"></div>`
target = insertToAnchorElement(elem)
// start the countdown
startUploadCountDown();
} catch (error) {
// generic exception handler for time being so that existing feature doesnt break but
// error gets logged
console.log(error);
}
// start the countdown
startUploadCountDown();
}

/* This will create a tick mark before "Run Code" button signalling LeetHub has done its job */
function markUploaded() {
elem = document.getElementById("leethub_progress_elem");
elem.className = "";
style = 'display: inline-block;transform: rotate(45deg);height:24px;width:12px;border-bottom:7px solid #78b13f;border-right:7px solid #78b13f;'
elem.style = style;
if (elem) {
elem.className = "";
style = 'display: inline-block;transform: rotate(45deg);height:24px;width:12px;border-bottom:7px solid #78b13f;border-right:7px solid #78b13f;'
elem.style = style;
}
}

/* This will create a failed tick mark before "Run Code" button signalling that upload failed */
function markUploadFailed() {
elem = document.getElementById("leethub_progress_elem");
elem.className = "";
style = 'display: inline-block;transform: rotate(45deg);height:24px;width:12px;border-bottom:7px solid red;border-right:7px solid red;'
elem.style = style;
if (elem) {
elem.className = "";
style = 'display: inline-block;transform: rotate(45deg);height:24px;width:12px;border-bottom:7px solid red;border-right:7px solid red;'
elem.style = style;
}
}

/* Sync to local storage */
Expand Down
2 changes: 1 addition & 1 deletion scripts/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const oAuth2 = {
chrome.storage.local.set({ pipe_leethub: true }, () => {
// opening pipe temporarily

chrome.tabs.create({ url, selected: true }, function () {
chrome.tabs.create({ url, active: true }, function () {
window.close();
chrome.tabs.getCurrent(function (tab) {
chrome.tabs.remove(tab.id, function () {});
Expand Down

0 comments on commit 83633d1

Please sign in to comment.