Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
199 lines (185 sloc) 8.11 KB
<!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>