Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"https://practice.geeksforgeeks.org/*"
],
"js": [
"scripts/util.js",
"scripts/leetcode.js",
"scripts/authorize.js",
"scripts/gfg.js"
Expand Down
1 change: 1 addition & 0 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ <h1 id="title">
</div>
</div>

<script src="scripts/util.js"></script>
<script src="scripts/oauth2.js"></script>
<script type="text/javascript" src="popup.js"></script>
</body>
Expand Down
43 changes: 14 additions & 29 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global oAuth2 */
/* global oAuth2, githubRequest, displayStats */
/* eslint no-undef: "error" */

let action = false;
Expand All @@ -10,28 +10,24 @@ $('#authenticate').on('click', () => {
});

/* Get URL for welcome page */
$('#welcome_URL').attr(
'href',
chrome.runtime.getURL('welcome.html')
);
$('#hook_URL').attr(
'href',
chrome.runtime.getURL('welcome.html')
);
$('#welcome_URL').attr('href', chrome.runtime.getURL('welcome.html'));
$('#hook_URL').attr('href', chrome.runtime.getURL('welcome.html'));

chrome.storage.local.get('leethub_token', (data) => {
const token = data.leethub_token;
if (token === null || token === undefined) {
action = true;
$('#auth_mode').show();
} else {
// To validate user, load user object from GitHub.
const AUTHENTICATION_URL = 'https://api.github.com/user';

const xhr = new XMLHttpRequest();
xhr.addEventListener('readystatechange', function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
githubRequest(
'GET',
AUTHENTICATION_URL,
token,
null,
(status) => {
if (status === 200) {
/* Show MAIN FEATURES */
chrome.storage.local.get('mode_type', (data2) => {
if (data2 && data2.mode_type === 'commit') {
Expand All @@ -40,13 +36,7 @@ chrome.storage.local.get('leethub_token', (data) => {
chrome.storage.local.get(
['stats', 'leethub_hook'],
(data3) => {
const { stats } = data3;
if (stats && stats.solved) {
$('#p_solved').text(stats.solved);
$('#p_solved_easy').text(stats.easy);
$('#p_solved_medium').text(stats.medium);
$('#p_solved_hard').text(stats.hard);
}
displayStats(data3.stats);
const leethubHook = data3.leethub_hook;
if (leethubHook) {
$('#repo_url').html(
Expand All @@ -59,9 +49,7 @@ chrome.storage.local.get('leethub_token', (data) => {
$('#hook_mode').show();
}
});
} else if (xhr.status === 401) {
// bad oAuth
// reset token and redirect to authorization process again!
} else if (status === 401) {
chrome.storage.local.set({ leethub_token: null }, () => {
console.log(
'BAD oAuth!!! Redirecting back to oAuth process',
Expand All @@ -70,10 +58,7 @@ chrome.storage.local.get('leethub_token', (data) => {
$('#auth_mode').show();
});
}
}
});
xhr.open('GET', AUTHENTICATION_URL, true);
xhr.setRequestHeader('Authorization', `token ${token}`);
xhr.send();
},
);
}
});
45 changes: 26 additions & 19 deletions scripts/gfg.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global languages, getSHA, uploadGit, encodeToBase64 */

/* Enum for languages supported by GeeksForGeeks. */
// const gfgLanguages = {
// Python3: '.py',
Expand Down Expand Up @@ -30,16 +32,19 @@ function findGfgLanguage() {
}

function findTitle() {
const ele = document.querySelector('[class^="problems_header_content__title"] > h3')
.innerText;
const ele = document.querySelector(
'[class^="problems_header_content__title"] > h3',
).innerText;
if (ele != null) {
return ele;
}
return '';
}

function findDifficulty() {
const ele = document.querySelectorAll('[class^="problems_header_description"]')[0].children[0].innerText;
const ele = document.querySelectorAll(
'[class^="problems_header_description"]',
)[0].children[0].innerText;

if (ele != null) {
if (ele.trim() == 'Basic' || ele.trim() === 'School') {
Expand All @@ -51,12 +56,13 @@ function findDifficulty() {
}

function getProblemStatement() {
const ele = document.querySelector('[class^="problems_problem_content"]');
const ele = document.querySelector(
'[class^="problems_problem_content"]',
);
return `${ele.outerHTML}`;
}

function getCode() {

const scriptContent = `
var editor = ace.edit("ace-editor");
var editorContent = editor.getValue();
Expand Down Expand Up @@ -103,14 +109,22 @@ const gfgLoader = setInterval(() => {
'practice.geeksforgeeks.org/problems',
)
) {

const submitBtn = document.evaluate(".//button[text()='Submit']", document.body, null, XPathResult.ANY_TYPE, null).iterateNext();
const submitBtn = document
.evaluate(
".//button[text()='Submit']",
document.body,
null,
XPathResult.ANY_TYPE,
null,
)
.iterateNext();

submitBtn.addEventListener('click', function () {
START_MONITOR = true;
const submission = setInterval(() => {
const output = document.querySelectorAll('[class^="problems_content"]')[0]
.innerText;
const output = document.querySelectorAll(
'[class^="problems_content"]',
)[0].innerText;
if (
output.includes('Problem Solved Successfully') &&
START_MONITOR
Expand All @@ -137,19 +151,12 @@ const gfgLoader = setInterval(() => {
const { stats } = s;
const filePath =
probName + toKebabCase(title + language);
let sha = null;
if (
stats !== undefined &&
stats.sha !== undefined &&
stats.sha[filePath] !== undefined
) {
sha = stats.sha[filePath];
}
const sha = getSHA(stats, filePath);

// Only create README if not already created
// if (sha === null) {
uploadGit(
btoa(unescape(encodeURIComponent(problemStatement))),
encodeToBase64(problemStatement),
probName,
'README.md',
README_MSG,
Expand All @@ -163,7 +170,7 @@ const gfgLoader = setInterval(() => {
if (code !== '') {
setTimeout(function () {
uploadGit(
btoa(unescape(encodeURIComponent(code))),
encodeToBase64(code),
probName,
toKebabCase(title + language),
SUBMIT_MSG,
Expand Down
Loading