(function() { //刷票次数监控 chrome.extension.onRequest.addListener(function (request, sender, sendResponse) { switch (request.ticketEvent) { case 'login': Storage.remove('mid8BusPop'); break; case 'refresh': var times = parseInt(request.times, 10), now = (new Date().getTime()); if (now >= config.adv.mid8range[0] && now < config.adv.mid8range[1] && times >= config.adv.ticketTimes && Storage.get('mid8BusPop') === null) { Storage.set('mid8BusPop', now); Storage.set('showStage', {name: 'advBusTicket'}); Util.popup(); setTimeout(function() { var showStage = Storage.get('showStage'); if (showStage && JSON.parse(showStage).name == 'advBusTicket') { Storage.remove('showStage'); } }, 5000); } break; } }); //标签情况监控 var tabsOf12306 = {}, reg = /http(s)?:\/\/(.*?\.)?12306.cn(\/.*?)?$/i, is12306 = function(tabInfo) { return tabInfo.url && reg.test(tabInfo.url); }; //获取已有的所有视窗信息 chrome.windows.getAll({populate: true},function(windowsInfos) { var i, j, windowId, onClosed = function(tabId) { if (!tabsOf12306[tabId]) { return ; } //离开了12306页面 delete tabsOf12306[tabId]; if (Object.keys(tabsOf12306).length !==0) { return ; } var now = (new Date().getTime()); if (now < config.adv.mid8range[0] || now >= config.adv.mid8range[1]) { return ; } if (now - parseInt(Storage.getWithDefault('mid8PresentPop', 10)) < config.adv.presentInterval) { return ; } Storage.set('mid8PresentPop', now); Storage.set('showStage', {name: 'advPresent'}); Util.popup(); setTimeout(function() { var showStage = Storage.get('showStage'); if (showStage && JSON.parse(showStage).name == 'advPresent') { Storage.remove('showStage'); } }, 5000); }; for (i = windowsInfos.length - 1; i >= 0; i--) { windowId = windowsInfos[i].id; for (j = windowsInfos[i].tabs.length - 1; j >= 0; j--) { if (is12306(windowsInfos[i].tabs[j])) { tabsOf12306[windowsInfos[i].tabs[j].id] = windowId; } } } chrome.tabs.onCreated.addListener(function (tab) { if (is12306(tab)) { tabsOf12306[tab.id] = tab.windowId; } }); chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { if (is12306(tab)) { tabsOf12306[tabId] = tab.windowId; } else { onClosed(tabId); } }); chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) { onClosed(tabId); }); }); }());