chromeworker/chromeproxy/background/worker.js

117 lines
3.3 KiB
JavaScript
Raw Normal View History

var task_manager = {
}
2020-11-16 11:04:53 +00:00
function GetTask(sender) {
fetch(GetTaskUrl).then(function (response) {
2020-11-18 11:49:46 +00:00
if (response.ok) {
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())
}
}).catch(function (reason) {
console.log(reason);
2020-11-20 07:30:52 +00:00
// GetTask();
})
2020-11-16 11:04:53 +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);
// console.log(task.data.taskid);
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)
// GetTask(sender);
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});
}
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) {
case BackgroundMsgType.CONTENT:
2020-11-18 11:49:46 +00:00
// 重新获取任务
FinishTask(sender, request.content);
2020-11-18 11:49:46 +00:00
break;
case BackgroundMsgType.ERROR:
2020-11-18 11:49:46 +00:00
// 重新获取任务
ErrorTask(sender, request.error)
GetTask(sender);
2020-11-18 11:49:46 +00:00
break;
case BackgroundMsgType.GETTASK:
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
// 配置使用的监听
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);
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}`});
}
})
return { cancel: true };
}
2020-11-19 11:05:32 +00:00
}, { 'urls': ["<all_urls>"] }, ['blocking']);