Skip to content

Commit 3aad6c7

Browse files
authored
Merge pull request QasimWani#68 from QasimWani/main
Merging LocalStorage bug fix to dev branch
2 parents 13db71a + 0333000 commit 3aad6c7

9 files changed

+76
-36
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.env
22
scripts/authorize.js
33
scripts/oauth2.js
4+
node_modules

manifest.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name" : "LeetHub",
44
"description" : "Automatically integrate your code with LeetCode and GitHub",
55
"homepage_url": "https://github.com/QasimWani/LeetHub",
6-
"version" : "0.1.1",
6+
"version" : "0.1.3",
77
"author" : "Qasim Wani",
88
"browser_action": {
99
"default_icon": "assets/thumbnail.png",
@@ -21,6 +21,7 @@
2121
"permissions": [
2222
"tabs",
2323
"activeTab",
24+
"unlimitedStorage",
2425
"storage"
2526
],
2627
"content_scripts": [

popup.html

+13-3
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,23 @@ <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: #000000; opacity: 0.75;">0 </span>
69+
<span
70+
id="p_solved_easy"
71+
style="color: #000000; opacity: 0.75"
72+
>0
73+
</span>
7074
<span style="color: #f0ad4e">&ensp; &ensp; Medium:</span>
71-
<span id="p_solved_medium" style="color: #000000; opacity: 0.75;"
75+
<span
76+
id="p_solved_medium"
77+
style="color: #000000; opacity: 0.75"
7278
>0
7379
</span>
7480
<span style="color: #d9534f">&ensp; &ensp; Hard:</span>
75-
<span id="p_solved_hard" style="color: #000000; opacity: 0.75;">0 </span>
81+
<span
82+
id="p_solved_hard"
83+
style="color: #000000; opacity: 0.75"
84+
>0
85+
</span>
7686
</p>
7787

7888
<div class="ui small header">

popup.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $('#hook_URL').attr(
1919
`chrome-extension://${chrome.runtime.id}/welcome.html`,
2020
);
2121

22-
chrome.storage.sync.get('leethub_token', (data) => {
22+
chrome.storage.local.get('leethub_token', (data) => {
2323
const token = data.leethub_token;
2424
if (token === null || token === undefined) {
2525
action = true;
@@ -33,11 +33,11 @@ chrome.storage.sync.get('leethub_token', (data) => {
3333
if (xhr.readyState === 4) {
3434
if (xhr.status === 200) {
3535
/* Show MAIN FEATURES */
36-
chrome.storage.sync.get('mode_type', (data2) => {
36+
chrome.storage.local.get('mode_type', (data2) => {
3737
if (data2 && data2.mode_type === 'commit') {
3838
$('#commit_mode').show();
3939
/* Get problem stats and repo link */
40-
chrome.storage.sync.get(
40+
chrome.storage.local.get(
4141
['stats', 'leethub_hook'],
4242
(data3) => {
4343
const { stats } = data3;
@@ -62,7 +62,7 @@ chrome.storage.sync.get('leethub_token', (data) => {
6262
} else if (xhr.status === 401) {
6363
// bad oAuth
6464
// reset token and redirect to authorization process again!
65-
chrome.storage.sync.set({ leethub_token: null }, () => {
65+
chrome.storage.local.set({ leethub_token: null }, () => {
6666
console.log(
6767
'BAD oAuth!!! Redirecting back to oAuth process',
6868
);

scripts/authorize.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const link = window.location.href;
102102

103103
/* Check for open pipe */
104104
if (window.location.host === 'github.com') {
105-
chrome.storage.sync.get('pipe_leethub', (data) => {
105+
chrome.storage.local.get('pipe_leethub', (data) => {
106106
if (data && data.pipe_leethub) {
107107
localAuth.parseAccessCode(link);
108108
}

scripts/background.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ function handleMessage(request) {
55
request.isSuccess === true
66
) {
77
/* Set username */
8-
chrome.storage.sync.set(
8+
chrome.storage.local.set(
99
{ leethub_username: request.username },
1010
() => {
1111
window.localStorage.leethub_username = request.username;
1212
},
1313
);
1414

1515
/* Set token */
16-
chrome.storage.sync.set({ leethub_token: request.token }, () => {
16+
chrome.storage.local.set({ leethub_token: request.token }, () => {
1717
window.localStorage[request.KEY] = request.token;
1818
});
1919

2020
/* Close pipe */
21-
chrome.storage.sync.set({ pipe_leethub: false }, () => {
21+
chrome.storage.local.set({ pipe_leethub: false }, () => {
2222
console.log('Closed pipe.');
2323
});
2424

scripts/leetcode.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const upload = (token, hook, code, directory, filename, sha, msg) => {
6565
if (xhr.status === 200 || xhr.status === 201) {
6666
const updatedSha = JSON.parse(xhr.responseText).content.sha; // get updated SHA.
6767

68-
chrome.storage.sync.get('stats', (data2) => {
68+
chrome.storage.local.get('stats', (data2) => {
6969
let { stats } = data2;
7070
if (stats === null || stats === {} || stats === undefined) {
7171
// create stats object
@@ -86,7 +86,7 @@ const upload = (token, hook, code, directory, filename, sha, msg) => {
8686
stats.hard += difficulty === 'Hard' ? 1 : 0;
8787
}
8888
stats.sha[filePath] = updatedSha; // update sha key.
89-
chrome.storage.sync.set({ stats }, () => {
89+
chrome.storage.local.set({ stats }, () => {
9090
console.log(
9191
`Successfully committed ${filename} to github`,
9292
);
@@ -153,21 +153,21 @@ function uploadGit(
153153
prepend = true,
154154
) {
155155
/* Get necessary payload data */
156-
chrome.storage.sync.get('leethub_token', (t) => {
156+
chrome.storage.local.get('leethub_token', (t) => {
157157
const token = t.leethub_token;
158158
if (token) {
159-
chrome.storage.sync.get('mode_type', (m) => {
159+
chrome.storage.local.get('mode_type', (m) => {
160160
const mode = m.mode_type;
161161
if (mode === 'commit') {
162162
/* Get hook */
163-
chrome.storage.sync.get('leethub_hook', (h) => {
163+
chrome.storage.local.get('leethub_hook', (h) => {
164164
const hook = h.leethub_hook;
165165
if (hook) {
166166
/* Get SHA, if it exists */
167167

168168
/* to get unique key */
169169
const filePath = problemName + fileName;
170-
chrome.storage.sync.get('stats', (s) => {
170+
chrome.storage.local.get('stats', (s) => {
171171
const { stats } = s;
172172
let sha = null;
173173

@@ -406,7 +406,7 @@ const loader = setInterval(() => {
406406
const problemName = window.location.pathname.split('/')[2]; // must be true.
407407
const language = findLanguage();
408408
if (language !== null) {
409-
chrome.storage.sync.get('stats', (s) => {
409+
chrome.storage.local.get('stats', (s) => {
410410
const { stats } = s;
411411
const filePath = problemName + problemName + language;
412412
let sha = null;
@@ -444,3 +444,28 @@ const loader = setInterval(() => {
444444
}
445445
}
446446
}, 1000);
447+
448+
/* Sync to local storage */
449+
chrome.storage.local.get('isSync', (data) => {
450+
keys = [
451+
'leethub_token',
452+
'leethub_username',
453+
'pipe_leethub',
454+
'stats',
455+
'mode_type',
456+
'leethub_hook',
457+
'mode_type',
458+
];
459+
if (!data || !data.isSync) {
460+
keys.forEach((key) => {
461+
chrome.storage.sync.get(key, (data) => {
462+
chrome.storage.local.set({ [key]: data[key] });
463+
});
464+
});
465+
chrome.storage.local.set({ isSync: true }, (data) => {
466+
console.log('LeetHub Synced to local values');
467+
});
468+
} else {
469+
console.log('LeetHub Local storage already synced!');
470+
}
471+
});

scripts/oauth2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const oAuth2 = {
2727
url += this.SCOPES[i];
2828
}
2929

30-
chrome.storage.sync.set({ pipe_leethub: true }, () => {
30+
chrome.storage.local.set({ pipe_leethub: true }, () => {
3131
// opening pipe temporarily
3232

3333
chrome.tabs.create({ url, selected: true }, function () {

scripts/welcome.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const statusCode = (res, status, name) => {
5252

5353
default:
5454
/* Change mode type to commit */
55-
chrome.storage.sync.set({ mode_type: 'commit' }, () => {
55+
chrome.storage.local.set({ mode_type: 'commit' }, () => {
5656
$('#error').hide();
5757
$('#success').html(
5858
`Successfully created <a target="blank" href="${res.html_url}">${name}</a>. Start <a href="http://leetcode.com">LeetCoding</a>!`,
@@ -65,9 +65,12 @@ const statusCode = (res, status, name) => {
6565
'inherit';
6666
});
6767
/* Set Repo Hook */
68-
chrome.storage.sync.set({ leethub_hook: res.full_name }, () => {
69-
console.log('Successfully set new repo hook');
70-
});
68+
chrome.storage.local.set(
69+
{ leethub_hook: res.full_name },
70+
() => {
71+
console.log('Successfully set new repo hook');
72+
},
73+
);
7174

7275
break;
7376
}
@@ -152,11 +155,11 @@ const linkRepo = (token, name) => {
152155
if (!bool) {
153156
// unable to gain access to repo in commit mode. Must switch to hook mode.
154157
/* Set mode type to hook */
155-
chrome.storage.sync.set({ mode_type: 'hook' }, () => {
158+
chrome.storage.local.set({ mode_type: 'hook' }, () => {
156159
console.log(`Error linking ${name} to LeetHub`);
157160
});
158161
/* Set Repo Hook to NONE */
159-
chrome.storage.sync.set({ leethub_hook: null }, () => {
162+
chrome.storage.local.set({ leethub_hook: null }, () => {
160163
console.log('Defaulted repo hook to NONE');
161164
});
162165

@@ -168,7 +171,7 @@ const linkRepo = (token, name) => {
168171
} else {
169172
/* Change mode type to commit */
170173
/* Save repo url to chrome storage */
171-
chrome.storage.sync.set(
174+
chrome.storage.local.set(
172175
{ mode_type: 'commit', repo: res.html_url },
173176
() => {
174177
$('#error').hide();
@@ -180,12 +183,12 @@ const linkRepo = (token, name) => {
180183
},
181184
);
182185
/* Set Repo Hook */
183-
chrome.storage.sync.set(
186+
chrome.storage.local.set(
184187
{ leethub_hook: res.full_name },
185188
() => {
186189
console.log('Successfully set new repo hook');
187190
/* Get problems solved count */
188-
chrome.storage.sync.get('stats', (psolved) => {
191+
chrome.storage.local.get('stats', (psolved) => {
189192
const { stats } = psolved;
190193
if (stats && stats.solved) {
191194
$('#p_solved').text(stats.solved);
@@ -213,11 +216,11 @@ const linkRepo = (token, name) => {
213216

214217
const unlinkRepo = () => {
215218
/* Set mode type to hook */
216-
chrome.storage.sync.set({ mode_type: 'hook' }, () => {
219+
chrome.storage.local.set({ mode_type: 'hook' }, () => {
217220
console.log(`Unlinking repo`);
218221
});
219222
/* Set Repo Hook to NONE */
220-
chrome.storage.sync.set({ leethub_hook: null }, () => {
223+
chrome.storage.local.set({ leethub_hook: null }, () => {
221224
console.log('Defaulted repo hook to NONE');
222225
});
223226

@@ -262,7 +265,7 @@ $('#hook_button').on('click', () => {
262265
- step 3: if (1), POST request to repoName (iff option = create new repo) ; else display error message.
263266
- step 4: if proceed from 3, hide hook_mode and display commit_mode (show stats e.g: files pushed/questions-solved/leaderboard)
264267
*/
265-
chrome.storage.sync.get('leethub_token', (data) => {
268+
chrome.storage.local.get('leethub_token', (data) => {
266269
const token = data.leethub_token;
267270
if (token === null || token === undefined) {
268271
/* Not authorized yet. */
@@ -274,7 +277,7 @@ $('#hook_button').on('click', () => {
274277
} else if (option() === 'new') {
275278
createRepo(token, repositoryName());
276279
} else {
277-
chrome.storage.sync.get('leethub_username', (data2) => {
280+
chrome.storage.local.get('leethub_username', (data2) => {
278281
const username = data2.leethub_username;
279282
if (!username) {
280283
/* Improper authorization. */
@@ -301,12 +304,12 @@ $('#unlink a').on('click', () => {
301304
});
302305

303306
/* Detect mode type */
304-
chrome.storage.sync.get('mode_type', (data) => {
307+
chrome.storage.local.get('mode_type', (data) => {
305308
const mode = data.mode_type;
306309

307310
if (mode && mode === 'commit') {
308311
/* Check if still access to repo */
309-
chrome.storage.sync.get('leethub_token', (data2) => {
312+
chrome.storage.local.get('leethub_token', (data2) => {
310313
const token = data2.leethub_token;
311314
if (token === null || token === undefined) {
312315
/* Not authorized yet. */
@@ -321,7 +324,7 @@ chrome.storage.sync.get('mode_type', (data) => {
321324
document.getElementById('commit_mode').style.display = 'none';
322325
} else {
323326
/* Get access to repo */
324-
chrome.storage.sync.get('leethub_hook', (repoName) => {
327+
chrome.storage.local.get('leethub_hook', (repoName) => {
325328
const hook = repoName.leethub_hook;
326329
if (!hook) {
327330
/* Not authorized yet. */

0 commit comments

Comments
 (0)