-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
95 lines (79 loc) · 3.38 KB
/
Copy pathbackground.js
File metadata and controls
95 lines (79 loc) · 3.38 KB
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
// init
fetchCredentials().then(()=>{update();});
// if user changes tab -> check for current tab url
chrome.tabs.onActiveChanged.addListener(checkTab);
chrome.tabs.onCreated.addListener(checkTab);
chrome.tabs.onUpdated.addListener(checkTab);
var email;
var password;
var isActive = false; // false: show tasks, true: show balance
var refreshTimeoutTask;
const activeRefreshTimeout = 15000; // if unbabel.com is opened, refresh every 15 seconds.
const inactiveRefreshTimeout = 60000; // if unbabel.com is closed, refresh every minute.
function update(){
// reset timeout
clearTimeout(refreshTimeoutTask);
// login method - returns api_key, username and current balance
var body = {"email": email, "password": password}
fetch("https://mobile.unbabel.com/mapi/v1/login", {method: "post", headers: {"Content-Type": "application/json"}, body: JSON.stringify(body)})
.then(res=>{
// only continue if api call is successful.
if(res.status == 201)
res.json().then((json=>{
if(!isActive) // if unbabel.com isn't the active tab, make another request using the api_key to get the current number of available tasks
fetch("https://mobile.unbabel.com/mapi/v1/available_tasks", {
method: "get",
headers: {"Authorization": "ApiKey " + json.username + ":" + json.api_key}}) // auth-header used by api: Authorization: "ApiKey (username):(api key)"
.then(res=>res.json()).then(json=>{
// if you have multiple language pairs, sum up all of the task counts
var totalTasks = 0;
for(var i = 0; i < json.paid.length; i++){
totalTasks += json.paid[i].tasks_available;
}
// set badgetext to total number of tasks
chrome.browserAction.setBadgeText({text: totalTasks.toString()});
})
else // isActive == true -> user is currently on the unbabel website
chrome.browserAction.setBadgeText({text: "$"+json.balance}); // set badgetext to current balance
}))
else // api call unsuccessful
chrome.browserAction.setBadgeText({text: ""});
})
// depending on whether current tab is unbabel or not, update again in x seconds.
const timeout = isActive ? activeRefreshTimeout : inactiveRefreshTimeout;
refreshTimeoutTask = setTimeout(update, timeout);
}
function fetchCredentials(){
return new Promise((res,rej)=>{
chrome.storage.sync.get('email', data=>{
email = data.email;
chrome.storage.sync.get('password', data=>{
password = data.password;
});
});
})
}
// called on event hook, if user changes tab
function checkTab(){
getActiveTabURL().then(url=>{
var newIsActive = url.indexOf("unbabel.com") != -1;
if(newIsActive != isActive)
update();
isActive = newIsActive;
});
}
// returns url of currently active tab
function getActiveTabURL(){
return new Promise((res, rej)=>{
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function(tabs) {
var tab = tabs[0];
if(tab)
res(tab.url);
else
res("");
});
});
}