var _extension = {};
function welcome(agr){
	chrome.tabs.getSelected(null,function(thisTab){
		chrome.tabs.create({
			index:thisTab.index,
			url: chrome.extension.getURL("./help.html?"+agr)
		});	
	});
}
function init(){
	//
	for (var k in localStorage){
		ga_push("[localStorage]", k, localStorage[k], 1);
	}
	if (navigator.userAgent.indexOf("ChromePlus") == -1 && navigator.userAgent.indexOf("SunChrome") == -1){
		if(!localStorage["version"]){
			welcome();
			ga_push("[Welcome]", location.hostname, navigator.userAgent, 0);
			localStorage["version"] = "1.0.0";
		}else if (localStorage["version"] < '3.9.0'){
			welcome('update=true');
			ga_push("[Update]", location.hostname, navigator.userAgent, 0);
		}
		
	}
	var ext = new XMLHttpRequest();
	ext.onreadystatechange = function() {
		if (ext.readyState == 4) {
			try{
				if (JSON.parse(ext.responseText)){
					_extension = JSON.parse(ext.responseText);
					localStorage["version"] = _extension.version;
					if (_gaq != null && typeof _gaq != "undefined")
						_gaq.push(['_setCustomVar', 1,'version', _extension.version, 1]);
				}
			}catch(e){}
		}
	}	
	try{
		ext.open("GET", "../manifest.json", true);
		ext.send();
	}catch(e){
		;
	}	
	
	setTimeout(function(){
		ga_push('Version',localStorage["version"]);
	},2000);		
	
	if (!localStorage["autoReplaceEnable"]){//undefined	
			localStorage["autoReplaceEnable"] = "true";
			chrome.browserAction.setIcon({path:"./img/icon-auto.png"});
	}else
		if (localStorage["autoReplaceEnable"] == "true"){
			chrome.browserAction.setIcon({path:"./img/icon-auto.png"});
		}else{
			chrome.browserAction.setIcon({path:"./img/icon-half.png"});
		}
	
	localStorage["download_site"] = [];
	
	ga_push("[PageLoad]", location.hostname, location.pathname, 1);
}

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
	var i = 0;
	switch (request.hope)
	{
	case "checkAutoReplaceEnable":
		sendResponse({"checkAutoReplaceEnable": localStorage["autoReplaceEnable"]});
		break;
	case "getPageSource":
		var timerflag ;
		var xhr = new XMLHttpRequest();
		//console.log("请求:"+ request.url);
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (timerflag == null)
					return;
				try{
					clearTimeout(timerflag);
					timerflag = null;
				}catch(e){}	
				//console.log("返回:"+ request.url);
				sendResponse({"responseText" :xhr.responseText,"innerHTML":request.innerHTML});
			}
		}	
		try{
			xhr.open("GET", request.url, true);
			xhr.send();
		}catch(e){
			sendResponse({"responseText" :"","innerHTML":request.innerHTML});
			console.log("返回异常:"+ request.url);
		}	
		timerflag = setTimeout(function(){
			timerflag = null;sendResponse({"responseText":"","innerHTML":request.innerHTML});
			console.log("请求源代码超时")
		},7000);
		break;
	case "getLocalStorage":
		chrome.tabs.getSelected(null, function(tab) {
			if (localStorage[request.StorageName])
				sendResponse({"localStorage" :localStorage[request.StorageName]});
			else
				sendResponse({"localStorage" :""});
		})
		break;
	case "evillinks":
		chrome.tabs.getSelected(null,function(thisTab){ 
			chrome.tabs.create({
					index:thisTab.index,
					url: chrome.extension.getURL("./links.html")
			},function(newtab){
				//console.log({"hope": "outputthunder","str":request.str,"url":request.url,"title":request.title});
				setTimeout(function(){chrome.tabs.sendRequest(newtab.id, {"hope": "outputthunder","str":request.str,"url":request.url,"title":request.title})},1000);
			});	
		}); 
		break;
	}
  });
