forked from QasimWani/LeetHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gfg): added script for GeeksForGeeks
- Loading branch information
Showing
2 changed files
with
195 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,45 @@ | ||
{ | ||
"manifest_version" : 2, | ||
"name" : "LeetHub", | ||
"description" : "Automatically integrate your code with LeetCode and GitHub", | ||
"homepage_url": "https://github.com/QasimWani/LeetHub", | ||
"version" : "0.1.3", | ||
"author" : "Qasim Wani", | ||
"browser_action": { | ||
"default_icon": "assets/thumbnail.png", | ||
"default_popup": "popup.html" | ||
}, | ||
"icons": { | ||
"16": "assets/thumbnail.png", | ||
"48": "assets/thumbnail.png", | ||
"128": "assets/thumbnail.png" | ||
}, | ||
"background": { | ||
"scripts": ["scripts/background.js"], | ||
"persistent": false | ||
}, | ||
"permissions": [ | ||
"tabs", | ||
"activeTab", | ||
"unlimitedStorage", | ||
"storage" | ||
], | ||
"content_scripts": [ | ||
{ | ||
"matches": [ | ||
"https://leetcode.com/*", "https://github.com/*" | ||
], | ||
"js": ["scripts/leetcode.js", "scripts/authorize.js"], | ||
"run_at": "document_start" | ||
} | ||
"manifest_version": 2, | ||
"name": "LeetHub", | ||
"description": "Automatically integrate your code with LeetCode and GitHub", | ||
"homepage_url": "https://github.com/QasimWani/LeetHub", | ||
"version": "0.1.3", | ||
"author": "Qasim Wani", | ||
"browser_action": { | ||
"default_icon": "assets/thumbnail.png", | ||
"default_popup": "popup.html" | ||
}, | ||
"icons": { | ||
"16": "assets/thumbnail.png", | ||
"48": "assets/thumbnail.png", | ||
"128": "assets/thumbnail.png" | ||
}, | ||
"background": { | ||
"scripts": [ | ||
"scripts/background.js" | ||
], | ||
"persistent": false | ||
}, | ||
"permissions": [ | ||
"tabs", | ||
"activeTab", | ||
"unlimitedStorage", | ||
"storage" | ||
], | ||
"content_scripts": [ | ||
{ | ||
"matches": [ | ||
"https://leetcode.com/*", | ||
"https://github.com/*", | ||
"https://practice.geeksforgeeks.org/*" | ||
], | ||
"content_security_policy": "script-src 'self' https://code.jquery.com/jquery-3.3.1.min.js https://github.com/login/oauth/authorize https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js; object-src 'self'" | ||
} | ||
"js": [ | ||
"scripts/leetcode.js", | ||
"scripts/authorize.js", | ||
"scripts/gfg.js" | ||
], | ||
"run_at": "document_start" | ||
} | ||
], | ||
"content_security_policy": "script-src 'self' https://code.jquery.com/jquery-3.3.1.min.js https://github.com/login/oauth/authorize https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js; object-src 'self'" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* Enum for languages supported by GeeksForGeeks. */ | ||
// const gfgLanguages = { | ||
// Python3: '.py', | ||
// 'C++': '.cpp', | ||
// Java: '.java', | ||
// Javascript: '.js', | ||
// }; | ||
|
||
// console.log("started gfg script"); | ||
|
||
/* Commit messages */ | ||
const README_MSG = 'Create README - LeetHub'; | ||
const SUBMIT_MSG = 'Added solution - LeetHub'; | ||
const UPDATE_MSG = 'Updated solution - LeetHub'; | ||
|
||
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 | ||
} | ||
|
||
function findTitle() { | ||
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 ""; | ||
} | ||
|
||
function getProblemStatement() { | ||
const ele = document.getElementsByClassName("problem-statement")[0]; | ||
return `${ele.outerHTML}`; | ||
} | ||
|
||
function getCode() { | ||
let hackyScriptContent = ` | ||
console.log("trying to get editor content"); | ||
var editorContent = editor.getValue(); | ||
// editorContent = editorContent.replace(/\\n/g,"<br/>") | ||
console.log(editorContent); | ||
var para = document.createElement("pre"); | ||
para.innerHTML+=editorContent; | ||
para.setAttribute("id","codeDataLeetHub") | ||
// para.appendChild(node); | ||
document.body.appendChild(para); | ||
// console.log(para); | ||
`; | ||
|
||
// 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; | ||
|
||
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 : ""; | ||
} | ||
|
||
const gfgLoader = setInterval(() => { | ||
let code = null; | ||
let problemStatement = null; | ||
let title = null; | ||
let language = null; | ||
let difficulty = null; | ||
|
||
let submitBtn = document.getElementById("run") | ||
|
||
// console.log("listening to events"); | ||
submitBtn.addEventListener("click", function () { | ||
const submission = setInterval(()=> { | ||
let output = document.getElementsByClassName("out")[0].innerText; | ||
if(output.includes("Correct Answer")){ | ||
// clear timeout | ||
clearTimeout(gfgLoader); | ||
clearTimeout(submission); | ||
// get data | ||
title = findTitle().trim(); | ||
difficulty = findDifficulty(); | ||
problemStatement = getProblemStatement(); | ||
code = getCode(); | ||
language = findGfgLanguage() | ||
|
||
let probName = title + ` - GFG`; | ||
|
||
problemStatement = `# ${title}\n## ${difficulty}\n` + problemStatement; | ||
|
||
if(language !== null){ | ||
chrome.storage.local.get('stats', (s) => { | ||
const { stats } = s; | ||
const filePath = title + "gfg" + 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, | ||
title+language, | ||
SUBMIT_MSG, | ||
'upload', | ||
); | ||
}, 2000); | ||
} | ||
}); | ||
} | ||
|
||
// proceeed to github | ||
} else if(output.includes("Compilation Error")){ | ||
// clear timeout and do nothing | ||
clearTimeout(submission); | ||
} | ||
},3000) | ||
}) | ||
}, 1000); |