58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
|
|
//开始刷票时隐藏
|
|
if (request.ticketEvent == 'refresh' && request.times == 0) {
|
|
chrome.tabs.sendRequest(sender.tab.id, {ticketEvent: 'flightHide'});
|
|
return ;
|
|
}
|
|
|
|
if (request.ticketEvent !== 'refresh' || request.times !== config.for12306.ticketTimes) {
|
|
return ;
|
|
}
|
|
|
|
//满足次数要求
|
|
chrome.tabs.sendRequest(sender.tab.id, {ticketEvent: 'get12306Query'}, function(query) {
|
|
var flightsData = [],
|
|
flight,
|
|
count = (query.date[1] - query.date[0]) / 86400000 + 1;
|
|
|
|
for (var i=query.date[0]; i<=query.date[1]; i+=86400000) {
|
|
flight = FlightFactory.getFromShort({
|
|
shortFrom: query.shortFrom,
|
|
shortTo: query.shortTo,
|
|
date: [i, i]
|
|
});
|
|
|
|
flight.getData(false, true, function(data) {
|
|
flightsData.push(data.main);
|
|
}, function(error) {
|
|
}, function() {
|
|
count--;
|
|
if (count == 0) {
|
|
if (flightsData.length) { //查询到数据时命令显示
|
|
flightsData.sort(function(flight1, flight2) {
|
|
if (flight1.date > flight2.date) {
|
|
return 1;
|
|
} else if (flight1.date < flight2.date) {
|
|
return -1;
|
|
} else{
|
|
return 0;
|
|
}
|
|
});
|
|
|
|
chrome.tabs.sendRequest(sender.tab.id, {
|
|
ticketEvent: 'flightShow',
|
|
version: baseInfo.getInstance().info.version,
|
|
query: {from: flight.query.from, to: flight.query.to},
|
|
data: flightsData,
|
|
context: query.context === undefined ? {} : query.context
|
|
}, function(response) {
|
|
if (response.status) {
|
|
Statistics.trigger(57);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}); |