forked from jjulian/shortmail-chrome-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail_check.html
62 lines (56 loc) · 1.83 KB
/
mail_check.html
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
<html>
<script>
// Called when the user clicks on the browser action icon.
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.create({"url": "http://shortmail.com/"});
});
function unreadCount() {
var email = localStorage["email"].toLowerCase();
var password = localStorage["password"];
if (email && password) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.shortmail.com/home/api/accounts.json', false);
xhr.setRequestHeader("Authorization", "Basic " + btoa(email + ":" + password));
xhr.send(null);
if (xhr.status === 200 || xhr.status === 304) {
var accounts = JSON.parse(xhr.responseText);
var account = undefined;
for (var i=0; i < accounts.length; i++) {
if (accounts[i].email_address.toLowerCase() === email) {
account = accounts[i];
break;
}
}
if (account) {
localStorage["previousCount"] = account.unseen_inbox_count;
return parseInt(account.unseen_inbox_count);
} else {
console.log('did not find '+email+' in '+accounts.length+' accounts');
chrome.browserAction.setBadgeText({"text": "?"});
return undefined;
}
} else {
chrome.browserAction.setBadgeText({"text": "x"});
return undefined;
}
}
}
function checkForNewMail() {
var count = unreadCount();
if (count === 0) {
// reset the badge
chrome.browserAction.setBadgeText({"text": ""});
} else if (count > 0) {
chrome.browserAction.setBadgeText({"text": "" + count});
}
}
// On extension load, show the last known count...
var previousCount = parseInt(localStorage["previousCount"]);
if (previousCount) {
chrome.browserAction.setBadgeText({"text": "" + previousCount});
}
// ...and immediately try to update it
checkForNewMail();
setInterval(checkForNewMail, 60 * 1000);
</script>
</html>