Skip to content

Commit f355507

Browse files
authored
Merge pull request QasimWani#48 from QasimWani/dev
Unknown problem bug fix
2 parents 3807122 + e293b87 commit f355507

File tree

6 files changed

+72
-26
lines changed

6 files changed

+72
-26
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
/scripts/authorize.js
2-
/scripts/oauth2.js
1+
scripts/authorize.js
2+
scripts/oauth2.js
33
.env

css/popup.css

+1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ body {
3030
font-family: sans-serif;
3131
border-top: solid black 1px;
3232
font-weight: 500;
33+
padding-top: 2%;
3334
font-size: medium;
3435
}

popup.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ <h1 id="title">
6666
</p>
6767
<p class="ui small header">
6868
<span style="color: #5cb85c">Easy:</span>
69-
<span id="p_solved_easy" style="color: #5cb85c">0 </span>
70-
<span style="color: #f0ad4e">Medium:</span>
71-
<span id="p_solved_medium" style="color: #f0ad4e"
69+
<span id="p_solved_easy" style="color: #000000; opacity: 0.75;">0 </span>
70+
<span style="color: #f0ad4e">&ensp; &ensp; Medium:</span>
71+
<span id="p_solved_medium" style="color: #000000; opacity: 0.75;"
7272
>0
7373
</span>
74-
<span style="color: #d9534f">Hard:</span>
75-
<span id="p_solved_hard" style="color: #d9534f">0 </span>
74+
<span style="color: #d9534f">&ensp; &ensp; Hard:</span>
75+
<span id="p_solved_hard" style="color: #000000; opacity: 0.75;">0 </span>
7676
</p>
7777

7878
<div class="ui small header">

popup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ chrome.storage.sync.get('leethub_token', (data) => {
5050
const leethubHook = data3.leethub_hook;
5151
if (leethubHook) {
5252
$('#repo_url').html(
53-
`<a target="blank" style="color: cadetblue !important" href="/${leethubHook}">Linked Repo</a>`,
53+
`<a target="blank" style="color: cadetblue !important; font-size:0.8em;" href="/${leethubHook}">${leethubHook}</a>`,
5454
);
5555
}
5656
},

scripts/leetcode.js

+61-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const languages = {
2020
Oracle: '.sql',
2121
};
2222

23+
/* Default commit message */
24+
const defaultMsg = 'Create README - LeetHub';
25+
2326
/* Difficulty of most recenty submitted question */
2427
let difficulty = '';
2528

@@ -42,13 +45,13 @@ function findLanguage() {
4245
}
4346

4447
/* Main function for uploading code to GitHub repo */
45-
const upload = (token, hook, code, directory, filename, sha) => {
48+
const upload = (token, hook, code, directory, filename, sha, msg) => {
4649
// To validate user, load user object from GitHub.
4750
const URL = `https://api.github.com/repos/${hook}/contents/${directory}/${filename}`;
4851

4952
/* Define Payload */
5053
let data = {
51-
message: `working commit - Created using LeetHub`,
54+
message: msg,
5255
content: code,
5356
};
5457
if (sha !== null) {
@@ -99,7 +102,7 @@ const upload = (token, hook, code, directory, filename, sha) => {
99102
xhr.send(data);
100103
};
101104

102-
function uploadGit(code, problemName, filename) {
105+
function uploadGit(code, problemName, fileName, msg = defaultMsg) {
103106
/* Get necessary payload data */
104107
chrome.storage.sync.get('leethub_token', (t) => {
105108
const token = t.leethub_token;
@@ -114,7 +117,7 @@ function uploadGit(code, problemName, filename) {
114117
/* Get SHA, if it exists */
115118

116119
/* to get unique key */
117-
const filePath = problemName + filename;
120+
const filePath = problemName + fileName;
118121
chrome.storage.sync.get('stats', (s) => {
119122
const { stats } = s;
120123
let sha = null;
@@ -127,7 +130,15 @@ function uploadGit(code, problemName, filename) {
127130
sha = stats.sha[filePath];
128131
}
129132
/* Upload to git. */
130-
upload(token, hook, code, problemName, filename, sha);
133+
upload(
134+
token,
135+
hook,
136+
code,
137+
problemName,
138+
fileName,
139+
sha,
140+
msg,
141+
);
131142
});
132143
}
133144
});
@@ -168,9 +179,9 @@ function parseQuestion() {
168179
const qbody = questionElem[0].innerHTML;
169180

170181
// Problem title.
171-
let qtitle = document.getElementsByClassName('css-v3d350')[0];
182+
let qtitle = document.getElementsByClassName('css-v3d350');
172183
if (checkElem(qtitle)) {
173-
qtitle = qtitle.innerHTML;
184+
qtitle = qtitle[0].innerHTML;
174185
} else {
175186
qtitle = 'unknown-problem';
176187
}
@@ -192,9 +203,25 @@ function parseQuestion() {
192203
return markdown;
193204
}
194205

206+
/* Parser function for time/space stats */
207+
function parseStats() {
208+
const probStats = document.getElementsByClassName('data__HC-i');
209+
if (!checkElem(probStats)) {
210+
return null;
211+
}
212+
const time = probStats[0].textContent;
213+
const timePercentile = probStats[1].textContent;
214+
const space = probStats[2].textContent;
215+
const spacePercentile = probStats[3].textContent;
216+
217+
// Format commit message
218+
return `Time: ${time} (${timePercentile}), Space: ${space} (${spacePercentile}) - LeetHub`;
219+
}
220+
195221
const loader = setInterval(() => {
196222
let code = null;
197223
let probStatement = null;
224+
let probStats = null;
198225

199226
const successTag = document.getElementsByClassName('success__3Ai7');
200227
if (
@@ -204,8 +231,9 @@ const loader = setInterval(() => {
204231
) {
205232
code = parseCode();
206233
probStatement = parseQuestion();
234+
probStats = parseStats();
207235
}
208-
if (code !== null && probStatement !== null) {
236+
if (code !== null && probStatement !== null && probStats !== null) {
209237
clearTimeout(loader);
210238
const problemName = window.location.pathname.split('/')[2]; // must be true.
211239
const language = findLanguage();
@@ -214,16 +242,33 @@ const loader = setInterval(() => {
214242
btoa(unescape(encodeURIComponent(code))),
215243
problemName,
216244
problemName + language,
245+
probStats,
217246
); // Encode `code` to base64
218247

219-
/* @TODO: Change this setTimeout to Promise */
220-
setTimeout(function () {
221-
uploadGit(
222-
btoa(unescape(encodeURIComponent(probStatement))),
223-
problemName,
224-
'README.md',
225-
);
226-
}, 2000);
248+
/* Only create README if not already created */
249+
chrome.storage.sync.get('stats', (s) => {
250+
const { stats } = s;
251+
const filePath = problemName + problemName + language;
252+
let sha = null;
253+
if (
254+
stats !== undefined &&
255+
stats.sha !== undefined &&
256+
stats.sha[filePath] !== undefined
257+
) {
258+
sha = stats.sha[filePath];
259+
}
260+
261+
if (sha === null) {
262+
/* @TODO: Change this setTimeout to Promise */
263+
setTimeout(function () {
264+
uploadGit(
265+
btoa(unescape(encodeURIComponent(probStatement))),
266+
problemName,
267+
'README.md',
268+
);
269+
}, 2000);
270+
}
271+
});
227272
}
228273
}
229274
}, 1000);

welcome.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@
104104
>
105105
<span style="color: #5cb85c">Easy:</span>
106106
<span id="p_solved_easy" style="color: #5cb85c">0 </span>
107-
<span style="color: #f0ad4e">Medium:</span>
107+
<span style="color: #f0ad4e">&ensp; &ensp; Medium:</span>
108108
<span id="p_solved_medium" style="color: #f0ad4e"
109109
>0
110110
</span>
111-
<span style="color: #d9534f">Hard:</span>
111+
<span style="color: #d9534f">&ensp; &ensp; Hard:</span>
112112
<span id="p_solved_hard" style="color: #d9534f">0 </span>
113113
</div>
114114
</div>

0 commit comments

Comments
 (0)