Skip to content

Commit

Permalink
Merge pull request QasimWani#68 from QasimWani/main
Browse files Browse the repository at this point in the history
Merging LocalStorage bug fix to dev branch
  • Loading branch information
QasimWani authored Feb 9, 2021
2 parents 13db71a + 0333000 commit 3aad6c7
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
scripts/authorize.js
scripts/oauth2.js
node_modules
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name" : "LeetHub",
"description" : "Automatically integrate your code with LeetCode and GitHub",
"homepage_url": "https://github.com/QasimWani/LeetHub",
"version" : "0.1.1",
"version" : "0.1.3",
"author" : "Qasim Wani",
"browser_action": {
"default_icon": "assets/thumbnail.png",
Expand All @@ -21,6 +21,7 @@
"permissions": [
"tabs",
"activeTab",
"unlimitedStorage",
"storage"
],
"content_scripts": [
Expand Down
16 changes: 13 additions & 3 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,23 @@ <h1 id="title">
</p>
<p class="ui small header">
<span style="color: #5cb85c">Easy:</span>
<span id="p_solved_easy" style="color: #000000; opacity: 0.75;">0 </span>
<span
id="p_solved_easy"
style="color: #000000; opacity: 0.75"
>0
</span>
<span style="color: #f0ad4e">&ensp; &ensp; Medium:</span>
<span id="p_solved_medium" style="color: #000000; opacity: 0.75;"
<span
id="p_solved_medium"
style="color: #000000; opacity: 0.75"
>0
</span>
<span style="color: #d9534f">&ensp; &ensp; Hard:</span>
<span id="p_solved_hard" style="color: #000000; opacity: 0.75;">0 </span>
<span
id="p_solved_hard"
style="color: #000000; opacity: 0.75"
>0
</span>
</p>

<div class="ui small header">
Expand Down
8 changes: 4 additions & 4 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $('#hook_URL').attr(
`chrome-extension://${chrome.runtime.id}/welcome.html`,
);

chrome.storage.sync.get('leethub_token', (data) => {
chrome.storage.local.get('leethub_token', (data) => {
const token = data.leethub_token;
if (token === null || token === undefined) {
action = true;
Expand All @@ -33,11 +33,11 @@ chrome.storage.sync.get('leethub_token', (data) => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
/* Show MAIN FEATURES */
chrome.storage.sync.get('mode_type', (data2) => {
chrome.storage.local.get('mode_type', (data2) => {
if (data2 && data2.mode_type === 'commit') {
$('#commit_mode').show();
/* Get problem stats and repo link */
chrome.storage.sync.get(
chrome.storage.local.get(
['stats', 'leethub_hook'],
(data3) => {
const { stats } = data3;
Expand All @@ -62,7 +62,7 @@ chrome.storage.sync.get('leethub_token', (data) => {
} else if (xhr.status === 401) {
// bad oAuth
// reset token and redirect to authorization process again!
chrome.storage.sync.set({ leethub_token: null }, () => {
chrome.storage.local.set({ leethub_token: null }, () => {
console.log(
'BAD oAuth!!! Redirecting back to oAuth process',
);
Expand Down
2 changes: 1 addition & 1 deletion scripts/authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const link = window.location.href;

/* Check for open pipe */
if (window.location.host === 'github.com') {
chrome.storage.sync.get('pipe_leethub', (data) => {
chrome.storage.local.get('pipe_leethub', (data) => {
if (data && data.pipe_leethub) {
localAuth.parseAccessCode(link);
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ function handleMessage(request) {
request.isSuccess === true
) {
/* Set username */
chrome.storage.sync.set(
chrome.storage.local.set(
{ leethub_username: request.username },
() => {
window.localStorage.leethub_username = request.username;
},
);

/* Set token */
chrome.storage.sync.set({ leethub_token: request.token }, () => {
chrome.storage.local.set({ leethub_token: request.token }, () => {
window.localStorage[request.KEY] = request.token;
});

/* Close pipe */
chrome.storage.sync.set({ pipe_leethub: false }, () => {
chrome.storage.local.set({ pipe_leethub: false }, () => {
console.log('Closed pipe.');
});

Expand Down
39 changes: 32 additions & 7 deletions scripts/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const upload = (token, hook, code, directory, filename, sha, msg) => {
if (xhr.status === 200 || xhr.status === 201) {
const updatedSha = JSON.parse(xhr.responseText).content.sha; // get updated SHA.

chrome.storage.sync.get('stats', (data2) => {
chrome.storage.local.get('stats', (data2) => {
let { stats } = data2;
if (stats === null || stats === {} || stats === undefined) {
// create stats object
Expand All @@ -86,7 +86,7 @@ const upload = (token, hook, code, directory, filename, sha, msg) => {
stats.hard += difficulty === 'Hard' ? 1 : 0;
}
stats.sha[filePath] = updatedSha; // update sha key.
chrome.storage.sync.set({ stats }, () => {
chrome.storage.local.set({ stats }, () => {
console.log(
`Successfully committed ${filename} to github`,
);
Expand Down Expand Up @@ -153,21 +153,21 @@ function uploadGit(
prepend = true,
) {
/* Get necessary payload data */
chrome.storage.sync.get('leethub_token', (t) => {
chrome.storage.local.get('leethub_token', (t) => {
const token = t.leethub_token;
if (token) {
chrome.storage.sync.get('mode_type', (m) => {
chrome.storage.local.get('mode_type', (m) => {
const mode = m.mode_type;
if (mode === 'commit') {
/* Get hook */
chrome.storage.sync.get('leethub_hook', (h) => {
chrome.storage.local.get('leethub_hook', (h) => {
const hook = h.leethub_hook;
if (hook) {
/* Get SHA, if it exists */

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

Expand Down Expand Up @@ -406,7 +406,7 @@ const loader = setInterval(() => {
const problemName = window.location.pathname.split('/')[2]; // must be true.
const language = findLanguage();
if (language !== null) {
chrome.storage.sync.get('stats', (s) => {
chrome.storage.local.get('stats', (s) => {
const { stats } = s;
const filePath = problemName + problemName + language;
let sha = null;
Expand Down Expand Up @@ -444,3 +444,28 @@ const loader = setInterval(() => {
}
}
}, 1000);

/* Sync to local storage */
chrome.storage.local.get('isSync', (data) => {
keys = [
'leethub_token',
'leethub_username',
'pipe_leethub',
'stats',
'mode_type',
'leethub_hook',
'mode_type',
];
if (!data || !data.isSync) {
keys.forEach((key) => {
chrome.storage.sync.get(key, (data) => {
chrome.storage.local.set({ [key]: data[key] });
});
});
chrome.storage.local.set({ isSync: true }, (data) => {
console.log('LeetHub Synced to local values');
});
} else {
console.log('LeetHub Local storage already synced!');
}
});
2 changes: 1 addition & 1 deletion scripts/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const oAuth2 = {
url += this.SCOPES[i];
}

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

chrome.tabs.create({ url, selected: true }, function () {
Expand Down
35 changes: 19 additions & 16 deletions scripts/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const statusCode = (res, status, name) => {

default:
/* Change mode type to commit */
chrome.storage.sync.set({ mode_type: 'commit' }, () => {
chrome.storage.local.set({ mode_type: 'commit' }, () => {
$('#error').hide();
$('#success').html(
`Successfully created <a target="blank" href="${res.html_url}">${name}</a>. Start <a href="http://leetcode.com">LeetCoding</a>!`,
Expand All @@ -65,9 +65,12 @@ const statusCode = (res, status, name) => {
'inherit';
});
/* Set Repo Hook */
chrome.storage.sync.set({ leethub_hook: res.full_name }, () => {
console.log('Successfully set new repo hook');
});
chrome.storage.local.set(
{ leethub_hook: res.full_name },
() => {
console.log('Successfully set new repo hook');
},
);

break;
}
Expand Down Expand Up @@ -152,11 +155,11 @@ const linkRepo = (token, name) => {
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' }, () => {
chrome.storage.local.set({ mode_type: 'hook' }, () => {
console.log(`Error linking ${name} to LeetHub`);
});
/* Set Repo Hook to NONE */
chrome.storage.sync.set({ leethub_hook: null }, () => {
chrome.storage.local.set({ leethub_hook: null }, () => {
console.log('Defaulted repo hook to NONE');
});

Expand All @@ -168,7 +171,7 @@ const linkRepo = (token, name) => {
} else {
/* Change mode type to commit */
/* Save repo url to chrome storage */
chrome.storage.sync.set(
chrome.storage.local.set(
{ mode_type: 'commit', repo: res.html_url },
() => {
$('#error').hide();
Expand All @@ -180,12 +183,12 @@ const linkRepo = (token, name) => {
},
);
/* Set Repo Hook */
chrome.storage.sync.set(
chrome.storage.local.set(
{ leethub_hook: res.full_name },
() => {
console.log('Successfully set new repo hook');
/* Get problems solved count */
chrome.storage.sync.get('stats', (psolved) => {
chrome.storage.local.get('stats', (psolved) => {
const { stats } = psolved;
if (stats && stats.solved) {
$('#p_solved').text(stats.solved);
Expand Down Expand Up @@ -213,11 +216,11 @@ const linkRepo = (token, name) => {

const unlinkRepo = () => {
/* Set mode type to hook */
chrome.storage.sync.set({ mode_type: 'hook' }, () => {
chrome.storage.local.set({ mode_type: 'hook' }, () => {
console.log(`Unlinking repo`);
});
/* Set Repo Hook to NONE */
chrome.storage.sync.set({ leethub_hook: null }, () => {
chrome.storage.local.set({ leethub_hook: null }, () => {
console.log('Defaulted repo hook to NONE');
});

Expand Down Expand Up @@ -262,7 +265,7 @@ $('#hook_button').on('click', () => {
- step 3: if (1), POST request to repoName (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) => {
chrome.storage.local.get('leethub_token', (data) => {
const token = data.leethub_token;
if (token === null || token === undefined) {
/* Not authorized yet. */
Expand All @@ -274,7 +277,7 @@ $('#hook_button').on('click', () => {
} else if (option() === 'new') {
createRepo(token, repositoryName());
} else {
chrome.storage.sync.get('leethub_username', (data2) => {
chrome.storage.local.get('leethub_username', (data2) => {
const username = data2.leethub_username;
if (!username) {
/* Improper authorization. */
Expand All @@ -301,12 +304,12 @@ $('#unlink a').on('click', () => {
});

/* Detect mode type */
chrome.storage.sync.get('mode_type', (data) => {
chrome.storage.local.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', (data2) => {
chrome.storage.local.get('leethub_token', (data2) => {
const token = data2.leethub_token;
if (token === null || token === undefined) {
/* Not authorized yet. */
Expand All @@ -321,7 +324,7 @@ chrome.storage.sync.get('mode_type', (data) => {
document.getElementById('commit_mode').style.display = 'none';
} else {
/* Get access to repo */
chrome.storage.sync.get('leethub_hook', (repoName) => {
chrome.storage.local.get('leethub_hook', (repoName) => {
const hook = repoName.leethub_hook;
if (!hook) {
/* Not authorized yet. */
Expand Down

0 comments on commit 3aad6c7

Please sign in to comment.