chromeworker/chromeproxy/background/worker.js

156 lines
3.6 KiB
JavaScript

var task_manager = {
}
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;
chrome.tabs.executeScript(sender.tab.id, {runAt: "document_end", code: `window.location.href = "${task.data.url}"` }, function (results) {
})
Tell(sender, InjectMsgType.FETCH);
} else {
Tell(sender, InjectMsgType.NOTASK);
}
})
} else {
console.log("error response:",response.text())
}
}).catch(function (reason) {
console.log(reason);
})
}
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);
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: 汇报错误
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) {
// console.log(request);
sendResponse({});
// console.log(request, sender); // 利用sender tab id 返回正确的任务id
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 URLSearchParams(details.url)
GetTaskUrl = params.get("taskurl");
chrome.tabs.remove(details.tabId, function () { });
return { cancel: true };
}
}, { 'urls': ["<all_urls>"] }, ['blocking']);
// chrome.webRequest.onCompleted.addListener(
// function (details) {
// console.log(details);
// console.log(document);
// // return { responseHeaders: details.responseHeaders};
// },
// {
// urls: ["<all_urls>"],
// types: [
// "main_frame",
// "sub_frame",
// "stylesheet",
// "script",
// "image",
// "object",
// "xmlhttprequest",
// "other"
// ]
// },
// ["responseHeaders", "extraHeaders"]
// );
// fetch('https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser').then(r => r.text()).then(result => {
// // Result now contains the response text, do what you want...
// console.log(result);
// })
// chrome.webRequest.onBeforeRequest.addListener(function(data){
// // data contains request_body
// return {redirectUrl: "https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"};
// },{'urls':["<all_urls>"]},['blocking']);