Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
Prepare 2 minute update
Browse files Browse the repository at this point in the history
cc #8
  • Loading branch information
kurtextrem committed Jun 15, 2014
1 parent 040ecea commit 255adcd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
70 changes: 42 additions & 28 deletions src/js/background.js
Expand Up @@ -19,6 +19,25 @@
'9': 'Woodman',
}

function throttle(fn, scope, delay) {
var last, deferTimer, count
return function () {
var context = scope, now = +new Date, args = arguments;
if (last && now < last + threshhold) {
count++
clearTimeout(deferTimer)
deferTimer = setTimeout(function () {
last = now
fn.apply(context, args)
}, delay * count * 60)
} else {
count = 0
last = now
fn.apply(context, args)
}
}
}

var Alert = function () {
this.addListener()
}
Expand All @@ -27,7 +46,7 @@
url: 'http://ps2alerts.com/API/status',

constructor: Alert,
updateTime: 1,
updateTime: 2,
servers: {},
main: 13,
flare: 0,
Expand Down Expand Up @@ -80,34 +99,29 @@
chrome.storage.local.set({lastUpdate: Date.now()})

qwest.post(this.url, {ref: 'kurtextrem alert monitor'}, { dataType: 'json' }).success(function(data) {
if (!data) {
// same as error API error U
}
if (!data) {
// same as error API error U
}

var server, length = Object.keys(data).length - 1
for (var i = 0; i < length; i++) {
server = data[i]
server.alert = data.Actives[i] || {}
server.id = +(server.ServerID)
server.name = serverData[server.id]
if (server.ServerStatus === 'ONLINE') {
this._updateServer(server)
} else {
this.alert = false
chrome.storage.local.set({alert: false})
server.alert.notified = false
this.sendToPopup(server)
}
var server, length = Object.keys(data).length - 1
for (var i = 0; i < length; i++) {
server = data[i]
server.alert = data.Actives[i] || {}
server.id = +(server.ServerID)
server.name = serverData[server.id]
if (server.ServerStatus === 'ONLINE') {
this._updateServer(server)
} else {
this.alert = false
chrome.storage.local.set({alert: false})
server.alert.notified = false
this.sendToPopup(server)
}
}

chrome.storage.local.set({ servers: this.servers, count: this.count, serverTimestamp: Date.now() })
this.updateIcon()
}.bind(this)).error(function() {
//server.alert.notified = false
//server.status = 'API error'
//this.sendToPopup(server)
//@todo: Not sure what I should do here
}.bind(this))
chrome.storage.local.set({ servers: this.servers, count: this.count, serverTimestamp: Date.now() })
this.updateIcon()
}.bind(this)).error(throttle(this.update(), this, 2))
},

sendToPopup: function (server) {
Expand Down Expand Up @@ -195,7 +209,7 @@
this.clearBadgeAlarm()
if (server.Status === 1) {
var current = Date.now(),
date = new Date(+server.alert.start - current)
date = new Date(+server.alert.start - current)

if (server.alert.type === 'Territory' || server.alert.zone === 'Global') {
date.setUTCHours(date.getUTCHours() + 2)
Expand Down Expand Up @@ -274,7 +288,7 @@
canvas = [canvas]
}
var context = canvas[0].getContext('2d'),
imageObj = new Image()
imageObj = new Image()

imageObj.onload = function () {
context.clearRect(0, 0, 19, 19)
Expand Down
6 changes: 3 additions & 3 deletions src/js/main.js
Expand Up @@ -7,7 +7,7 @@
if (typeof sessionStorage.startup === 'undefined') {
sessionStorage.startup = 1
chrome.runtime.getBackgroundPage(function (w) {
w.alert.init(function() {
w.alert.init(true, function() {
w.alert.updateIcon()
})
})
Expand Down Expand Up @@ -262,13 +262,13 @@
var $e = $('#refresh')
$e.attr('disabled', true)
chrome.runtime.getBackgroundPage(function(w) {
w.alert.init(function() {
w.alert.init(true, function() {
chrome.storage.local.get({lastUpdate: 0, serverTimestamp: 0}, function(data) {
$e.attr('title', new Date(data.lastUpdate) + '(' +new Date(data.serverTimestamp) + ')')
})
window.setTimeout(function() {
$e.removeAttr('disabled')
}, 30000)
}, 120000)
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
@@ -1,7 +1,7 @@
{
"name": "PlanetSide 2 Alerts",
"short_name": "PS2 Alerts",
"version": "0.4.1",
"version": "0.4.2",
"manifest_version": 2,

"description": "Easy overview over PlanetSide2 Alerts in Chrome.",
Expand Down

0 comments on commit 255adcd

Please sign in to comment.