From 5e378a6e0908636fba6ea91e1defa0321b863d1c Mon Sep 17 00:00:00 2001 From: Jeremy Date: Wed, 2 Dec 2020 11:52:59 -0800 Subject: [PATCH] Run prettier/eslint --- .eslintrc | 6 + css/popup.css | 61 ++--- css/welcome.css | 121 +++++---- package.json | 5 +- popup.html | 151 ++++++----- popup.js | 116 +++++---- scripts/authorize.js | 190 +++++++------- scripts/background.js | 73 +++--- scripts/leetcode.js | 374 ++++++++++++++------------- scripts/oauth2.js | 67 +++-- scripts/welcome.js | 579 ++++++++++++++++++++++-------------------- welcome.html | 197 ++++++++------ 12 files changed, 1032 insertions(+), 908 deletions(-) diff --git a/.eslintrc b/.eslintrc index 05b76898..c1133652 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,10 @@ { + "env": { + "browser": true, + "commonjs": true, + "es6": true, + "jquery": true + }, "extends": ["airbnb", "prettier"], "plugins": ["prettier"], "rules": { diff --git a/css/popup.css b/css/popup.css index e5b608a4..5c38772f 100644 --- a/css/popup.css +++ b/css/popup.css @@ -1,33 +1,34 @@ body { - width: 350px; - background-color: #dfd9d99d !important; - padding: 10px; - text-align:center; - transition: background-color .75s ease; - font-size:13px; - - font-family: "Helvetica Neue", "Lucida Grande", sans-serif; - } + width: 350px; + background-color: #dfd9d99d !important; + padding: 10px; + text-align: center; + transition: background-color 0.75s ease; + font-size: 13px; - #title { - font-family: "Norwester", "Helvetica Neue", "Lucida Grande", sans-serif; - font-size: 30px; - font-weight: bold; - margin-bottom: 0px; - margin-top: 10px; - } - - #caption { - font-family: "Norwester", "Helvetica Neue", "Lucida Grande", sans-serif; - font-size: 14px; - font-weight: normal; - margin-bottom: 0px; - } + font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; +} - .onboarding - { - font-family: sans-serif; - border-top: solid black 1px; - font-weight: 500; - font-size: medium; - } \ No newline at end of file +#title { + font-family: 'Norwester', 'Helvetica Neue', 'Lucida Grande', + sans-serif; + font-size: 30px; + font-weight: bold; + margin-bottom: 0px; + margin-top: 10px; +} + +#caption { + font-family: 'Norwester', 'Helvetica Neue', 'Lucida Grande', + sans-serif; + font-size: 14px; + font-weight: normal; + margin-bottom: 0px; +} + +.onboarding { + font-family: sans-serif; + border-top: solid black 1px; + font-weight: 500; + font-size: medium; +} diff --git a/css/welcome.css b/css/welcome.css index d89f4306..9ecc7cac 100644 --- a/css/welcome.css +++ b/css/welcome.css @@ -1,66 +1,65 @@ body { - background-color: #000000 !important; - padding: 10px; - text-align:center; - transition: background-color .75s ease; - font-family: "Helvetica Neue", "Lucida Grande", sans-serif; - } + background-color: #000000 !important; + padding: 10px; + text-align: center; + transition: background-color 0.75s ease; + font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; +} - #title { - font-family: "Norwester", "Helvetica Neue", "Lucida Grande", sans-serif; - font-size: 5em; - font-weight: bold; - margin-bottom: 0px; - margin-top: 10px; - } - - .caption { - font-family: "Norwester", "Helvetica Neue", "Lucida Grande", sans-serif; - font-size: 1.5em; - font-weight: normal !important; - margin-top: 3px !important; - margin-bottom: 0px; - } - #error - { - font-family: "Norwester", "Helvetica Neue", "Lucida Grande", sans-serif; - font-weight: normal !important; - color: red !important; - margin-bottom: 0px; - } - - #success - { - font-family: "Norwester", "Helvetica Neue", "Lucida Grande", sans-serif; - font-size: 1.1em; - font-weight: normal !important; - color: green !important; - margin-bottom: 0px; - } +#title { + font-family: 'Norwester', 'Helvetica Neue', 'Lucida Grande', + sans-serif; + font-size: 5em; + font-weight: bold; + margin-bottom: 0px; + margin-top: 10px; +} - #unlink - { - font-family: "Norwester", "Helvetica Neue", "Lucida Grande", sans-serif; - font-size: 1.1em; - font-weight: normal !important; - color: green !important; - margin-bottom: 0px; - } +.caption { + font-family: 'Norwester', 'Helvetica Neue', 'Lucida Grande', + sans-serif; + font-size: 1.5em; + font-weight: normal !important; + margin-top: 3px !important; + margin-bottom: 0px; +} +#error { + font-family: 'Norwester', 'Helvetica Neue', 'Lucida Grande', + sans-serif; + font-weight: normal !important; + color: red !important; + margin-bottom: 0px; +} - p - { - color: white !important; - } - .onboarding - { - font-family: sans-serif; - border-top: solid white 1px; - font-weight: 500; - font-size: medium; - } +#success { + font-family: 'Norwester', 'Helvetica Neue', 'Lucida Grande', + sans-serif; + font-size: 1.1em; + font-weight: normal !important; + color: green !important; + margin-bottom: 0px; +} - a - { - color: white !important; - cursor: pointer; - } \ No newline at end of file +#unlink { + font-family: 'Norwester', 'Helvetica Neue', 'Lucida Grande', + sans-serif; + font-size: 1.1em; + font-weight: normal !important; + color: green !important; + margin-bottom: 0px; +} + +p { + color: white !important; +} +.onboarding { + font-family: sans-serif; + border-top: solid white 1px; + font-weight: 500; + font-size: medium; +} + +a { + color: white !important; + cursor: pointer; +} diff --git a/package.json b/package.json index 2e885ea1..bbf93031 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,10 @@ "description": "Automatically integrate your code with LeetCode and GitHub", "scripts": { "setup": "npm i", - "test": "eslint **/*.{js,jsx,ts,tsx} --color && prettier --check **/*.{js,jsx,ts,tsx,css,html}", "format": "prettier --write **/*.{js,jsx,ts,tsx,css,html}", - "lint": "eslint **/*.{js,jsx,ts,tsx} --fix" + "format-test": "prettier --check **/*.{js,jsx,ts,tsx,css,html}", + "lint": "eslint **/*.{js,jsx,ts,tsx} --fix", + "lint-test": "eslint **/*.{js,jsx,ts,tsx} --color" }, "devDependencies": { "eslint": "^7.2.0", diff --git a/popup.html b/popup.html index 7d6eb81a..ac0034b1 100644 --- a/popup.html +++ b/popup.html @@ -1,69 +1,100 @@ - - - - - - + + - + + + - + +
+
+

+ LeetHub +

+

Sync your code from LeetCode to GitHub

+
+ + -
-
-

LeetHub

-

Sync your code from LeetCode to GitHub

-
- - - - - - - - - -
- - -
- - - - -
+ + +
+ + +
+ + + + +
+
- - \ No newline at end of file + + + + diff --git a/popup.js b/popup.js index b6aa9494..5414950d 100644 --- a/popup.js +++ b/popup.js @@ -1,66 +1,64 @@ -var action = false; +let action = false; -$("#authenticate").on('click', ()=>{ - if(action) - { - oAuth2.begin(); - } +$('#authenticate').on('click', () => { + if (action) { + oAuth2.begin(); + } }); /* Get URL for welcome page */ -$("#welcome_URL").attr("href", `chrome-extension://${chrome.runtime.id}/welcome.html`); -$("#hook_URL").attr("href", `chrome-extension://${chrome.runtime.id}/welcome.html`); +$('#welcome_URL').attr( + 'href', + `chrome-extension://${chrome.runtime.id}/welcome.html`, +); +$('#hook_URL').attr( + 'href', + `chrome-extension://${chrome.runtime.id}/welcome.html`, +); -chrome.storage.sync.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"; - - var xhr = new XMLHttpRequest(); - xhr.addEventListener('readystatechange', function(event) { - if(xhr.readyState == 4) { - if(xhr.status == 200) { - /* Show MAIN FEATURES */ - chrome.storage.sync.get("mode_type", data=>{ - if(data && data.mode_type == "commit") - { - $("#commit_mode").show(); - /* Get problems solved count */ - chrome.storage.sync.get("stats", psolved=>{ - psolved = psolved.stats; - if(psolved && psolved["solved"]) - { - $("#p_solved").text(psolved["solved"]); - } - }); - } - else - { - $("#hook_mode").show(); - } - }); - } - else if(xhr.status == 401) //bad oAuth - { - //reset token and redirect to authorization process again! - chrome.storage.sync.set({"leethub_token": null}, data=>{ - console.log("BAD oAuth!!! Redirecting back to oAuth process"); - action = true; - $("#auth_mode").show(); - }); +chrome.storage.sync.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 (event) { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + /* Show MAIN FEATURES */ + chrome.storage.sync.get('mode_type', (data) => { + if (data && data.mode_type == 'commit') { + $('#commit_mode').show(); + /* Get problems solved count */ + chrome.storage.sync.get('stats', (psolved) => { + psolved = psolved.stats; + if (psolved && psolved.solved) { + $('#p_solved').text(psolved.solved); } + }); + } else { + $('#hook_mode').show(); } - }); - xhr.open('GET', AUTHENTICATION_URL, true); - xhr.setRequestHeader("Authorization", `token ${token}`); - xhr.send(); - } - -}); \ No newline at end of file + }); + } else if (xhr.status == 401) { + // bad oAuth + // reset token and redirect to authorization process again! + chrome.storage.sync.set({ leethub_token: null }, (data) => { + console.log( + 'BAD oAuth!!! Redirecting back to oAuth process', + ); + action = true; + $('#auth_mode').show(); + }); + } + } + }); + xhr.open('GET', AUTHENTICATION_URL, true); + xhr.setRequestHeader('Authorization', `token ${token}`); + xhr.send(); + } +}); diff --git a/scripts/authorize.js b/scripts/authorize.js index 05d472a5..24060c71 100644 --- a/scripts/authorize.js +++ b/scripts/authorize.js @@ -3,109 +3,107 @@ IMPLEMENTATION OF AUTHENTICATION ROUTE AFTER REDIRECT FROM GITHUB. */ -var local_auth = { +const local_auth = { + /** + * Initialize + */ + init() { + this.KEY = 'leethub_token'; + this.ACCESS_TOKEN_URL = + 'https://github.com/login/oauth/access_token'; + this.AUTHORIZATION_URL = + 'https://github.com/login/oauth/authorize'; + this.CLIENT_ID = 'beb4f0aa19ab8faf5004'; + this.CLIENT_SECRET = '843f835609c7ef02ef0f2f1645bc49514c0e65a6'; + this.REDIRECT_URL = 'https://github.com/'; // for example, https://github.com + this.SCOPES = ['repo']; + }, - /** - * Initialize - */ - init: function() { - - this.KEY = "leethub_token"; - this.ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"; - this.AUTHORIZATION_URL = "https://github.com/login/oauth/authorize"; - this.CLIENT_ID = "beb4f0aa19ab8faf5004"; - this.CLIENT_SECRET = "843f835609c7ef02ef0f2f1645bc49514c0e65a6"; - this.REDIRECT_URL = "https://github.com/"; //for example, https://github.com - this.SCOPES = ['repo']; - }, + /** + * Parses Access Code + * + * @param url The url containing the access code. + */ + parseAccessCode(url) { + if (url.match(/\?error=(.+)/)) { + chrome.tabs.getCurrent(function (tab) { + chrome.tabs.remove(tab.id, function () {}); + }); + } else { + this.requestToken(url.match(/\?code=([\w\/\-]+)/)[1]); + } + }, - /** - * Parses Access Code - * - * @param url The url containing the access code. - */ - parseAccessCode: function(url) { - if(url.match(/\?error=(.+)/)) { - chrome.tabs.getCurrent(function(tab) { - chrome.tabs.remove(tab.id, function(){}); - }); - } - else { - this.requestToken(url.match(/\?code=([\w\/\-]+)/)[1]); - } - }, + /** + * Request Token + * + * @param code The access code returned by provider. + */ + requestToken(code) { + const that = this; + const data = new FormData(); + data.append('client_id', this.CLIENT_ID); + data.append('client_secret', this.CLIENT_SECRET); + data.append('code', code); - /** - * Request Token - * - * @param code The access code returned by provider. - */ - requestToken: function(code) { - var that = this; - var data = new FormData(); - data.append('client_id', this.CLIENT_ID); - data.append('client_secret', this.CLIENT_SECRET); - data.append('code', code); + const xhr = new XMLHttpRequest(); + xhr.addEventListener('readystatechange', function (event) { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + that.finish( + xhr.responseText.match(/access_token=([^&]*)/)[1], + ); + } else { + chrome.runtime.sendMessage({ + closeWebPage: true, + isSuccess: false, + }); + } + } + }); + xhr.open('POST', this.ACCESS_TOKEN_URL, true); + xhr.send(data); + }, - var xhr = new XMLHttpRequest(); - xhr.addEventListener('readystatechange', function(event) { - if(xhr.readyState == 4) { - if(xhr.status == 200) { - that.finish(xhr.responseText.match(/access_token=([^&]*)/)[1]); - } - else { - chrome.runtime.sendMessage({ - closeWebPage: true, - isSuccess: false - }); - } - } - }); - xhr.open('POST', this.ACCESS_TOKEN_URL, true); - xhr.send(data); - }, + /** + * Finish + * + * @param token The OAuth2 token given to the application from the provider. + */ + finish(token) { + /* Get username */ + // To validate user, load user object from GitHub. + const AUTHENTICATION_URL = 'https://api.github.com/user'; - /** - * Finish - * - * @param token The OAuth2 token given to the application from the provider. - */ - finish: function(token) { - /* Get username */ - // To validate user, load user object from GitHub. - const AUTHENTICATION_URL = "https://api.github.com/user"; - - var xhr = new XMLHttpRequest(); - xhr.addEventListener('readystatechange', function(event) { - if(xhr.readyState == 4) { - if(xhr.status == 200) { - let username = JSON.parse(xhr.responseText)['login']; - chrome.runtime.sendMessage({ - closeWebPage: true, - isSuccess: true, - token: token, - username: username, - KEY: this.KEY - }); - } - } - }); - xhr.open('GET', AUTHENTICATION_URL, true); - xhr.setRequestHeader("Authorization", `token ${token}`); - xhr.send(); - } + const xhr = new XMLHttpRequest(); + xhr.addEventListener('readystatechange', function (event) { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + const username = JSON.parse(xhr.responseText).login; + chrome.runtime.sendMessage({ + closeWebPage: true, + isSuccess: true, + token, + username, + KEY: this.KEY, + }); + } + } + }); + xhr.open('GET', AUTHENTICATION_URL, true); + xhr.setRequestHeader('Authorization', `token ${token}`); + xhr.send(); + }, }; -local_auth.init(); //load params. -var link = window.location.href; +local_auth.init(); // load params. +const link = window.location.href; /* Check for open pipe */ -if(window.location.host == "github.com") -{ - chrome.storage.sync.get("pipe_leethub", data=>{ - if(data && data.pipe_leethub) - { - local_auth.parseAccessCode(link); - } - }); +if (window.location.host == 'github.com') { + chrome.storage.sync.get('pipe_leethub', (data) => { + if (data && data.pipe_leethub) { + local_auth.parseAccessCode(link); + } + }); } diff --git a/scripts/background.js b/scripts/background.js index eb576471..68a60dae 100644 --- a/scripts/background.js +++ b/scripts/background.js @@ -1,36 +1,49 @@ function handleMessage(request, sender, sendResponse) { - if(request && request.closeWebPage == true && request.isSuccess == true) - { - /* Set username */ - chrome.storage.sync.set({"leethub_username": request.username}, (data)=>{ - window['localStorage']['leethub_username'] = request.username; - }); - - /* Set token */ - chrome.storage.sync.set({"leethub_token": request.token}, (data)=>{ - window['localStorage'][request.KEY] = request.token; - }); + if ( + request && + request.closeWebPage == true && + request.isSuccess == true + ) { + /* Set username */ + chrome.storage.sync.set( + { leethub_username: request.username }, + (data) => { + window.localStorage.leethub_username = request.username; + }, + ); - /* Close pipe */ - chrome.storage.sync.set({"pipe_leethub": false}, data=>{ - console.log("Closed pipe."); - }); + /* Set token */ + chrome.storage.sync.set( + { leethub_token: request.token }, + (data) => { + window.localStorage[request.KEY] = request.token; + }, + ); - chrome.tabs.getSelected(null, function(tab) { - chrome.tabs.remove(tab.id); - }); + /* Close pipe */ + chrome.storage.sync.set({ pipe_leethub: false }, (data) => { + console.log('Closed pipe.'); + }); - /* Go to onboarding for UX */ - let url_onboarding = `chrome-extension://${chrome.runtime.id}/welcome.html`; - chrome.tabs.create({url: url_onboarding, selected: true}); //creates new tab - } - else if(request && request.closeWebPage == true && request.isSuccess == true) - { - alert("Something went wrong while trying to authenticate your profile!"); - chrome.tabs.getSelected(null, function(tab) { - chrome.tabs.remove(tab.id); - }); - } + chrome.tabs.getSelected(null, function (tab) { + chrome.tabs.remove(tab.id); + }); + + /* Go to onboarding for UX */ + const url_onboarding = `chrome-extension://${chrome.runtime.id}/welcome.html`; + chrome.tabs.create({ url: url_onboarding, selected: true }); // creates new tab + } else if ( + request && + request.closeWebPage == true && + request.isSuccess == true + ) { + alert( + 'Something went wrong while trying to authenticate your profile!', + ); + chrome.tabs.getSelected(null, function (tab) { + chrome.tabs.remove(tab.id); + }); + } } -chrome.runtime.onMessage.addListener(handleMessage); \ No newline at end of file +chrome.runtime.onMessage.addListener(handleMessage); diff --git a/scripts/leetcode.js b/scripts/leetcode.js index a96ede90..2be2ee59 100644 --- a/scripts/leetcode.js +++ b/scripts/leetcode.js @@ -1,210 +1,226 @@ -var code = null; +let code = null; /* Enum for languages supported by LeetCode. */ -const languages = -{ - "Python" : ".py", - "Python3" : ".py", - "C++" : ".cpp", - "C" : ".c", - "Java" : ".java", - "C#" : ".cs", - "JavaScript" : ".js", - "Ruby" : ".rb", - "Swift" : ".swift", - "Go" : ".go", - "Kotlin" : ".kt", - "Scala" : ".scala", - "Rust" : ".rs", - "PHP" : ".php", - "TypeScript" : ".ts", - "MySQL" : ".sql", - "MS SQL Server" : ".sql", - "Oracle" : ".sql" +const languages = { + Python: '.py', + Python3: '.py', + 'C++': '.cpp', + C: '.c', + Java: '.java', + 'C#': '.cs', + JavaScript: '.js', + Ruby: '.rb', + Swift: '.swift', + Go: '.go', + Kotlin: '.kt', + Scala: '.scala', + Rust: '.rs', + PHP: '.php', + TypeScript: '.ts', + MySQL: '.sql', + 'MS SQL Server': '.sql', + Oracle: '.sql', }; -var TimeoutHandle = null; +const TimeoutHandle = null; const loader = setInterval(() => { - success_tag = document.getElementsByClassName("success__3Ai7"); - if(success_tag != undefined && success_tag.length > 0 && success_tag[0].innerText.trim() == "Success") - { - code = parse_code(); - probStatement = parse_question(); - } - if(code != null && probStatement != null) - { - clearTimeout(loader); - const problem_name = window.location.pathname.split("/")[2]; //must be true. - let language = find_language(); - if(language != null) - { - upload_git(btoa(unescape(encodeURIComponent(code))), problem_name, problem_name+language); //Encode `code` to base64 - - /* @TODO: Change this setTimeout to Promise */ - setTimeout(function(){upload_git(btoa(unescape(encodeURIComponent(probStatement))), problem_name, 'README.md')}, 2000); - } + success_tag = document.getElementsByClassName('success__3Ai7'); + if ( + success_tag != undefined && + success_tag.length > 0 && + success_tag[0].innerText.trim() == 'Success' + ) { + code = parse_code(); + probStatement = parse_question(); + } + if (code != null && probStatement != null) { + clearTimeout(loader); + const problem_name = window.location.pathname.split('/')[2]; // must be true. + const language = find_language(); + if (language != null) { + upload_git( + btoa(unescape(encodeURIComponent(code))), + problem_name, + problem_name + language, + ); // Encode `code` to base64 + + /* @TODO: Change this setTimeout to Promise */ + setTimeout(function () { + upload_git( + btoa(unescape(encodeURIComponent(probStatement))), + problem_name, + 'README.md', + ); + }, 2000); } + } }, 1000); /* Get file extension for submission */ function find_language() { - let tag = [... document.getElementsByClassName("ant-select-selection-selected-value")]; - if(tag && tag.length > 0) - { - for (let i = 0; i < tag.length; i++) { - const elem = tag[i].textContent; - if(elem != undefined && languages[elem] != undefined) - { - return languages[elem]; //should generate respective file extension - } - } - return null; + const tag = [ + ...document.getElementsByClassName( + 'ant-select-selection-selected-value', + ), + ]; + if (tag && tag.length > 0) { + for (let i = 0; i < tag.length; i++) { + const elem = tag[i].textContent; + if (elem != undefined && languages[elem] != undefined) { + return languages[elem]; // should generate respective file extension + } } + return null; + } } function upload_git(code, problem_name, filename) { - - /* Get necessary payload data */ - chrome.storage.sync.get("leethub_token", token=>{ - token = token.leethub_token; - if(token) - { - chrome.storage.sync.get("mode_type", mode=>{ - mode = mode.mode_type; - if(mode == "commit") - { - /* Get hook */ - chrome.storage.sync.get("leethub_hook", hook=>{ - hook = hook.leethub_hook; - if(hook) - { - /* Get SHA, if it exists */ - - /* to get unique key */ - const filePath = problem_name+filename - chrome.storage.sync.get("stats", stats=>{ - stats = stats.stats; - let sha = null; - - if(stats != undefined && stats["sha"] != undefined && stats["sha"][filePath] != undefined) - { - sha = stats["sha"][filePath]; - } - /* Upload to git. */ - upload(token, hook, code, problem_name, filename, sha); - }); - } - }); + /* Get necessary payload data */ + chrome.storage.sync.get('leethub_token', (token) => { + token = token.leethub_token; + if (token) { + chrome.storage.sync.get('mode_type', (mode) => { + mode = mode.mode_type; + if (mode == 'commit') { + /* Get hook */ + chrome.storage.sync.get('leethub_hook', (hook) => { + hook = hook.leethub_hook; + if (hook) { + /* Get SHA, if it exists */ + + /* to get unique key */ + const filePath = problem_name + filename; + chrome.storage.sync.get('stats', (stats) => { + stats = stats.stats; + let sha = null; + + if ( + stats != undefined && + stats.sha != undefined && + stats.sha[filePath] != undefined + ) { + sha = stats.sha[filePath]; } - }); + /* Upload to git. */ + upload( + token, + hook, + code, + problem_name, + filename, + sha, + ); + }); + } + }); } - }); + }); + } + }); } - /* Main function for uploading code to GitHub repo */ -var upload = (token, hook, code, directory, filename, sha)=>{ - - // To validate user, load user object from GitHub. - const URL = "https://api.github.com/repos/" + hook + "/contents/" +directory+'/'+ filename; - - /* Define Payload */ - var data = { - 'message': `working commit - Created using LeetHub`, - 'content': code - } - if(sha != null) - { - data["sha"] = sha; //get sha for files that already exist in the gh file system. +var upload = (token, hook, code, directory, filename, sha) => { + // To validate user, load user object from GitHub. + const URL = `https://api.github.com/repos/${hook}/contents/${directory}/${filename}`; + + /* Define Payload */ + let data = { + message: `working commit - Created using LeetHub`, + content: code, + }; + if (sha != null) { + data.sha = sha; // get sha for files that already exist in the gh file system. + } + + data = JSON.stringify(data); + + const xhr = new XMLHttpRequest(); + xhr.addEventListener('readystatechange', function (event) { + if (xhr.readyState == 4) { + if (xhr.status == 200 || xhr.status == 201) { + sha = JSON.parse(xhr.responseText).content.sha; // get updated SHA. + + chrome.storage.sync.get('stats', (data) => { + let { stats } = data; + if (stats == null || stats == {} || stats == undefined) { + // create stats object + stats = {}; + stats.solved = 0; + stats.sha = {}; + } + const filePath = directory + filename; + // Only increment solved problems statistics once + // New submission commits twice (README and problem) + if (filename !== 'README.md') { + stats.solved += 1; + } + stats.sha[filePath] = sha; // update sha key. + chrome.storage.sync.set({ stats }, (m_data) => { + console.log( + `Successfully committed ${filename} to github`, + ); + }); + }); + } } - - data = JSON.stringify(data); - - var xhr = new XMLHttpRequest(); - xhr.addEventListener('readystatechange', function(event) { - if(xhr.readyState == 4) { - - if(xhr.status == 200 || xhr.status == 201) { - sha = JSON.parse(xhr.responseText)["content"]["sha"]; //get updated SHA. - - chrome.storage.sync.get("stats", data=>{ - let stats = data.stats; - if(stats == null || stats == {} || stats == undefined) //create stats object - { - stats = {}; - stats["solved"] = 0; - stats["sha"] = {}; - } - const filePath = directory+filename; - // Only increment solved problems statistics once - // New submission commits twice (README and problem) - if (filename !== "README.md") { - stats["solved"] += 1; - } - stats["sha"][filePath] = sha; //update sha key. - chrome.storage.sync.set({"stats" : stats}, m_data=>{ - console.log(`Successfully committed ${filename} to github`); - }); - }); - } - } - }); - xhr.open('PUT', URL, true); - xhr.setRequestHeader("Authorization", `token ${token}`); - xhr.setRequestHeader("Accept", "application/vnd.github.v3+json"); - xhr.send(data); -} + }); + xhr.open('PUT', URL, true); + xhr.setRequestHeader('Authorization', `token ${token}`); + xhr.setRequestHeader('Accept', 'application/vnd.github.v3+json'); + xhr.send(data); +}; /* Main parser function for the code */ -function parse_code() -{ - elem = document.getElementsByClassName("CodeMirror-code"); - if(elem != undefined && elem.length > 0) - { - elem = elem[0]; - parsed_code = ""; - text_arr = elem.innerText.split("\n"); - for(let i = 1; i < text_arr.length; i+=2) - { - parsed_code += text_arr[i] + "\n"; - } - return parsed_code; +function parse_code() { + elem = document.getElementsByClassName('CodeMirror-code'); + if (elem != undefined && elem.length > 0) { + elem = elem[0]; + parsed_code = ''; + text_arr = elem.innerText.split('\n'); + for (let i = 1; i < text_arr.length; i += 2) { + parsed_code += `${text_arr[i]}\n`; } - return null; + return parsed_code; + } + return null; } /* Util function to check if an element exists */ -function checkElem(elem){ - return (elem && elem.length>0); +function checkElem(elem) { + return elem && elem.length > 0; } /* Parser function for the question and tags */ -function parse_question(){ - const questionElem = document.getElementsByClassName("content__u3I1 question-content__JfgR"); - if(!checkElem(questionElem)){ - return null; - } - const qbody = questionElem[0].innerHTML; - - // Problem title. - const qtitlte = document.getElementsByClassName('css-v3d350')[0].innerHTML; - - var difficulty = ''; - - // Problem difficulty, each problem difficulty has its own class. - const isHard = document.getElementsByClassName('css-t42afm'); - const isMedium = document.getElementsByClassName('css-dcmtd5'); - const isEasy = document.getElementsByClassName('css-14oi08n'); - - if(checkElem(isEasy)){ - difficulty='Easy'; - }else if(checkElem(isMedium)){ - difficulty='Medium'; - }else if(checkElem(isHard)){ - difficulty='Hard' - } - // Final formatting of the contents of the README for each problem - const markdown = '

' + qtitlte + '

' + difficulty + '


' + qbody; - return markdown; +function parse_question() { + const questionElem = document.getElementsByClassName( + 'content__u3I1 question-content__JfgR', + ); + if (!checkElem(questionElem)) { + return null; + } + const qbody = questionElem[0].innerHTML; + + // Problem title. + const qtitlte = document.getElementsByClassName('css-v3d350')[0] + .innerHTML; + + let difficulty = ''; + + // Problem difficulty, each problem difficulty has its own class. + const isHard = document.getElementsByClassName('css-t42afm'); + const isMedium = document.getElementsByClassName('css-dcmtd5'); + const isEasy = document.getElementsByClassName('css-14oi08n'); + + if (checkElem(isEasy)) { + difficulty = 'Easy'; + } else if (checkElem(isMedium)) { + difficulty = 'Medium'; + } else if (checkElem(isHard)) { + difficulty = 'Hard'; + } + // Final formatting of the contents of the README for each problem + const markdown = `

${qtitlte}

${difficulty}


${qbody}`; + return markdown; } diff --git a/scripts/oauth2.js b/scripts/oauth2.js index ac96f40b..565f3dc2 100644 --- a/scripts/oauth2.js +++ b/scripts/oauth2.js @@ -1,40 +1,39 @@ -var oAuth2 = { +const oAuth2 = { + /** + * Initialize + */ + init() { + this.KEY = 'leethub_token'; + this.ACCESS_TOKEN_URL = + 'https://github.com/login/oauth/access_token'; + this.AUTHORIZATION_URL = + 'https://github.com/login/oauth/authorize'; + this.CLIENT_ID = 'beb4f0aa19ab8faf5004'; + this.CLIENT_SECRET = '843f835609c7ef02ef0f2f1645bc49514c0e65a6'; + this.REDIRECT_URL = 'https://github.com/'; // for example, https://github.com + this.SCOPES = ['repo']; + }, - /** - * Initialize - */ - init: function() { - - this.KEY = "leethub_token"; - this.ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"; - this.AUTHORIZATION_URL = "https://github.com/login/oauth/authorize"; - this.CLIENT_ID = "beb4f0aa19ab8faf5004"; - this.CLIENT_SECRET = "843f835609c7ef02ef0f2f1645bc49514c0e65a6"; - this.REDIRECT_URL = "https://github.com/"; //for example, https://github.com - this.SCOPES = ['repo']; - }, + /** + * Begin + */ + begin() { + this.init(); // secure token params. - /** - * Begin - */ - begin: function() { - this.init(); //secure token params. + let url = `${this.AUTHORIZATION_URL}?client_id=${this.CLIENT_ID}&redirect_uri${this.REDIRECT_URL}&scope=`; - var url = this.AUTHORIZATION_URL + "?client_id=" + this.CLIENT_ID + "&redirect_uri" + this.REDIRECT_URL + "&scope="; - - for(var i in this.SCOPES) { - url += this.SCOPES[i]; - } - chrome.storage.sync.set({"pipe_leethub" : true}, data=>{ //opening pipe temporarily - - chrome.tabs.create({url: url, selected: true}, function(data) { - window.close(); - chrome.tabs.getCurrent(function(tab) { - chrome.tabs.remove(tab.id, function(){}); - }); - }); + for (const i in this.SCOPES) { + url += this.SCOPES[i]; + } + chrome.storage.sync.set({ pipe_leethub: true }, (data) => { + // opening pipe temporarily + chrome.tabs.create({ url, selected: true }, function (data) { + window.close(); + chrome.tabs.getCurrent(function (tab) { + chrome.tabs.remove(tab.id, function () {}); }); - - } + }); + }); + }, }; diff --git a/scripts/welcome.js b/scripts/welcome.js index b0f3f4b7..c91eb097 100644 --- a/scripts/welcome.js +++ b/scripts/welcome.js @@ -1,174 +1,163 @@ /* Check for value of select tag, Get Started disabled by default */ $('#type').on('change', function (e) { - let valueSelected = this.value; - if(valueSelected) - { - $("#hook_button").attr("disabled", false); - } - else - { - $("#hook_button").attr("disabled", true); - } + const valueSelected = this.value; + if (valueSelected) { + $('#hook_button').attr('disabled', false); + } else { + $('#hook_button').attr('disabled', true); + } }); +$('#hook_button').on('click', () => { + /* on click should generate: 1) option 2) repository name */ + if (!option()) { + $('#error').text( + 'No option selected - Pick an option from dropdown menu below that best suits you!', + ); + $('#error').show(); + } else if (!repository_name()) { + $('#error').text( + 'No repository name added - Enter the name of your repository!', + ); + $('#name').focus(); + $('#error').show(); + } else { + $('#error').hide(); + $('#success').text('Attempting to create Hook... Please wait.'); + $('#success').show(); - -$("#hook_button").on('click', ()=>{ - /* on click should generate: 1) option 2) repository name */ - if(!option()) - { - $("#error").text("No option selected - Pick an option from dropdown menu below that best suits you!"); - $("#error").show(); - } - else if(!repository_name()) - { - $("#error").text("No repository name added - Enter the name of your repository!"); - $("#name").focus(); - $("#error").show(); - } - else - { - $("#error").hide(); - $("#success").text("Attempting to create Hook... Please wait."); - $("#success").show(); - - /* + /* Perform processing - step 1: Check if current stage == hook. - step 2: store repo name as repo_name in chrome storage. - step 3: if (1), POST request to repo_name (iff option = create new repo) ; else display error message. - step 4: if proceed from 3, hide hook_mode and display commit_mode (show stats e.g: files pushed/questions-solved/leaderboard) */ - chrome.storage.sync.get("leethub_token", (data)=>{ - const token = data.leethub_token; - if(token == null || token == undefined) - { - /* Not authorized yet. */ - $("#error").text("Authorization error - Grant LeetHub access to your GitHub account to continue (launch extension to proceed)"); - $("#error").show(); - $("#success").hide(); - } - else - { - if(option() == "new") - { - create_repo(token, repository_name()); - } - else - { - chrome.storage.sync.get("leethub_username", (data)=>{ - let username = data.leethub_username; - if(!username) - { - /* Improper authorization. */ - $("#error").text("Improper Authorization error - Grant LeetHub access to your GitHub account to continue (launch extension to proceed)"); - $("#error").show(); - $("#success").hide(); - } - else - { - link_repo(token, username + "/" + repository_name(), false); - } - }); - - } - } + chrome.storage.sync.get('leethub_token', (data) => { + const token = data.leethub_token; + if (token == null || token == undefined) { + /* Not authorized yet. */ + $('#error').text( + 'Authorization error - Grant LeetHub access to your GitHub account to continue (launch extension to proceed)', + ); + $('#error').show(); + $('#success').hide(); + } else if (option() == 'new') { + create_repo(token, repository_name()); + } else { + chrome.storage.sync.get('leethub_username', (data) => { + const username = data.leethub_username; + if (!username) { + /* Improper authorization. */ + $('#error').text( + 'Improper Authorization error - Grant LeetHub access to your GitHub account to continue (launch extension to proceed)', + ); + $('#error').show(); + $('#success').hide(); + } else { + link_repo( + token, + `${username}/${repository_name()}`, + false, + ); + } }); - - } + } + }); + } }); -$("#unlink a").on('click', () => { - unlink_repo(); - $("#unlink").hide(); - $("#success").text("Successfully unlinked your current git repo. Please create/link a new hook."); +$('#unlink a').on('click', () => { + unlink_repo(); + $('#unlink').hide(); + $('#success').text( + 'Successfully unlinked your current git repo. Please create/link a new hook.', + ); }); /* Detect mode type */ -chrome.storage.sync.get("mode_type", data=>{ - const mode = data.mode_type; +chrome.storage.sync.get('mode_type', (data) => { + const mode = data.mode_type; - if(mode && mode == "commit") - { - /* Check if still access to repo */ - chrome.storage.sync.get("leethub_token", data=>{ - const token = data.leethub_token; - if(token == null || token == undefined) - { - /* Not authorized yet. */ - $("#error").text("Authorization error - Grant LeetHub access to your GitHub account to continue (click LeetHub extension on the top right to proceed)"); - $("#error").show(); - $("#success").hide(); - /* Hide accordingly */ - document.getElementById("hook_mode").style.display = "inherit"; - document.getElementById("commit_mode").style.display = "none"; - } - else - { - /* Get access to repo */ - chrome.storage.sync.get("leethub_hook", repo_name=>{ - repo_name = repo_name.leethub_hook; - if(!repo_name) - { - /* Not authorized yet. */ - $("#error").text("Improper Authorization error - Grant LeetHub access to your GitHub account to continue (click LeetHub extension on the top right to proceed)"); - $("#error").show(); - $("#success").hide(); - /* Hide accordingly */ - document.getElementById("hook_mode").style.display = "inherit"; - document.getElementById("commit_mode").style.display = "none"; - } - else - { - /* Username exists, at least in storage. Confirm this */ - link_repo(token, repo_name); - } - }); - } + if (mode && mode == 'commit') { + /* Check if still access to repo */ + chrome.storage.sync.get('leethub_token', (data) => { + const token = data.leethub_token; + if (token == null || token == undefined) { + /* Not authorized yet. */ + $('#error').text( + 'Authorization error - Grant LeetHub access to your GitHub account to continue (click LeetHub extension on the top right to proceed)', + ); + $('#error').show(); + $('#success').hide(); + /* Hide accordingly */ + document.getElementById('hook_mode').style.display = + 'inherit'; + document.getElementById('commit_mode').style.display = 'none'; + } else { + /* Get access to repo */ + chrome.storage.sync.get('leethub_hook', (repo_name) => { + repo_name = repo_name.leethub_hook; + if (!repo_name) { + /* Not authorized yet. */ + $('#error').text( + 'Improper Authorization error - Grant LeetHub access to your GitHub account to continue (click LeetHub extension on the top right to proceed)', + ); + $('#error').show(); + $('#success').hide(); + /* Hide accordingly */ + document.getElementById('hook_mode').style.display = + 'inherit'; + document.getElementById('commit_mode').style.display = + 'none'; + } else { + /* Username exists, at least in storage. Confirm this */ + link_repo(token, repo_name); + } }); + } + }); - document.getElementById("hook_mode").style.display = "none"; - document.getElementById("commit_mode").style.display = "inherit"; - } - else - { - document.getElementById("hook_mode").style.display = "inherit"; - document.getElementById("commit_mode").style.display = "none"; - } + document.getElementById('hook_mode').style.display = 'none'; + document.getElementById('commit_mode').style.display = 'inherit'; + } else { + document.getElementById('hook_mode').style.display = 'inherit'; + document.getElementById('commit_mode').style.display = 'none'; + } }); -var option = ()=>{ - return $("#type").val(); -} +var option = () => { + return $('#type').val(); +}; -var repository_name = ()=>{ - return $("#name").val().trim(); -} +var repository_name = () => { + return $('#name').val().trim(); +}; +var create_repo = (token, name) => { + const AUTHENTICATION_URL = 'https://api.github.com/user/repos'; + let data = { + name, + private: true, + auto_init: true, + description: + 'Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub](https://github.com/QasimWani/LeetHub).', + }; + data = JSON.stringify(data); -var create_repo = (token, name)=>{ - const AUTHENTICATION_URL = "https://api.github.com/user/repos"; - var data = { - 'name': name, - 'private': true, - 'auto_init': true, - 'description': "Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub](https://github.com/QasimWani/LeetHub)." + const xhr = new XMLHttpRequest(); + xhr.addEventListener('readystatechange', function (event) { + if (xhr.readyState == 4) { + status_code(JSON.parse(xhr.responseText), xhr.status, name); } - data = JSON.stringify(data); - - var xhr = new XMLHttpRequest(); - xhr.addEventListener('readystatechange', function(event) { - if(xhr.readyState == 4) { - status_code(JSON.parse(xhr.responseText), xhr.status, name); - } - }); + }); - xhr.open('POST', AUTHENTICATION_URL, true); - xhr.setRequestHeader("Authorization", `token ${token}`); - xhr.setRequestHeader("Accept", "application/vnd.github.v3+json"); - xhr.send(data); -} + xhr.open('POST', AUTHENTICATION_URL, true); + xhr.setRequestHeader('Authorization', `token ${token}`); + xhr.setRequestHeader('Accept', 'application/vnd.github.v3+json'); + xhr.send(data); +}; /* Method for linking hook with an existing repository @@ -176,160 +165,186 @@ var create_repo = (token, name)=>{ 1. Check if existing repository exists and the user has write access to it. 2. Link Hook to it (chrome Storage). */ -var link_repo = (token, name)=>{ - const AUTHENTICATION_URL = "https://api.github.com/repos/" + name; +var link_repo = (token, name) => { + const AUTHENTICATION_URL = `https://api.github.com/repos/${name}`; - var xhr = new XMLHttpRequest(); - xhr.addEventListener('readystatechange', function(event) { - if(xhr.readyState == 4) { - const res = JSON.parse(xhr.responseText); - var bool = link_status_code(xhr.status, name); - if(xhr.status == 200) //BUG FIX - { - if(!bool) //unable to gain access to repo in commit mode. Must switch to hook mode. - { - /* Set mode type to hook */ - chrome.storage.sync.set({"mode_type": "hook"}, data=>{ - console.log(`Error linking ${name} to LeetHub`); - }); - /* Set Repo Hook to NONE */ - chrome.storage.sync.set({"leethub_hook": null}, data=>{ - console.log("Defaulted repo hook to NONE"); - }); + const xhr = new XMLHttpRequest(); + xhr.addEventListener('readystatechange', function (event) { + if (xhr.readyState == 4) { + const res = JSON.parse(xhr.responseText); + const bool = link_status_code(xhr.status, name); + if (xhr.status == 200) { + // BUG FIX + if (!bool) { + // unable to gain access to repo in commit mode. Must switch to hook mode. + /* Set mode type to hook */ + chrome.storage.sync.set({ mode_type: 'hook' }, (data) => { + console.log(`Error linking ${name} to LeetHub`); + }); + /* Set Repo Hook to NONE */ + chrome.storage.sync.set({ leethub_hook: null }, (data) => { + console.log('Defaulted repo hook to NONE'); + }); - /* Hide accordingly */ - document.getElementById("hook_mode").style.display = "inherit"; - document.getElementById("commit_mode").style.display = "none"; + /* Hide accordingly */ + document.getElementById('hook_mode').style.display = + 'inherit'; + document.getElementById('commit_mode').style.display = + 'none'; + } else { + /* Change mode type to commit */ + chrome.storage.sync.set({ mode_type: 'commit' }, (data) => { + $('#error').hide(); + $('#success').html( + `Successfully linked ${name} to LeetHub. Start LeetCoding now!`, + ); + $('#success').show(); + $('#unlink').show(); + }); + /* Set Repo Hook */ + chrome.storage.sync.set( + { leethub_hook: res.full_name }, + (data) => { + console.log('Successfully set new repo hook'); + /* Get problems solved count */ + chrome.storage.sync.get('stats', (psolved) => { + psolved = psolved.stats; + if (psolved && psolved.solved) { + $('#p_solved').text(psolved.solved); } - else - { - /* Change mode type to commit */ - chrome.storage.sync.set({"mode_type": "commit"}, data=>{ - $("#error").hide(); - $("#success").html(`Successfully linked ${name} to LeetHub. Start LeetCoding now!`); - $("#success").show(); - $("#unlink").show(); - }); - /* Set Repo Hook */ - chrome.storage.sync.set({"leethub_hook": res['full_name']}, data=>{ - console.log("Successfully set new repo hook"); - /* Get problems solved count */ - chrome.storage.sync.get("stats", psolved=>{ - psolved = psolved.stats; - if(psolved && psolved["solved"]) - { - $("#p_solved").text(psolved["solved"]); - } - }); - }); - /* Hide accordingly */ - document.getElementById("hook_mode").style.display = "none"; - document.getElementById("commit_mode").style.display = "inherit"; - } - } + }); + }, + ); + /* Hide accordingly */ + document.getElementById('hook_mode').style.display = 'none'; + document.getElementById('commit_mode').style.display = + 'inherit'; } - }); + } + } + }); - xhr.open('GET', AUTHENTICATION_URL, true); - xhr.setRequestHeader("Authorization", `token ${token}`); - xhr.setRequestHeader("Accept", "application/vnd.github.v3+json"); - xhr.send(); -} + xhr.open('GET', AUTHENTICATION_URL, true); + xhr.setRequestHeader('Authorization', `token ${token}`); + xhr.setRequestHeader('Accept', 'application/vnd.github.v3+json'); + xhr.send(); +}; var unlink_repo = () => { - /* Set mode type to hook */ - chrome.storage.sync.set({ "mode_type": "hook" }, data => { - console.log(`Unlinking repo`); - }); - /* Set Repo Hook to NONE */ - chrome.storage.sync.set({ "leethub_hook": null }, data => { - console.log("Defaulted repo hook to NONE"); - }); + /* Set mode type to hook */ + chrome.storage.sync.set({ mode_type: 'hook' }, (data) => { + console.log(`Unlinking repo`); + }); + /* Set Repo Hook to NONE */ + chrome.storage.sync.set({ leethub_hook: null }, (data) => { + console.log('Defaulted repo hook to NONE'); + }); - /* Hide accordingly */ - document.getElementById("hook_mode").style.display = "inherit"; - document.getElementById("commit_mode").style.display = "none"; -} + /* Hide accordingly */ + document.getElementById('hook_mode').style.display = 'inherit'; + document.getElementById('commit_mode').style.display = 'none'; +}; /* Status codes for linking of repo */ -var link_status_code = (status, name)=>{ - let bool = false; - switch (status) - { - case 301: - $("#success").hide(); - $("#error").html(`Error linking ${name} to LeetHub.
This repository has been moved permenantly. Try creating a new one.`); - $("#error").show(); - break; - - case 403: - $("#success").hide(); - $("#error").html(`Error linking ${name} to LeetHub.
Forbidden action. Please make sure you have the right access to this repository.`); - $("#error").show(); - break; - - case 404: - $("#success").hide(); - $("#error").html(`Error linking ${name} to LeetHub.
Resource not found. Make sure you enter the right repository name.`); - $("#error").show(); - break; - - default: - bool = true; - break; - } - return bool; -} +var link_status_code = (status, name) => { + let bool = false; + switch (status) { + case 301: + $('#success').hide(); + $('#error').html( + `Error linking ${name} to LeetHub.
This repository has been moved permenantly. Try creating a new one.`, + ); + $('#error').show(); + break; + + case 403: + $('#success').hide(); + $('#error').html( + `Error linking ${name} to LeetHub.
Forbidden action. Please make sure you have the right access to this repository.`, + ); + $('#error').show(); + break; + + case 404: + $('#success').hide(); + $('#error').html( + `Error linking ${name} to LeetHub.
Resource not found. Make sure you enter the right repository name.`, + ); + $('#error').show(); + break; + + default: + bool = true; + break; + } + return bool; +}; /* Status codes for creating of repo */ -var status_code = (res, status, name)=>{ - switch (status) { - case 304: - $("#success").hide(); - $("#error").text(`Error creating ${name} - Unable to modify repository. Try again later!`); - $("#error").show(); - break; +var status_code = (res, status, name) => { + switch (status) { + case 304: + $('#success').hide(); + $('#error').text( + `Error creating ${name} - Unable to modify repository. Try again later!`, + ); + $('#error').show(); + break; - case 400: - $("#success").hide(); - $("#error").text(`Error creating ${name} - Bad POST request, make sure you're not overriding any existing scripts`); - $("#error").show(); - break; + case 400: + $('#success').hide(); + $('#error').text( + `Error creating ${name} - Bad POST request, make sure you're not overriding any existing scripts`, + ); + $('#error').show(); + break; - case 401: - $("#success").hide(); - $("#error").text(`Error creating ${name} - Unauthorized access to repo. Try again later!`); - $("#error").show(); - break; + case 401: + $('#success').hide(); + $('#error').text( + `Error creating ${name} - Unauthorized access to repo. Try again later!`, + ); + $('#error').show(); + break; - case 403: - $("#success").hide(); - $("#error").text(`Error creating ${name} - Forbidden access to repository. Try again later!`); - $("#error").show(); - break; + case 403: + $('#success').hide(); + $('#error').text( + `Error creating ${name} - Forbidden access to repository. Try again later!`, + ); + $('#error').show(); + break; - case 422: - $("#success").hide(); - $("#error").text(`Error creating ${name} - Unprocessable Entity. Repository may have already been created. Try Linking instead (select 2nd option).`); - $("#error").show(); - break; + case 422: + $('#success').hide(); + $('#error').text( + `Error creating ${name} - Unprocessable Entity. Repository may have already been created. Try Linking instead (select 2nd option).`, + ); + $('#error').show(); + break; - default: - /* Change mode type to commit */ - chrome.storage.sync.set({"mode_type": "commit"}, data=>{ - $("#error").hide(); - $("#success").html(`Successfully created ${name}. Start LeetCoding!`); - $("#success").show(); - $("#unlink").show(); - /* Show new layout */ - document.getElementById("hook_mode").style.display = "none"; - document.getElementById("commit_mode").style.display = "inherit"; - }); - /* Set Repo Hook */ - chrome.storage.sync.set({"leethub_hook": res['full_name']}, data=>{ - console.log("Successfully set new repo hook"); - }); + default: + /* Change mode type to commit */ + chrome.storage.sync.set({ mode_type: 'commit' }, (data) => { + $('#error').hide(); + $('#success').html( + `Successfully created ${name}. Start LeetCoding!`, + ); + $('#success').show(); + $('#unlink').show(); + /* Show new layout */ + document.getElementById('hook_mode').style.display = 'none'; + document.getElementById('commit_mode').style.display = + 'inherit'; + }); + /* Set Repo Hook */ + chrome.storage.sync.set( + { leethub_hook: res.full_name }, + (data) => { + console.log('Successfully set new repo hook'); + }, + ); - break; - } -} \ No newline at end of file + break; + } +}; diff --git a/welcome.html b/welcome.html index 0ec3fd8a..59b66db8 100644 --- a/welcome.html +++ b/welcome.html @@ -1,86 +1,133 @@ + + + - - - - - - + + + - - - - -
-
-
-

LeetHub

-

Automatically sync your code from LeetCode to GitHub

-







- - - -
- - -