82 lines
1.9 KiB
JavaScript
82 lines
1.9 KiB
JavaScript
var getFlightTimer = function(flightId, delay) {
|
|
|
|
if (arguments[1] === undefined) {
|
|
delay = config.monitor.interval;
|
|
}
|
|
|
|
|
|
return new Timer(flightId, delay, config.monitor.interval, function(timer) {
|
|
var info = (new FlightMonitorBox()).get(timer.id),
|
|
flight = info.flight,
|
|
monitor = info.monitor,
|
|
oldData = flight.data,
|
|
newData,
|
|
popup = false;
|
|
|
|
monitor.lastTime = Util.date.now();
|
|
flight.getData(
|
|
true,
|
|
true,
|
|
function(newData) {
|
|
if (monitor.type == 'absolute') {
|
|
if (parseInt(newData.main.price, 10) < parseInt(oldData.main.price, 10)) { //满足条件
|
|
Storage.set('showStage', {
|
|
name: 'ticketNotice',
|
|
data: flight.getId()
|
|
});
|
|
popup = true;
|
|
}
|
|
} else { //relative
|
|
if (parseInt(newData.main.price, 10) != parseInt(oldData.main.price, 10)) {
|
|
Storage.set('showStage', {
|
|
name: 'ticketNotice',
|
|
data: flight.getId()
|
|
});
|
|
popup = true;
|
|
}
|
|
}
|
|
|
|
delete info.error;
|
|
chrome.browserAction.setBadgeText({text: ''});
|
|
},
|
|
function(error) {
|
|
if (error.name === 'QuerySoldOutError') {
|
|
Storage.set('showStage', {
|
|
name: 'ticketAdvice',
|
|
data: {
|
|
errors: [{
|
|
flightId: flight.getId(),
|
|
status: '已售罄',
|
|
}],
|
|
advice: '请您重新设置'
|
|
}
|
|
});
|
|
timer.halt();
|
|
popup = true;
|
|
} else if (error.name === 'QueryDateError') {
|
|
Storage.set('showStage', {
|
|
name: 'ticketAdvice',
|
|
data: {
|
|
errors: [{
|
|
flightId: flight.getId(),
|
|
status: '已过期',
|
|
}],
|
|
advice: '请您重新设置'
|
|
}
|
|
});
|
|
timer.halt();
|
|
popup = true;
|
|
}
|
|
|
|
info.error = error;
|
|
chrome.browserAction.setBadgeText({text: '!'});
|
|
},
|
|
function() {
|
|
(new FlightMonitorBox()).persistenceOne(flight.getId());
|
|
if (popup) {
|
|
Util.popup();
|
|
}
|
|
}
|
|
);
|
|
});
|
|
}; |