refactor(gettask): 1.simple gettask

This commit is contained in:
eson
2020-11-24 15:16:37 +08:00
parent 0ea415ae78
commit 38d369bb58
10 changed files with 173 additions and 155 deletions

View File

@@ -0,0 +1,32 @@
var href = window.location.href;
var content = document.documentElement.innerHTML;
setTimeout(function(){
window.close();
}, 15000)
if (condition == undefined) {
condition = function () {
return true;
};
}
if (condition()) {
Tell(BackgroundMsgType.CONTENT, content);
} else {
Tell(BackgroundMsgType.NOTWANT, content);
}
function Tell(backgroundType, content) {
try {
if (content == undefined) {
chrome.runtime.sendMessage({ type: backgroundType });
} else {
chrome.runtime.sendMessage({ type: backgroundType, content: content });
}
} catch (error) {
console.log(error);
window.location.href = Host;
}
}

View File

@@ -0,0 +1,23 @@
// 配置使用的监听
chrome.webRequest.onBeforeRequest.addListener(function (details) {
if (details.url.startsWith("http://eson.config")) {
var params = new URL(details.url).searchParams;
UpdateHost(params.get("taskurl"))
chrome.tabs.remove(details.tabId);
chrome.tabs.query({ currentWindow: true }, function (tabs) {
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}` });
}
})
return { cancel: true };
}
}, { 'urls': ["<all_urls>"] }, ['blocking']);

View File

@@ -1,58 +1,81 @@
console.log("background");
var task_manager = {
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
sendResponse({});
switch (request.type) {
case BackgroundMsgType.CONTENT:
FinishTask(sender, request.content);
break;
case BackgroundMsgType.ERROR:
ErrorTask(sender, request.error)
break;
case BackgroundMsgType.GETTASK:
GetTask(sender);
break;
case BackgroundMsgType.NOTWANT:
ErrorTask(sender, request.content);
break;
default:
break;
}
});
function GetTask(sender) {
fetch(GetTaskUrl).then(function (response) {
if (response.ok) {
response.json().then(function (task) {
if(task.code == 200) {
task_manager[sender.tab.id] = task;
if(task.data.content_condition) {
execCode = `PassContentCondition = ${task.data.content_condition};`
}
chrome.tabs.executeScript(sender.tab.id, {runAt: "document_end", code: `window.location.href = "${task.data.url}";`}, function (result) {
if(execCode) {
console.log(execCode);
chrome.tabs.executeScript(sender.tab.id, {code: execCode});
}
})
Tell(sender, InjectMsgType.FETCH);
} else {
Tell(sender, InjectMsgType.NOTASK);
}
CaptureContent(task);
})
} else {
// ErrorTask(sender, "error response:" + response.text())
console.log("error response:",response.text())
console.log("error response:", response.text())
ErrorTask(sender, response.text());
}
}).catch(function (reason) {
console.log(reason);
// GetTask();
ErrorTask(sender, reason);
})
}
function CaptureContent(task) {
if (task.code == 200) {
chrome.tabs.create({ url: task.data.url }, function (tab) {
task_manager[tab.id] = task;
if (task.data.content_condition) {
condition = `${task.data.content_condition}`
}
if (condition) {
chrome.tabs.executeScript(tab.id, { runAt: "document_end", code: condition }, function () {
chrome.tabs.executeScript(tab.id, { runAt: "document_end", file: "background/capture.js" });
});
} else {
chrome.tabs.executeScript(tab.id, { runAt: "document_end", file: "background/capture.js" });
}
});
}
}
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);
// console.log(task.data.taskid);
fetch(FinishTaskUrl, {method: "POST", body: formdata }).then(function (response) {
if (response.ok) {
response.json().then(function (value) {
delete task_manager[sender.tab.id];
chrome.tabs.remove(sender.tab.id);
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: 汇报错误
// ErrorTask(sender, error);
}).catch(function (error) {
ErrorTask(sender, error);
console.log(error)
// GetTask(sender);
return;
});
}
@@ -62,60 +85,23 @@ function ErrorTask(sender, error) {
var formdata = new FormData();
formdata.append("taskid", task.data.taskid);
formdata.append("error", error);
fetch(ErrorTaskUrl, {method: "POST", body: formdata});
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
sendResponse({});
switch (request.type) {
case BackgroundMsgType.CONTENT:
// 重新获取任务
FinishTask(sender, request.content);
break;
case BackgroundMsgType.ERROR:
// 重新获取任务
ErrorTask(sender, request.error)
GetTask(sender);
break;
case BackgroundMsgType.GETTASK:
GetTask(sender);
break;
case BackgroundMsgType.NOTWANT:
Tell(sender,InjectMsgType.WAIT);
break;
default:
break;
}
});
function Tell(sender, jnjectType) {
try {
chrome.tabs.sendMessage(sender.tab.id , {type: jnjectType})
} catch (error) {
console.log(error);
}
}
// 配置使用的监听
chrome.webRequest.onBeforeRequest.addListener(function (details) {
if (details.url.startsWith("http://eson.config")) {
var params = new URL(details.url).searchParams;
UpdateHost(params.get("taskurl"))
chrome.tabs.remove(details.tabId);
chrome.tabs.query({currentWindow: true}, function(tabs) {
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);
fetch(ErrorTaskUrl, { method: "POST", body: formdata }, function (response) {
if (response.ok) {
response.json().then(function (value) {
if (value.code == 200) {
GetTask(sender);
return
}
}
for(var i = count; i < WorkerNumber;i++) {
chrome.tabs.create({url: `${Host}`});
}
})
return { cancel: true };
}
}, { 'urls': ["<all_urls>"] }, ['blocking']);
});
}
}).then().catch(function (error) {
console.log(error)
return;
});
}