/**
* 注册标签页更新时的事件
 * 这里调用了initialize()事件，把func.js注入当前标签页中 
 */
var objTimer
var djcs=1;//关键字点击次数，0点击，1停止
var valuekey="";//关键字
var xmsx=" ";//小米选择顺序
var sxtz=0;//选择是否停止，0点击，1停止
var ckjb=0;//窗口句柄
setupTimer();
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
	initialize(tabId);
}
);
/**
 * 注册切换标签页时的事件
 * 这里调用了initialize()事件，把func.js注入当前标签页中
 */
chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo){
	initialize(tabId);
}
);

/**
 * 初始化方法 ，注入func.js事件
 * @param {Object} tabId
 */
function initialize(tabId){
	chrome.tabs.executeScript(tabId,{
		file: "func.js", allFrames: true
	}
	);
	chrome.tabs.executeScript(tabId,{
		file: "jquery-2.0.2.js", allFrames: true
	}
	);
}

/**
 * 启动一个chrome.extension.onRequest事件监听器用来处理消息
 */
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse){
	chrome.tabs.executeScript(null,{
		code: "switchLight("+ request +");", allFrames: true
	}
	)
}
);


chrome.extension.onConnect.addListener(function(port){
	console.assert(port.name == "qgks");
port.onMessage.addListener(function(msg){
	if (msg.joke == "start"){
		setupTimer();
		alert("开始抢购");
	}
	if (msg.joke == "stop"){
		if(objTimer){
			window.clearInterval(objTimer);
			djcs=0;
			alert("停止抢购");
		}
	}
	if (msg.joke == 0){
		djcs=0;
	}
	if (msg.joke == 1){
		djcs=1;
	}
	if (msg.joke == 2){
		sxtz=0;
	}
        if (msg.joke == 3){
		sxtz=1;
	}

	if (msg.joke.split("/")[0] == 2){
		valuekey=msg.joke.split("/")[1];
	}

	if (msg.joke.split("/")[0] == 4){
		ckjb=parseInt(msg.joke.split("/")[1]);
              //alert(ckjb);
	}

}
);
}
);



function setupTimer(){
	if ( objTimer || false){
		window.clearInterval(objTimer);
	}
	objTimer = window.setInterval(qg1,1000);
}
function cancelTimer(){
	if(objTimer) window.clearInterval(objTimer);
}
function qg1(){
	//if(ckjb!=0){
		//readtext();
	//}
       //alert("001");
        //writetext();
	chrome.tabs.executeScript(null,{code: "qg(" + djcs+ ",'" + valuekey + "','" + xmsx + "'," + sxtz + "," + ckjb + ");",allFrames: true
	}
	);
}

//var plugin = document.createElement("embed");
//plugin.setAttribute("type", "application/x-npapi-file-io");
//document.documentElement.appendChild(plugin);

//function readtext() {
//    var a = "c:\\temp\\396364.txt";
//    var yzm=plugin.getTextFile(a);
//}  

//function writetext()
//{
 //   var a = "c:\\temp\\396365.txt";
 //   if(plugin.fileExists(a))
  //  {
  //    plugin.removeFile(a);
  //  }
  //  var yzm=plugin.saveTextFile(a,"abcde");
  //  alert("完成");
//}

window._alert = window.alert;//临时保存  
function fun(){
window.alert = function(str){
	return;
}
}
function fun1(){
	window.alert = window._alert;
}
