Skip to content

Commit

Permalink
added cardinality and element checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibek Shrestha committed Aug 12, 2021
1 parent 30883f7 commit 8218025
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions scripts/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,18 +485,21 @@ document.addEventListener('click', (event) => {
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 "";
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()}`.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 Down Expand Up @@ -608,25 +611,34 @@ function startUploadCountDown() {
}

/* 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/")) {
function insertToAnchorElement(elem) {
if (document.URL.startsWith("https://leetcode.com/explore/")) {
// means we are in explore page
target = document.getElementsByClassName('action')[0].getElementsByClassName('row')[0].getElementsByClassName('col-sm-6')[1]
elem.className = "pull-left"
target.childNodes[0].prepend(elem)

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 {
target = document.getElementsByClassName('action__38Xc')[0]
elem.className = "runcode-wrapper__8rXm"
target.childNodes[0].prepend(elem)
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() {
try {
elem = document.getElementById('leethub_progress_anchor_element')
if (elem !== undefined) {
if (!elem) {
elem = document.createElement('span')
elem.id = "leethub_progress_anchor_element"
elem.style = "margin-right: 20px;padding-top: 2px;"
Expand All @@ -645,17 +657,21 @@ function startUpload() {
/* 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

0 comments on commit 8218025

Please sign in to comment.