-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
128 lines (120 loc) · 3.97 KB
/
common.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var COMMON = (function () {
var STATE = {
isLoggedIn: false,
token: null,
}
var loginUser = function (callback) {
if(!isOnLine()) return callback(true, {
code: 99 // code 99 is for no internet
})
chrome.storage.sync.get(['studentId', 'password'], function (data) {
console.log("data", data);
if (data.studentId && data.password) {
APICALLS.getAuthToken(data.studentId, data.password, function (err, data) {
if (err) {
STATE.isLoggedIn = false;
STATE.token = null;
return callback(err, data);
}
STATE.isLoggedIn = true;
STATE.token = data;
chrome.storage.sync.set({ 'token': STATE.token }, function () {
callback(null, STATE.token);
});
})
} else {
return callback(true, {
code: 1
})
}
})
}
var getUnreadEmails = function(callback) {
if(!isOnLine()) return callback(true, {
code: 99 // code 99 is for no internet
})
getToken(function(err, token) {
if(err) {
return callback(err); // show user login page
}
APICALLS.searchRequest({
token: token
}, function(err, data) {
if(err) return callback(err, null);
chrome.browserAction.setBadgeText({
text: data.m ? data.m.length.toString() : '0'
})
chrome.storage.sync.set({
'unreadMsgs': data.m
}, function() {
return callback(null, data.m);
})
})
})
}
var deleteEmail = function(ids, callback) {
if(!isOnLine()) return callback(true, {
code: 99 // code 99 is for no internet
})
getToken(function(err, token) {
if(err) {
return callback(err);
}
APICALLS.itemAction({
token: token,
ids: ids,
op: 'delete'
}, function(err, done) {
if(err) return callback(err, null);
return callback(null, done);
})
})
}
var getStorageInfo = function(callback) {
if(!isOnLine()) return callback(true, {
code: 99 // code 99 is for no internet
})
getToken(function(err, token) {
if(err) {
return callback(err);
}
chrome.storage.sync.get(['studentId'], function(data) {
if(!data.studentId) return callback(true, null);
APICALLS.accountInfo({
token: token,
userid: data.studentId,
}, function(err, done) {
if(err) return callback(err, null);
return callback(null, done);
})
});
})
}
var getToken = function(callback) {
if(STATE.token) return callback(null, STATE.token);
chrome.storage.sync.get(['token'], function(data) {
if(!data.token) {
loginUser(function(err, data) {
return callback(err, data);
})
} else {
// when browser was reopened, variable will be blank
STATE.token = data.token;
return callback(null, STATE.token);
}
})
}
var getState = function (key) {
return STATE[key];
}
var isOnLine = function() {
return window.navigator.onLine;
}
return {
loginUser: loginUser,
getState: getState,
getUnreadEmails: getUnreadEmails,
deleteEmail: deleteEmail,
getStorageInfo: getStorageInfo,
}
})();