Skip to content

Commit

Permalink
Update Promises Sequentially
Browse files Browse the repository at this point in the history
  • Loading branch information
Rupesh Dabbir committed Nov 2, 2018
1 parent fbd0f19 commit 1051b50
Showing 1 changed file with 77 additions and 59 deletions.
136 changes: 77 additions & 59 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,97 @@
// @version 0.1
// @description try to take over the world! The primary aim of this script is to identify in what coding language the user has written the code. For v1, I only consider the first result.
// @author Rupesh Dabbir
// @match https://leetcode.com/contest/weekly-contest-108/ranking/*
// @match https://leetcode.com/contest/*/ranking/*
// @grant none
// ==/UserScript==

(function() {

const waitFor = (...selectors) => new Promise(resolve => {
const delay = 500
const f = () => {
const elements = selectors.map(selector => document.querySelector(selector))
if (elements.every(element => element != null)) {
resolve(elements)
} else {
setTimeout(f, delay)
const delay = 500
const f = () => {
const elements = selectors.map(selector => document.querySelector(selector))
if (elements.every(element => element != null)) {
resolve(elements)
} else {
setTimeout(f, delay)
}
}
}
f()
f()
})

waitFor('.pagination').then(([table])=>{
waitFor('.pagination').then(([table]) => {
$('.pagination').click(function() {
doAjax();
});
});

console.log("From TamperMonkey",$);
waitFor('thead > tr').then(([table])=>{
console.log("From TamperMonkey", $);
waitFor('thead > tr').then(([table]) => {
console.log(table);
$('thead > tr').append('<th> Language</th>');
doAjax();
});

function doAjax() {
const contextSuffix = window.location.href.split('/').filter((val) => val.indexOf('weekly-contest') > -1)[0];
const page = window.location.href.split('ranking/')[1][0] ? window.location.href.split('ranking/')[1][0]: 1;
const page = window.location.href.split('ranking/')[1][0] ? window.location.href.split('ranking/')[1][0] : 1;
const url = `https://leetcode.com/contest/api/ranking/${contextSuffix}/?pagination=${page}`;
console.log("Page is:", page);
console.info("url is:",url);
console.info("url is:", url);
fetch(url)
.then((resp) => resp.json())
.then(function(data) {
console.log("Results", data);
// Data is a JSON.
fetchSubmissionIDS(data);
});
console.log("Results", data);
// Data is a JSON.
fetchSubmissionIDS(data);
});
}

function fetchSubmissionIDS(data) {
const ids = [];
data.submissions.forEach((submission) => {
const one = Object.values(submission)[0];
const id = one.submission_id;
ids.push(id);
const ids = [];
data.submissions.forEach((submission) => {
const one = Object.values(submission)[0];
const id = one.submission_id;
ids.push(id);
});
console.log("IDS are", ids);
fetchLanguageInfo(ids);
}

function doSyncCall(id) {
let url = 'https://leetcode.com/api/submissions/';

return new Promise((resolve, reject) => {
fetch(url + id).then(resp => resp.json()).then((data) => resolve(data.lang))
});
console.log("IDS are",ids);
fetchLanguageInfo(ids);
}
}

function fetchLanguageInfo(ids) {
const languageArr = [];
const languageArr = [];
// let url = 'https://leetcode.com/api/submissions/';
// let final = new Promise();

let url = 'https://leetcode.com/api/submissions/';
ids.reduce((promise, id) => {
return promise.then((result) => {
return doSyncCall(id).then(val => {
languageArr.push(val);
// final.resolve();
});
});
}, Promise.resolve()).then(() => {
console.log("LANGGG",languageArr);
appendLangToDOM(languageArr);
});

// Promise.all(ids.map(id =>
// fetch(url + id).then(resp => resp.json()).then((data) => languageArr.push(data.lang))
// )).then(data => {
// console.log("AFTER PROMISE ALL");
// appendLangToDOM(languageArr);
// })

Promise.all(ids.map(id =>
fetch(url+id).then(resp => resp.json()).then((data) => languageArr.push(data.lang))
)).then(data => {
console.log("AFTER PROMISE ALL");
appendLangToDOM(languageArr);
})

//console.log("Final Array with Languages", languageArr);
//appendLangToDOM(languageArr);
//console.log("Final Array with Languages", languageArr);
//appendLangToDOM(languageArr);
}

function makeCallAndFetchLanguage(id, arr) {
Expand All @@ -85,34 +103,34 @@
fetch(url)
.then((resp) => resp.json())
.then(function(data) {
//console.log("Language Results", data);
// Data is a JSON.
arr.push(Promise.resolve(data.lang));
});
//console.log("Language Results", data);
// Data is a JSON.
arr.push(Promise.resolve(data.lang));
});
}

function appendLangToDOM(arr) {
console.log("==enter==");
console.log(arr);
const myTd = document.createElement('td');
myTd.innerHTML = "JS <3";
const domNode = $('.success').append(myTd);
const trs = Array.prototype.slice.call($('tbody > tr'));
const domNode = $('.success').append(myTd);
const trs = Array.prototype.slice.call($('tbody > tr'));
$('.success')
let j=0;
//console.log("TR's", trs);
window.tr = trs;
$('tbody > tr').each(function(i) {
//console.log("Each Elem", $('tbody > tr')[i]);
if(i > 0) {
const td = document.createElement('td');
//console.log("array JAKE", arr[j]);
td.innerHTML = arr[j];
$('tbody > tr')[i].append(td);
j++;
}
});
let j = 0;
//console.log("TR's", trs);
window.tr = trs;
$('tbody > tr').each(function(i) {
//console.log("Each Elem", $('tbody > tr')[i]);
if (i > 0) {
const td = document.createElement('td');
//console.log("array JAKE", arr[j]);
td.innerHTML = arr[j];
$('tbody > tr')[i].append(td);
j++;
}
});
}


})();
})();

0 comments on commit 1051b50

Please sign in to comment.