-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.html
More file actions
199 lines (185 loc) · 8.11 KB
/
background.html
File metadata and controls
199 lines (185 loc) · 8.11 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!doctype html><head><script src="js/jquery-1.5.2.min.js" type="text/javascript"></script><script>
var version, notified = 0,
subscriptions = {
accounts: {}
,timers: {}
,settings: {
url: 'http://bytesized-hosting.com/api/profile/'
,testData: 'testdata.xml'
,interval: 1000*60*5 // ten minutes *60*10
,ajaxParams: {
cache: false
,dataType: 'xml'
,headers: { 'X-Powered-By': 'BySH Chrome Plugin' }
}
}
// this function also loads old accounts into memory
,newAccount: function(apikey,boxes){
// store account data
var account = {
apikey: apikey
,login: "unknown"
,balance: "unknown"
,boxes: (boxes) ? boxes : {} // initialize or load box storage
,poke: function(callback){
$.ajax(subscriptions.settings.url,
{
cache: false
,data: {
api_key: apikey
}
})
.success(function(data, textStatus, xhr){
var res = $(data)
,login = res.find('login').text()
,balance = res.find('balance').text()
,len = res.length;
subscriptions.accounts[apikey].login = login;
subscriptions.accounts[apikey].balance = balance;
res.find('shared_account').each(function(i){
var $this = $(this),
response = {
quota: $this.find('cached_quota').text()
,at: new Date()
,limit: $this.find('soft_quota').text()
,hardLimit: $this.find('hard_quota').text()
,box: $this.find('server_name').text()
,percent: Math.round($this.find('percentage_quota').text())
,liveQuota: $this.find('live_quota').text()
,daysTillDelete: $this.find('days_till_deletion').text()
,paidTill: $this.find('paid_till').text()
,webVNCLink: $this.find('web_vnc_link').text()
,upgradeAvailable: $this.find('upgrade_available').text()
};
// check to see if we're >90% usage. If so, we'll change the icon to red
if(response.percent > 100){
if(!notified){
chrome.browserAction.setIcon({path:"logo_fail.png"});
var notification = webkitNotifications.createNotification(
'logo.png', // icon url - can be relative
response.box, // notification title
'Your ByteSized Box is over quota ('+response.percent+'%, '+response.liveQuota+'), strange things might start happening. Please clean up.' // notification body text
);
notification.show();
notified = 1;
subscriptions.timers.apiOverLimit = setTimeout(function(){ notified = 0; }, 1000*60*60*4); // notify every 4 hours
}
console.warn('Account over quota at '+response.percent+'% usage. API_KEY: '+apikey);
} else if(response.percent >= 90){
chrome.browserAction.setIcon({path:"logo_fail.png"});
console.warn('Account at '+response.percent+'% usage. API_KEY: '+apikey);
} else {
chrome.browserAction.setIcon({path:"logo.png"});
console.log('Account at '+response.percent+'% usage. API_KEY: '+apikey);
}
// we'll push the data and save it to local storage, removing all but the newest 100 responses
if(!(subscriptions.accounts[apikey].boxes[response.box] instanceof Array))
subscriptions.accounts[apikey].boxes[response.box] = []; // hacky way to test :(
if(subscriptions.accounts[apikey].boxes[response.box].push(response) > 100){
// we might save historical data longer in the future
// but for now, just keep the last 100 data points.
// Might change this to while() to grab any extra stragglers
subscriptions.accounts[apikey].boxes[response.box].shift();
}
}); // end shared-account loop
// store news items
subscriptions.accounts[apikey].news = []; // reset array
// we shouldn't need to store or keep track of old news items, should we?
res.find('news_item').each(function(i){
var $this = $(this);
subscriptions.accounts[apikey].news.push({
title: $this.find('title').text()
,body: $this.find('body').text()
});
});
// END - Store data at this point
localStorage[apikey] = JSON.stringify(subscriptions.accounts[apikey]);
if(typeof callback === 'function'){
callback(xhr.status);
}
}).error(function(xhr, status){
console.log(status, xhr.status);
callback(xhr.status);
}); // end ajax
} // end poke
,destroy: function(){
var accts = JSON.parse(localStorage.accounts)
,i = accts.indexOf(apikey);
accts.splice(i, 1);
localStorage.accounts = JSON.stringify(accts);
delete subscriptions.accounts[apikey];
delete localStorage[apikey];
clearInterval(subscriptions.timers[apikey]);
delete subscriptions.timers[apikey];
}
};
// put in account array and save account name in localStorage
subscriptions.accounts[apikey] = account;
var accts = (localStorage.accounts) ? JSON.parse(localStorage.accounts) : [];
if($.inArray(apikey, accts) === -1){ // old or new?
accts.push(apikey);
localStorage.accounts = JSON.stringify(accts);
}
}
};
// message passing
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
if(request.method == 'save' && request.apikey !== undefined){
subscriptions.newAccount(request.apikey);
// enable the timer
subscriptions.timers[request.apikey] = setInterval(function(){
console.log('Poking account: '+request.apikey);
subscriptions.accounts[request.apikey].poke();
}, subscriptions.settings.interval);
subscriptions.accounts[request.apikey].poke(function(status){
if(status == '500'){
sendResponse({status: '500'});
} else if(status == '' || status == 200)
sendResponse({status: 'OK'});
});
} else if(request.method == 'delete' && request.apikey !== undefined){
subscriptions.accounts[request.apikey].destroy();
sendResponse({status: 'OK'});
}
console.log(sender.tab);
console.log(request);
});
$(function(){
// let's see if there are any saved accounts
// or if we're using an old version of the script (just in case, sanity ya know?)
if(localStorage.accounts === undefined || !(JSON.parse(localStorage.accounts) instanceof Array)){
console.log('Please enter account details!');
// nope! let's tell them to enter their account details
window.open('options.html', 'bysh-options');
} else {
// we found some accounts, let's load the old data in to memory
var accounts = JSON.parse(localStorage.accounts);
accounts.forEach(function(apikey){ // loop through the accounts
// now that we know the username, we can find the old data in localStorage
if(localStorage[apikey]){ // make sure it's really there
var userData = JSON.parse(localStorage[apikey]);
subscriptions.newAccount(userData.apikey, userData.boxes);
// now store each timer instance in a variable so we can stop it if needed.
subscriptions.timers[apikey] = setInterval(function(){
console.log('Poking account: '+apikey);
subscriptions.accounts[apikey].poke();
}, subscriptions.settings.interval);
} else {
// something happened to their data, wipe accounts and start over :(
delete localStorage.accounts;
delete localStorage[apikey];
clearInterval(subscriptions.timers[apikey]);
delete subscriptions.timers[apikey];
delete subscriptions.accounts[apikey];
console.warn('Account data does not exist for apikey '+apikey);
}
});
}
});
(function(){
$.get('manifest.json', function(data){
data = JSON.parse(data);
version = data.version;
});
})
</script>