var tabStatus={},version,firstRun=!1,firstUpgrade=!1,blackList=[/^https?:\/\/[^\/]*\.taobao\.com\/.*/i,/^https?:\/\/[^\/]*\.alipay\.com\/.*/i],MAX_LOG=400;(function(){$.ajax("manifest.json",{success:function(a){a=JSON.parse(a);version=a.version;trackVersion(version)}})})();
function startListener(){chrome.extension.onConnect.addListener(function(a){a.sender.tab?initPort(a):console.error("Not connect from tab")});chrome.extension.onRequest.addListener(function(a,b,c){if(b.tab)if("Configuration"==a.command){var d=setting.getPageConfig(a.href);c(d);a.top&&(resetTabStatus(b.tab.id),a={href:a.href,clsid:"NULL",urldetect:!0},!d.pageRule&&setting.getFirstMatchedRule(a,setting.defaultRules)&&detectControl(a,b.tab.id,0))}else"GetNotification"==a.command?getNotification(a,b,c):
"DismissNotification"==a.command?(chrome.tabs.sendRequest(b.tab.id,{command:"DismissNotificationPage"}),c({})):"BlockSite"==a.command&&(setting.blocked.push({type:"wild",value:a.site}),setting.update(),c({}));else console.error("Request from non-tab")})}var blocked={};function notifyUser(a,b){var c=tabStatus[b];if(c.notify&&(c.urldetect||c.count>c.actived))console.log("Notify the user on tab ",b),chrome.tabs.sendRequest(b,{command:"NotifyUser",tabid:b},null)}
var greenIcon=chrome.extension.getURL("icon16.png"),grayIcon=chrome.extension.getURL("icon16-gray.png"),errorIcon=chrome.extension.getURL("icon16-error.png"),responseCommands={};function resetTabStatus(a){tabStatus[a]={count:0,actived:0,error:0,urldetect:0,notify:!0,issueId:null,logs:{"0":[]},objs:{"0":[]},frames:1,tracking:!1}}
function initPort(a){var b=a.sender.tab.id;b in tabStatus||resetTabStatus(b);var c=tabStatus[b],d=tabStatus[b].frames++;c.logs[d]=[];c.objs[d]=[];a.onMessage.addListener(function(c){var a=responseCommands[c.command];a?(delete c.command,a(c,b,d)):console.error("Unknown command "+c.command)});a.onDisconnect.addListener(function(){for(var a=0;a<c.objs[d].length;++a)countTabObject(c,c.objs[d][a],-1);showTabStatus(b);c.tracking?0==c.count&&(window.open("log.html?tabid="+b),c.tracking=!1):window.setTimeout(function(){c.logs[d]=
[];c.objs[d]=[]},6E4)})}chrome.tabs.onRemoved.addListener(function(a){tabStatus[a]&&(tabStatus[a].removed=!0,window.setTimeout(function(){delete tabStatus[a]},3E5))});
function countTabObject(a,b,c){for(var d=0;d<blackList.length;++d)if(b.href.match(blackList[d]))return;setting.getFirstMatchedRule(b,setting.blocked)&&(a.notify=!1);if(b.urldetect)a.urldetect+=c;else if(b.actived?(a.actived+=c,0<c&&trackUse(b.rule)):0<c&&trackNotUse(b.href),a.count+=c,b=setting.getMatchedIssue(b))a.error+=c,a.issueId=b.identifier,0<c&&trackIssue(b)}
function showTabStatus(a){if(!tabStatus[a].removed){var b=tabStatus[a],c="",d=greenIcon;!b.count&&!b.urldetect?chrome.pageAction.hide(a):(chrome.pageAction.show(a),b.urldetect?(d=grayIcon,c=$$("status_urldetect")):0!=b.count&&(0!=b.error?(d=errorIcon,c=$$("status_error")):b.count!=b.actived?(d=grayIcon,c=$$("status_disabled")):(d=greenIcon,c=$$("status_ok"))),chrome.pageAction.setIcon({tabId:a,path:d}),chrome.pageAction.setTitle({tabId:a,title:c}),chrome.pageAction.setPopup({tabId:a,popup:"popup.html?tabid="+
a}))}}function detectControl(a,b,c){var d=tabStatus[b];0!=c&&d.objs[0].length&&(countTabObject(d,d.objs[0][0],-1),d.objs[0]=[]);d.objs[c].push(a);countTabObject(d,a,1);showTabStatus(b);notifyUser(a,b)}responseCommands.DetectControl=detectControl;responseCommands.Log=function(a,b,c){b=tabStatus[b].logs[c];b.length<MAX_LOG?b.push(a.message):b.length==MAX_LOG&&b.push("More logs clipped")};
function generateLogFile(a){var b=tabStatus[a];if(!b)return"No log for tab "+a;var c;c="---------- Start of log --------------\n"+("UserAgent: "+navigator.userAgent+"\n");c+="Extension version: "+version+"\n";c+="\n";for(var d=0,e=0;e<b.frames;++e)if(!(0==b.objs[e].length&&0==b.logs[e].length)){d&&(c+="\n\n");++d;c+="------------ Frame "+(e+1)+" ------------------\n";c+="Objects:\n";for(var f=0;f<b.objs[e].length;++f)c+=JSON.stringify(b.objs[e][f])+"\n";c+="\n";c+="Log:\n";for(f=0;f<b.logs[e].length;++f)c+=
b.logs[e][f]+"\n"}return 0==d?"No log for tab "+a:c+"\n---------------- End of log ---------------\n"}function getNotification(a,b,c){var d=b.tab.id;chrome.tabs.get(d,function(a){var b={};b.tabId=a.id;b.site=a.url.replace(/[^:]*:\/\/([^\/]*).*/,"$1");b.sitePattern=a.url.replace(/([^:]*:\/\/[^\/]*).*/,"$1/*");b.message=tabStatus[d].urldetect?$$("status_urldetect"):$$("status_disabled");b.closeMsg=$$("bar_close");b.enableMsg=$$("bar_enable");b.blockMsg=$$("bar_block",b.site);c(b)})};
