-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpopup.js
79 lines (73 loc) · 3.06 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
67
68
69
70
71
72
73
74
75
76
77
78
79
let begin = false;
chrome.storage.local.get(['darkmodeFlag'], function (darkmode_flag) {
console.log(darkmode_flag.darkmodeFlag);
if (darkmode_flag.darkmodeFlag === 1) {
window.onload = function() {
$('#darkmode-toggle').click();
}
}
});
$('#authentication_button').on('click', () => {
if (begin) {
startGitHubOAuthProcess.githubOAuth();
}
});
$('#index_URL').attr(
'href', chrome.runtime.getURL('index.html')
);
$('#link_repo_redirect').attr(
'href', chrome.runtime.getURL('index.html')
);
chrome.storage.local.get('githubAccessToken', (responseToken) => {
const accessToken = responseToken.githubAccessToken;
if (accessToken === null || accessToken === undefined) {
begin = true;
$('#authentication_phase').attr('style', 'display:inherit;');
}
else {
const gitHubAPI_authURL = 'https://api.github.com/user';
const xhttp = new XMLHttpRequest();
xhttp.addEventListener('readystatechange', function () {
if (xhttp.readyState === 4) {
if (xhttp.status === 200) {
chrome.storage.local.get('current_phase', (phase) => {
if (phase && phase.current_phase === 'solve_and_push') {
$('#solve_and_push_phase').attr('style', 'display:inherit;');
chrome.storage.local.get(
['userStatistics', 'github_LinkedRepository'],
(userStats) => {
const { userStatistics } = userStats;
if (userStatistics && userStatistics.solved) {
$('#successful_submissions').text(userStatistics.solved);
$('#successful_submissions_school').text(userStatistics.school);
$('#successful_submissions_basic').text(userStatistics.basic);
$('#successful_submissions_easy').text(userStatistics.easy);
$('#successful_submissions_medium').text(userStatistics.medium);
$('#successful_submissions_hard').text(userStatistics.hard);
}
const gitHubLinkedRepository = userStats.github_LinkedRepository;
if (gitHubLinkedRepository) {
$('#repository_link').html(`<a target="blank" style="color: #104a8e !important; font-size:0.8em;" href="https://github.com/${gitHubLinkedRepository}">${gitHubLinkedRepository}</a>`,);
}
},
);
}
else {
$('#link_repo_phase').attr('style', 'display:inherit;');
}
});
}
else if (xhttp.status === 401) {
chrome.storage.local.set({ githubAccessToken: null }, () => {
console.log('Something went wrong during authentication. Please try again after some time!',);
begin = true;
$('#authentication_phase').attr('style', 'display:inherit;');
});
}
}
});
xhttp.open('GET', gitHubAPI_authURL, true);
xhttp.setRequestHeader('Authorization', `token ${accessToken}`);
xhttp.send();
}
});