125 lines
3.1 KiB
JavaScript
125 lines
3.1 KiB
JavaScript
|
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
|
||
|
switch (request.type) {
|
||
|
case 'addMonitor':
|
||
|
handleAddMonitor(request, sendResponse);
|
||
|
break;
|
||
|
case 'removeMonitor':
|
||
|
sendResponse(handleRemoveMonitor(request));
|
||
|
break;
|
||
|
case 'updateErrorStatus':
|
||
|
sendResponse(handleUpdateErrorStatus(request));
|
||
|
break;
|
||
|
case 'receiveBargain':
|
||
|
(new TimerManager()).getTimer('bargain').start();
|
||
|
break;
|
||
|
case 'refuseBargain':
|
||
|
(new TimerManager()).getTimer('bargain').halt();
|
||
|
break;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
chrome.extension.onConnect.addListener(function(port) {
|
||
|
switch (port.name) {
|
||
|
case 'retryMonitor':
|
||
|
port.onMessage.addListener(function(message) {
|
||
|
(new FlightMonitorBox()).get(message.flightId).flight.getData(
|
||
|
config.monitor.retryTimes,
|
||
|
true,
|
||
|
function(response) {
|
||
|
delete (new FlightMonitorBox()).get(message.flightId).error;
|
||
|
(new FlightMonitorBox()).persistenceOne(message.flightId);
|
||
|
port.postMessage({flightId: message.flightId});
|
||
|
handleUpdateErrorStatus();
|
||
|
},
|
||
|
function(error) {
|
||
|
(new FlightMonitorBox()).get(message.flightId).error = error;
|
||
|
(new FlightMonitorBox()).persistenceOne(message.flightId);
|
||
|
port.postMessage({flightId: message.flightId, error: error});
|
||
|
}
|
||
|
);
|
||
|
});
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
var handleAddMonitor = function(request, sendResponse) {
|
||
|
var flight = FlightFactory.getFromId(request.flightId),
|
||
|
monitor = request.monitor,
|
||
|
error;
|
||
|
|
||
|
flight.getData(
|
||
|
true,
|
||
|
true,
|
||
|
function(data) {
|
||
|
monitor.time = Util.date.now();
|
||
|
monitor.lastTime = monitor.time;
|
||
|
|
||
|
(new FlightMonitorBox()).add(
|
||
|
flight,
|
||
|
monitor,
|
||
|
request.lastShowedPrice
|
||
|
).persistence();
|
||
|
|
||
|
Util.updateIcon('icons/icon_16.png');
|
||
|
(new TimerManager()).getTimer('flip').start();
|
||
|
|
||
|
(new TimerManager()).addTimer(getFlightTimer(request.flightId));
|
||
|
(new TimerManager()).getTimer(request.flightId).start();
|
||
|
|
||
|
$.post(
|
||
|
'http://api.liebao.cn/flight-extension/qunar/?c=follow_qunar',
|
||
|
{
|
||
|
from: flight.query.from,
|
||
|
to: flight.query.to,
|
||
|
start: Util.date.format(flight.query.date[0]),
|
||
|
end: Util.date.format(flight.query.date[1])
|
||
|
}
|
||
|
);
|
||
|
sendResponse({
|
||
|
status: 0
|
||
|
});
|
||
|
},
|
||
|
function(error) {
|
||
|
(new FlightMonitorBox()).remove(request.flightId).persistence();
|
||
|
sendResponse({
|
||
|
status: 1,
|
||
|
error: e
|
||
|
});
|
||
|
}
|
||
|
);
|
||
|
};
|
||
|
|
||
|
var handleRemoveMonitor = function(request) {
|
||
|
(new FlightMonitorBox()).remove(request.flightId).persistence();
|
||
|
|
||
|
if ((new FlightMonitorBox()).getCount() <= 0) {
|
||
|
Util.updateIcon('icons/gray.png');
|
||
|
(new TimerManager()).getTimer('flip').halt();
|
||
|
}
|
||
|
|
||
|
(new TimerManager()).deleteTimer(request.flightId);
|
||
|
|
||
|
return handleUpdateErrorStatus(request);
|
||
|
};
|
||
|
|
||
|
var handleUpdateErrorStatus = function(request) {
|
||
|
var hasError = false,
|
||
|
flights = (new FlightMonitorBox()).getAll(),
|
||
|
flightId;
|
||
|
|
||
|
for (flightId in flights) {
|
||
|
if (flights[flightId].error !== undefined) {
|
||
|
hasError = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (hasError === false) {
|
||
|
chrome.browserAction.setBadgeText({text: ''});
|
||
|
} else {
|
||
|
chrome.browserAction.setBadgeText({text: '!'});
|
||
|
}
|
||
|
|
||
|
return {status: hasError ? 1 : 0};
|
||
|
};
|