2020-11-15 17:47:34 +00:00
|
|
|
|
2020-11-17 11:42:36 +00:00
|
|
|
var task_manager = {
|
|
|
|
}
|
2020-11-16 11:04:53 +00:00
|
|
|
|
2020-11-19 07:32:43 +00:00
|
|
|
function GetTask(sender) {
|
|
|
|
fetch(GetTaskUrl).then(function (response) {
|
2020-11-18 11:49:46 +00:00
|
|
|
if (response.ok) {
|
2020-11-19 07:32:43 +00:00
|
|
|
response.json().then(function (task) {
|
|
|
|
if(task.code == 200) {
|
|
|
|
task_manager[sender.tab.id] = task;
|
2020-11-23 10:50:25 +00:00
|
|
|
var execCode = `window.location.href = "${task.data.url}";`
|
|
|
|
if(task.data.content_condition) {
|
|
|
|
execCode += `PassContentCondition = ${task.data.content_condition};`
|
|
|
|
}
|
|
|
|
chrome.tabs.executeScript(sender.tab.id, {runAt: "document_end", code: execCode})
|
2020-11-18 11:49:46 +00:00
|
|
|
Tell(sender, InjectMsgType.FETCH);
|
|
|
|
} else {
|
|
|
|
Tell(sender, InjectMsgType.NOTASK);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2020-11-19 11:05:32 +00:00
|
|
|
// ErrorTask(sender, "error response:" + response.text())
|
2020-11-18 11:49:46 +00:00
|
|
|
console.log("error response:",response.text())
|
|
|
|
}
|
2020-11-17 11:42:36 +00:00
|
|
|
}).catch(function (reason) {
|
|
|
|
console.log(reason);
|
2020-11-20 07:30:52 +00:00
|
|
|
// GetTask();
|
2020-11-17 11:42:36 +00:00
|
|
|
})
|
2020-11-16 11:04:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 07:32:43 +00:00
|
|
|
function FinishTask(sender, content) {
|
|
|
|
var task = task_manager[sender.tab.id];
|
|
|
|
|
|
|
|
var formdata = new FormData();
|
|
|
|
formdata.append("taskid", task.data.taskid);
|
|
|
|
formdata.append("content", content);
|
2020-11-20 11:13:45 +00:00
|
|
|
// console.log(task.data.taskid);
|
2020-11-19 07:32:43 +00:00
|
|
|
fetch(FinishTaskUrl, {method: "POST", body: formdata }).then(function (response) {
|
|
|
|
if (response.ok) {
|
|
|
|
response.json().then(function (value) {
|
|
|
|
if (value.code == 200) {
|
|
|
|
GetTask(sender);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).catch(function(error){
|
|
|
|
// TODO: 汇报错误
|
2020-11-19 11:05:32 +00:00
|
|
|
// ErrorTask(sender, error);
|
|
|
|
console.log(error)
|
2020-11-20 11:13:45 +00:00
|
|
|
// GetTask(sender);
|
2020-11-19 07:32:43 +00:00
|
|
|
return;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function ErrorTask(sender, error) {
|
|
|
|
var task = task_manager[sender.tab.id];
|
|
|
|
var formdata = new FormData();
|
|
|
|
formdata.append("taskid", task.data.taskid);
|
|
|
|
formdata.append("error", error);
|
|
|
|
fetch(ErrorTaskUrl, {method: "POST", body: formdata});
|
|
|
|
}
|
|
|
|
|
2020-11-17 11:42:36 +00:00
|
|
|
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
|
2020-11-18 11:49:46 +00:00
|
|
|
sendResponse({});
|
2020-11-16 11:04:53 +00:00
|
|
|
switch (request.type) {
|
2020-11-17 11:42:36 +00:00
|
|
|
case BackgroundMsgType.CONTENT:
|
2020-11-18 11:49:46 +00:00
|
|
|
// 重新获取任务
|
2020-11-19 07:32:43 +00:00
|
|
|
FinishTask(sender, request.content);
|
2020-11-18 11:49:46 +00:00
|
|
|
break;
|
2020-11-17 11:42:36 +00:00
|
|
|
case BackgroundMsgType.ERROR:
|
2020-11-18 11:49:46 +00:00
|
|
|
// 重新获取任务
|
2020-11-19 07:32:43 +00:00
|
|
|
ErrorTask(sender, request.error)
|
|
|
|
GetTask(sender);
|
2020-11-18 11:49:46 +00:00
|
|
|
break;
|
2020-11-17 11:42:36 +00:00
|
|
|
case BackgroundMsgType.GETTASK:
|
2020-11-19 07:32:43 +00:00
|
|
|
GetTask(sender);
|
2020-11-18 11:49:46 +00:00
|
|
|
break;
|
|
|
|
case BackgroundMsgType.NOTWANT:
|
|
|
|
Tell(sender,InjectMsgType.WAIT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2020-11-16 11:04:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-18 11:49:46 +00:00
|
|
|
function Tell(sender, jnjectType) {
|
|
|
|
try {
|
|
|
|
chrome.tabs.sendMessage(sender.tab.id , {type: jnjectType})
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
2020-11-16 11:04:53 +00:00
|
|
|
|
2020-11-17 11:42:36 +00:00
|
|
|
// 配置使用的监听
|
|
|
|
chrome.webRequest.onBeforeRequest.addListener(function (details) {
|
|
|
|
if (details.url.startsWith("http://eson.config")) {
|
2020-11-23 10:50:25 +00:00
|
|
|
var params = new URL(details.url).searchParams;
|
2020-11-20 10:24:42 +00:00
|
|
|
UpdateHost(params.get("taskurl"))
|
2020-11-23 10:50:25 +00:00
|
|
|
chrome.tabs.remove(details.tabId);
|
2020-11-20 11:13:45 +00:00
|
|
|
chrome.tabs.query({currentWindow: true}, function(tabs) {
|
2020-11-23 10:50:25 +00:00
|
|
|
var count = 0
|
|
|
|
for(var tab of tabs) {
|
|
|
|
if(!tab.url.startsWith("chrome://") ) {
|
|
|
|
count++;
|
|
|
|
chrome.tabs.executeScript(tab.id, {runAt: "document_end", code: `window.location.href = "${Host}"` })
|
|
|
|
} else {
|
|
|
|
chrome.tabs.remove(tab.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(var i = count; i < WorkerNumber;i++) {
|
|
|
|
chrome.tabs.create({url: `${Host}`});
|
2020-11-20 11:13:45 +00:00
|
|
|
}
|
|
|
|
})
|
2020-11-17 11:42:36 +00:00
|
|
|
return { cancel: true };
|
|
|
|
}
|
2020-11-19 11:05:32 +00:00
|
|
|
}, { 'urls': ["<all_urls>"] }, ['blocking']);
|