Skip to content

Commit 5de772b

Browse files
committed
updating path
2 parents 7cd0e37 + 6159d1d commit 5de772b

File tree

5 files changed

+182
-32
lines changed

5 files changed

+182
-32
lines changed

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

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

23+
/* Commit messages */
24+
const readmeMsg = 'Create README - LeetHub';
25+
const discussionMsg = 'Prepend discussion post - LeetHub';
26+
2327
/* Difficulty of most recenty submitted question */
2428
let difficulty = '';
2529

@@ -42,18 +46,16 @@ function findLanguage() {
4246
}
4347

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

4953
/* Define Payload */
5054
let data = {
51-
message: `working commit - Created using LeetHub`,
55+
message: msg,
5256
content: code,
57+
sha,
5358
};
54-
if (sha !== null) {
55-
data.sha = sha; // get sha for files that already exist in the gh file system.
56-
}
5759

5860
data = JSON.stringify(data);
5961

@@ -99,7 +101,57 @@ const upload = (token, hook, code, directory, filename, sha) => {
99101
xhr.send(data);
100102
};
101103

102-
function uploadGit(code, problemName, filename) {
104+
/* Main function for updating code on GitHub Repo */
105+
/* Currently only used for prepending discussion posts to README */
106+
const update = (token, hook, addition, directory, msg, prepend) => {
107+
const URL = `https://api.github.com/repos/${hook}/contents/${directory}/README.md`;
108+
109+
/* Read from existing file on GitHub */
110+
const xhr = new XMLHttpRequest();
111+
xhr.addEventListener('readystatechange', function () {
112+
if (xhr.readyState === 4) {
113+
if (xhr.status === 200 || xhr.status === 201) {
114+
const response = JSON.parse(xhr.responseText);
115+
const existingContent = decodeURIComponent(
116+
escape(atob(response.content)),
117+
);
118+
let newContent = '';
119+
120+
/* Discussion posts prepended at top of README */
121+
/* Future implementations may require appending to bottom of file */
122+
if (prepend) {
123+
newContent = btoa(
124+
unescape(encodeURIComponent(addition + existingContent)),
125+
);
126+
}
127+
128+
/* Write file with new content to GitHub */
129+
upload(
130+
token,
131+
hook,
132+
newContent,
133+
directory,
134+
'README.md',
135+
response.sha,
136+
msg,
137+
);
138+
}
139+
}
140+
});
141+
xhr.open('GET', URL, true);
142+
xhr.setRequestHeader('Authorization', `token ${token}`);
143+
xhr.setRequestHeader('Accept', 'application/vnd.github.v3+json');
144+
xhr.send();
145+
};
146+
147+
function uploadGit(
148+
code,
149+
problemName,
150+
fileName,
151+
msg,
152+
action,
153+
prepend = true,
154+
) {
103155
/* Get necessary payload data */
104156
chrome.storage.sync.get('leethub_token', (t) => {
105157
const token = t.leethub_token;
@@ -114,7 +166,7 @@ function uploadGit(code, problemName, filename) {
114166
/* Get SHA, if it exists */
115167

116168
/* to get unique key */
117-
const filePath = problemName + filename;
169+
const filePath = problemName + fileName;
118170
chrome.storage.sync.get('stats', (s) => {
119171
const { stats } = s;
120172
let sha = null;
@@ -126,8 +178,29 @@ function uploadGit(code, problemName, filename) {
126178
) {
127179
sha = stats.sha[filePath];
128180
}
129-
/* Upload to git. */
130-
upload(token, hook, code, problemName, filename, sha);
181+
182+
if (action === 'upload') {
183+
/* Upload to git. */
184+
upload(
185+
token,
186+
hook,
187+
code,
188+
problemName,
189+
fileName,
190+
sha,
191+
msg,
192+
);
193+
} else if (action === 'update') {
194+
/* Update on git */
195+
update(
196+
token,
197+
hook,
198+
code,
199+
problemName,
200+
msg,
201+
prepend,
202+
);
203+
}
131204
});
132205
}
133206
});
@@ -168,9 +241,9 @@ function parseQuestion() {
168241
const qbody = questionElem[0].innerHTML;
169242

170243
// Problem title.
171-
let qtitle = document.getElementsByClassName('css-v3d350')[0];
244+
let qtitle = document.getElementsByClassName('css-v3d350');
172245
if (checkElem(qtitle)) {
173-
qtitle = qtitle.innerHTML;
246+
qtitle = qtitle[0].innerHTML;
174247
} else {
175248
qtitle = 'unknown-problem';
176249
}
@@ -192,9 +265,64 @@ function parseQuestion() {
192265
return markdown;
193266
}
194267

268+
/* Parser function for time/space stats */
269+
function parseStats() {
270+
const probStats = document.getElementsByClassName('data__HC-i');
271+
if (!checkElem(probStats)) {
272+
return null;
273+
}
274+
const time = probStats[0].textContent;
275+
const timePercentile = probStats[1].textContent;
276+
const space = probStats[2].textContent;
277+
const spacePercentile = probStats[3].textContent;
278+
279+
// Format commit message
280+
return `Time: ${time} (${timePercentile}), Space: ${space} (${spacePercentile}) - LeetHub`;
281+
}
282+
283+
document.addEventListener('click', (event) => {
284+
const element = event.target;
285+
const oldPath = window.location.pathname;
286+
287+
/* Act on Post button click */
288+
/* Complex since "New" button shares many of the same properties as "Post button */
289+
if (
290+
element.classList.contains('icon__3Su4') ||
291+
element.parentElement.classList.contains('icon__3Su4') ||
292+
element.parentElement.classList.contains(
293+
'btn-content-container__214G',
294+
) ||
295+
element.parentElement.classList.contains('header-right__2UzF')
296+
) {
297+
setTimeout(function () {
298+
/* Only post if post button was clicked and url changed */
299+
if (
300+
oldPath !== window.location.pathname &&
301+
oldPath ===
302+
window.location.pathname.substring(0, oldPath.length) &&
303+
!Number.isNaN(window.location.pathname.charAt(oldPath.length))
304+
) {
305+
const date = new Date();
306+
const currentDate = `${date.getDate()}/${date.getMonth()}/${date.getFullYear()} at ${date.getHours()}:${date.getMinutes()}`;
307+
const addition = `[Discussion Post (created on ${currentDate})](${window.location}) \n`;
308+
const problemName = window.location.pathname.split('/')[2]; // must be true.
309+
310+
uploadGit(
311+
addition,
312+
problemName,
313+
'README.md',
314+
discussionMsg,
315+
'update',
316+
);
317+
}
318+
}, 1000);
319+
}
320+
});
321+
195322
const loader = setInterval(() => {
196323
let code = null;
197324
let probStatement = null;
325+
let probStats = null;
198326

199327
const successTag = document.getElementsByClassName('success__3Ai7');
200328
if (
@@ -204,26 +332,47 @@ const loader = setInterval(() => {
204332
) {
205333
code = parseCode();
206334
probStatement = parseQuestion();
335+
probStats = parseStats();
207336
}
208-
if (code !== null && probStatement !== null) {
337+
if (code !== null && probStatement !== null && probStats !== null) {
209338
clearTimeout(loader);
210339
const problemName = window.location.pathname.split('/')[2]; // must be true.
211340
const language = findLanguage();
212341
if (language !== null) {
213342
uploadGit(
214-
btoa(unescape(encodeURIComponent(code))),
343+
btoa(unescape(encodeURIComponent(probStatement))),
215344
problemName,
216-
problemName + language,
217-
); // Encode `code` to base64
345+
'README.md',
346+
readmeMsg,
347+
'upload',
348+
);
218349

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);
350+
/* Only create README if not already created */
351+
chrome.storage.sync.get('stats', (s) => {
352+
const { stats } = s;
353+
const filePath = problemName + problemName + language;
354+
let sha = null;
355+
if (
356+
stats !== undefined &&
357+
stats.sha !== undefined &&
358+
stats.sha[filePath] !== undefined
359+
) {
360+
sha = stats.sha[filePath];
361+
}
362+
363+
if (sha === null) {
364+
/* @TODO: Change this setTimeout to Promise */
365+
setTimeout(function () {
366+
uploadGit(
367+
btoa(unescape(encodeURIComponent(code))),
368+
problemName,
369+
problemName + language,
370+
probStats,
371+
'upload',
372+
); // Encode `code` to base64
373+
}, 2000);
374+
}
375+
});
227376
}
228377
}
229-
}, 1000);
378+
}, 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)