Skip to content

Commit

Permalink
basic model is completed
Browse files Browse the repository at this point in the history
  • Loading branch information
gongjunhao committed Nov 22, 2017
1 parent 39ca9ee commit 868f55b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4,523 deletions.
1 change: 0 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<meta charset="utf8">
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' http://* 'unsafe-inline'; script-src 'self' http://* 'unsafe-inline' 'unsafe-eval'" />
<script type="text/javascript" src="lib/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="lib/dexie.js"></script>
<script type="text/javascript" src="js/utils.js"></script>
<link href="css/index.css" rel="stylesheet" type="text/css">
</head>
Expand Down
19 changes: 17 additions & 2 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,23 @@ function processTask(isServerTime) {
}
if((new Date(tasks[i].killTime) - standerTime) >= 0 && (new Date(tasks[i].killTime) - standerTime) <= 600){
//异步执行点击事件
var opt = { type: "basic", title: "秒杀助手提醒", message: tasks[i].name + "\n秒杀任务完成!", iconUrl: "image/bell.png"};
chrome.notifications.create(dialogId+++"", opt);
var task = tasks[i];
var tabId = null;
chrome.tabs.query({url: task.url}, function(results) {
if (results.length > 0) {
for(var j=0; j<results.length; j++){
if(results[j].active){
tabId = results[j].id;
}
}
if(tabId == null) {
tabId = results[0].id;
}
}
chrome.tabs.executeScript(tabId, { code: "secKill("+task.id+");"});
var opt = { type: "basic", title: "秒杀助手提醒", message: task.name + "\n秒杀任务完成!", iconUrl: "image/bell.png"};
chrome.notifications.create(dialogId+++"", opt);
});
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/js/content.js

This file was deleted.

57 changes: 57 additions & 0 deletions src/js/secKill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* 根据任务ID获取任务,执行点击
* @param taskId
*/
function secKill(taskId) {
console.log("开始秒杀!");
console.log(taskId);
chrome.storage.local.get({"tasks": new Array()}, function(value) {
tasks = value.tasks;
if(tasks != undefined && tasks != null && tasks.length > 0) {
for(var i=0; i<tasks.length; i++) {
if(taskId == tasks[i].id) {
dealTask(tasks[i]);
}
}
}
});
}

/**
* 根据xPath查询节点
* @param STR_XPATH
* @returns {Array}
*/
function getElementsByXPath(STR_XPATH) {
var xresult = document.evaluate(STR_XPATH, document, null, XPathResult.ANY_TYPE, null);
var xnodes = [];
var xres;
while (xres = xresult.iterateNext()) {
xnodes.push(xres);
}
return xnodes;
}

/**
* 处理任务
* @param task
*/
function dealTask(task) {
var count = 0;
var timer = setInterval(function () {
if(task.selector == "jQuery") {
$(task.location).each(function(){
this.click();
});
} else {
$(getElementsByXPath(task.location)).each(function(){
this.click();
});
}
count++;
if(count>task.count) {
clearInterval(timer);
}
}, task.frequency);

}
Loading

0 comments on commit 868f55b

Please sign in to comment.