Skip to content

Commit

Permalink
Update gfg.js (QasimWani#310)
Browse files Browse the repository at this point in the history
* Update gfg.js

Fix QasimWani#307

* Review update

* 0.0.9

* Update `output` window `querySelector`

The old selector element gets removed from the DOM when the output window close is closed.
  • Loading branch information
ASahu16 authored Oct 9, 2022
1 parent 1c53d70 commit 8db4314
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "LeetHub",
"version": "0.0.8",
"version": "0.0.9",
"private": true,
"description": "Automatically integrate your code with LeetCode and GitHub",
"scripts": {
Expand Down
40 changes: 17 additions & 23 deletions scripts/gfg.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ const UPDATE_MSG = 'Updated solution - LeetHub';
let START_MONITOR = true;
const toKebabCase = (string) => {
return string
.replace(/[^a-zA-Z0-9\. ]/g, '') //remove special chars
.replace(/[^a-zA-Z0-9\. ]/g, '') // remove special chars
.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]
const ele = document.getElementsByClassName('divider text')[0]
.innerText;
let lang = ele.split('(')[0].trim();
const 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]
const ele = document.querySelector('[class^="problems_header_content__title"] > h3')
.innerText;
if (ele != null) {
return ele;
Expand All @@ -39,9 +39,7 @@ function findTitle() {
}

function findDifficulty() {
const ele = document.getElementsByClassName(
'problem-tab__problem-level',
)[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 @@ -53,35 +51,32 @@ function findDifficulty() {
}

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

function getCode() {
let hackyScriptContent = `
console.log("trying to get editor content");

const scriptContent = `
var editor = ace.edit("ace-editor");
var editorContent = editor.getValue();
// console.log(editorContent);
var para = document.createElement("pre");
para.innerText+=editorContent;
para.setAttribute("id","codeDataLeetHub")
document.body.appendChild(para);
// console.log(para);
`;

// console.log(hackyScriptContent);

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

let nodeDeletionScript = `
const nodeDeletionScript = `
document.body.removeChild(para)
`;
var script = document.createElement('script');
Expand All @@ -93,7 +88,7 @@ function getCode() {
document.documentElement
).appendChild(script);

return text ? text : '';
return text || '';
}

const gfgLoader = setInterval(() => {
Expand All @@ -108,13 +103,13 @@ const gfgLoader = setInterval(() => {
'practice.geeksforgeeks.org/problems',
)
) {
let submitBtn = document.getElementById('run');

// console.log("listening to events");
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(() => {
let output = document.getElementsByClassName('out')[0]
const output = document.querySelectorAll('[class^="problems_content"]')[0]
.innerText;
if (
output.includes('Problem Solved Successfully') &&
Expand All @@ -132,10 +127,9 @@ const gfgLoader = setInterval(() => {
language = findGfgLanguage();

// format data
let probName = title + ` - GFG`;
const probName = `${title} - GFG`;

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

// if language was found
if (language !== null) {
Expand Down

0 comments on commit 8db4314

Please sign in to comment.