feat(config): add retry and waitcapture property

This commit is contained in:
eson
2020-11-25 16:34:15 +08:00
parent 899f554336
commit b14615f339
7 changed files with 54 additions and 28 deletions

View File

@@ -2,30 +2,22 @@ var href = window.location.href;
var content = document.documentElement.innerHTML;
try {
if(waittime === undefined) {
waittime = 6000
if (condition == undefined) {
condition = function () {
return true;
};
}
} catch (error) {
waittime = 6000
}
if (condition == undefined) {
condition = function () {
return true;
};
}
if (condition()) {
Tell(BackgroundMsgType.CONTENT, content);
} else {
setTimeout(function () {
if (condition()) {
Tell(BackgroundMsgType.CONTENT, content);
} else {
Tell(BackgroundMsgType.NOTWANT, content);
}
}, waittime)
Tell(BackgroundMsgType.NOTWANT, content);
}
function Tell(backgroundType, content) {

View File

@@ -16,16 +16,29 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
GetTask(sender);
break;
case BackgroundMsgType.NOTWANT:
task = task_manager[sender.tab.id];
task.data.retry --;
if(task.data.retry >= 0) {
CaptureContent(task);
} else {
ErrorTask(sender, request.content);
}
if(task.data.waitcapture != undefined) {
setTimeout(function(){
CaptureContentCurrentPage(sender.tab.id);
}, task.data.waitcapture);
} else {
task.data.retry --;
if(task.data.retry >= 0) {
setTimeout(function(){
CaptureContent(task);
}, 6000)
} else {
ErrorTask(sender, request.content);
}
}
break;
case BackgroundMsgType.CLOSETAB:
delete task_manager[sender.tab.id]
chrome.tabs.remove(sender.tab.id);
default:
break;
}
@@ -48,6 +61,10 @@ function GetTask(sender) {
})
}
function CaptureContentCurrentPage(tabid) {
chrome.tabs.executeScript(tabid, { runAt: "document_end", file: "background/capture.js" });
}
function CaptureContent(task) {
if (task.code == 200) {
@@ -60,12 +77,12 @@ function CaptureContent(task) {
if (task.data.content_condition) {
condition = `${task.data.content_condition}`
}
if (condition) {
if (condition != undefined) {
chrome.tabs.executeScript(tab.id, { runAt: "document_end", code: condition }, function () {
chrome.tabs.executeScript(tab.id, { runAt: "document_end", file: "background/capture.js" });
CaptureContentCurrentPage(tab.id)
});
} else {
chrome.tabs.executeScript(tab.id, { runAt: "document_end", file: "background/capture.js" });
CaptureContentCurrentPage(tab.id)
}
});
}
@@ -74,7 +91,7 @@ function CaptureContent(task) {
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);

View File

@@ -19,6 +19,7 @@ const BackgroundMsgType = {
CONTENT: 'content',
GETTASK: 'gettask',
TIMEOUT: 'timeout',
CLOSETAB: 'closetab',
ERROR: 'error',
OK: 'ok',
};

View File

@@ -5,7 +5,7 @@ if(href.startsWith(Host)) {
}, 1);
} else {
setTimeout(function(){
close();
chrome.runtime.sendMessage({ type: BackgroundMsgType.CLOSETAB })
}, 15000)
}