Skip to content

Commit

Permalink
fix: fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
BRO3886 committed Apr 3, 2021
1 parent 75dffe3 commit 9d07842
Showing 1 changed file with 138 additions and 134 deletions.
272 changes: 138 additions & 134 deletions scripts/gfg.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,48 @@ const SUBMIT_MSG = 'Added solution - LeetHub';
const UPDATE_MSG = 'Updated solution - LeetHub';
let START_MONITOR = true;
const toKebabCase = (string) => {
return string
.replace(/([a-z])([A-Z])/g, '$1-$2') // get all lowercase letters that are near to uppercase ones
.replace(/[\s_]+/g, '-') // replace all spaces and low dash
.toLowerCase(); // convert to lower case
return string
.replace(/([a-z])([A-Z])/g, '$1-$2') // get all lowercase letters that are near to uppercase ones
.replace(/[\s_]+/g, '-') // replace all spaces and low dash
.toLowerCase(); // convert to lower case
};

function findGfgLanguage() {
const ele = document.getElementsByClassName('filter-option')[0]
.innerText;
let lang = ele.split('(')[0].trim();
if (lang.length > 0 && languages[lang]) {
return languages[lang];
}
return null;
const ele = document.getElementsByClassName('filter-option')[0]
.innerText;
let lang = ele.split('(')[0].trim();
if (lang.length > 0 && languages[lang]) {
return languages[lang];
}
return null;
}

function findTitle() {
const ele = document.getElementsByClassName('problem-tab__name')[0]
.innerText;
if (ele != null) {
return ele;
}
return '';
const ele = document.getElementsByClassName('problem-tab__name')[0]
.innerText;
if (ele != null) {
return ele;
}
return '';
}

function findDifficulty() {
const ele = document.getElementsByClassName(
'problem-tab__problem-level',
)[0].innerText;
if (ele != null) {
return ele;
}
return '';
const ele = document.getElementsByClassName(
'problem-tab__problem-level',
)[0].innerText;
if (ele != null) {
return ele;
}
return '';
}

function getProblemStatement() {
const ele = document.getElementsByClassName('problem-statement')[0];
return `${ele.outerHTML}`;
const ele = document.getElementsByClassName('problem-statement')[0];
return `${ele.outerHTML}`;
}

function getCode() {
let hackyScriptContent = `
let hackyScriptContent = `
console.log("trying to get editor content");
var editorContent = editor.getValue();
// console.log(editorContent);
Expand All @@ -66,117 +66,121 @@ function getCode() {
// console.log(para);
`;

// console.log(hackyScriptContent);
// console.log(hackyScriptContent);

var script = document.createElement('script');
script.id = 'tmpScript';
script.appendChild(document.createTextNode(hackyScriptContent));
(
document.body ||
document.head ||
document.documentElement
).appendChild(script);
const text = document.getElementById('codeDataLeetHub').innerText;
var script = document.createElement('script');
script.id = 'tmpScript';
script.appendChild(document.createTextNode(hackyScriptContent));
(
document.body ||
document.head ||
document.documentElement
).appendChild(script);
const text = document.getElementById('codeDataLeetHub').innerText;

let nodeDeletionScript = `
let nodeDeletionScript = `
document.body.removeChild(para)
`;
var script = document.createElement('script');
script.id = 'tmpScript';
script.appendChild(document.createTextNode(nodeDeletionScript));
(
document.body ||
document.head ||
document.documentElement
).appendChild(script);

return text ? text : '';
var script = document.createElement('script');
script.id = 'tmpScript';
script.appendChild(document.createTextNode(nodeDeletionScript));
(
document.body ||
document.head ||
document.documentElement
).appendChild(script);

return text ? text : '';
}

const gfgLoader = setInterval(() => {
let code = null;
let problemStatement = null;
let title = null;
let language = null;
let difficulty = null;

if (
window.location.href.includes(
'practice.geeksforgeeks.org/problems',
)
) {
let submitBtn = document.getElementById('run');

// console.log("listening to events");
submitBtn.addEventListener('click', function() {
START_MONITOR = true;
const submission = setInterval(() => {
let output = document.getElementsByClassName('out')[0]
.innerText;
if (output.includes('Correct Answer')) {
// clear timeout
START_MONITOR = false;
clearInterval(gfgLoader);
clearInterval(submission);
// get data
title = findTitle().trim();
difficulty = findDifficulty();
problemStatement = getProblemStatement();
code = getCode();
language = findGfgLanguage();

// format data
let probName = title + ` - GFG`;

problemStatement =
`# ${title}\n## ${difficulty}\n` + problemStatement;

// if language was found
if (language !== null) {
chrome.storage.local.get('stats', (s) => {
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];
}

// Only create README if not already created
if (sha === null) {
uploadGit(
btoa(problemStatement),
probName,
'README.md',
README_MSG,
'upload',
);
}

if (code !== '') {
setTimeout(function() {
uploadGit(
btoa(code),
probName,
toKebabCase(title + language),
SUBMIT_MSG,
'upload',
);
}, 1000);
}
});
}
} else if (output.includes('Compilation Error')) {
// clear timeout and do nothing
clearInterval(submission);
} else if (!START_MONITOR && (output.includes('Compilation Error') || output.includes('Correct Answer'))) {
clearInterval(submission);
}
}, 1000);
});
}
}, 1000);
let code = null;
let problemStatement = null;
let title = null;
let language = null;
let difficulty = null;

if (
window.location.href.includes(
'practice.geeksforgeeks.org/problems',
)
) {
let submitBtn = document.getElementById('run');

// console.log("listening to events");
submitBtn.addEventListener('click', function () {
START_MONITOR = true;
const submission = setInterval(() => {
let output = document.getElementsByClassName('out')[0]
.innerText;
if (output.includes('Correct Answer')) {
// clear timeout
START_MONITOR = false;
clearInterval(gfgLoader);
clearInterval(submission);
// get data
title = findTitle().trim();
difficulty = findDifficulty();
problemStatement = getProblemStatement();
code = getCode();
language = findGfgLanguage();

// format data
let probName = title + ` - GFG`;

problemStatement =
`# ${title}\n## ${difficulty}\n` + problemStatement;

// if language was found
if (language !== null) {
chrome.storage.local.get('stats', (s) => {
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];
}

// Only create README if not already created
if (sha === null) {
uploadGit(
btoa(problemStatement),
probName,
'README.md',
README_MSG,
'upload',
);
}

if (code !== '') {
setTimeout(function () {
uploadGit(
btoa(code),
probName,
toKebabCase(title + language),
SUBMIT_MSG,
'upload',
);
}, 1000);
}
});
}
} else if (output.includes('Compilation Error')) {
// clear timeout and do nothing
clearInterval(submission);
} else if (
!START_MONITOR &&
(output.includes('Compilation Error') ||
output.includes('Correct Answer'))
) {
clearInterval(submission);
}
}, 1000);
});
}
}, 1000);

0 comments on commit 9d07842

Please sign in to comment.