(function() { var counter = {}, ctripHandle = function(from, to, date, referer) { var flight = FlightFactory.getFromCtrip(from, to, date); flight.getData(false, true, function(data) { Storage.set('showStage', { name: 'promotion', data: { query: { from: CityMap.code2name[from], to: CityMap.code2name[to] }, tickets: flight.getData(), referer: referer } }); Storage.set('promotionTime', Util.date.now()); counter = {}; Util.popup(); }); }; chrome.webRequest.onCompleted.addListener(function(details) { //检查上次弹出时间 var lastPromotionTime = Storage.get('promotionTime'); if (lastPromotionTime !== null && lastPromotionTime !== undefined && Util.date.now() - parseInt(lastPromotionTime, 10) < config.promotion.silent) { return ; } var result = details.url.match(/booking\/(.*\-.*)\-day\-1\.html/); if (typeof(result) == 'undefined' || result == null || typeof(result[1]) == 'undefined') { return ; } var flightFlag = result[1]; if (typeof(counter[flightFlag]) == 'undefined') { counter[flightFlag] = 0; } counter[flightFlag]++; if (counter[flightFlag] >= config.promotion.limit) { //解析出发、目的地,日期 var flightInfo = flightFlag.split('-'), result = details.url.match(/DDate1=([0-9\-]*)/), dateInfo; if (result === undefined || result === null || result[1] === undefined) { chrome.tabs.get(details.tabId, function(tabInfo) { dateInfo = tabInfo.title.match(/^[0-9\-]*/); if (dateInfo !== undefined && dateInfo !== null && dateInfo[0]) { ctripHandle(flightInfo[0], flightInfo[1], Util.date.formatFromYmd(dateInfo[0]), details.url); } }); } else { ctripHandle(flightInfo[0], flightInfo[1], result[1], details.url); } } }, { urls: ['http://flights.ctrip.com/booking/*'] }); }());