forked from QasimWani/LeetHub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
66 lines (61 loc) · 2.3 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var action = false;
$("#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`);
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();
});
}
}
});
xhr.open('GET', AUTHENTICATION_URL, true);
xhr.setRequestHeader("Authorization", `token ${token}`);
xhr.send();
}
});