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; var content = document.documentElement.innerHTML;
try { try {
if(waittime === undefined) { if (condition == undefined) {
waittime = 6000 condition = function () {
return true;
};
} }
} catch (error) { } catch (error) {
waittime = 6000
}
if (condition == undefined) {
condition = function () { condition = function () {
return true; return true;
}; };
} }
if (condition()) { if (condition()) {
Tell(BackgroundMsgType.CONTENT, content); Tell(BackgroundMsgType.CONTENT, content);
} else { } else {
setTimeout(function () {
if (condition()) {
Tell(BackgroundMsgType.CONTENT, content);
} else {
Tell(BackgroundMsgType.NOTWANT, content); Tell(BackgroundMsgType.NOTWANT, content);
}
}, waittime)
} }
function Tell(backgroundType, content) { function Tell(backgroundType, content) {

View File

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

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strconv"
"time" "time"
"github.com/bwmarrin/snowflake" "github.com/bwmarrin/snowflake"
@ -85,6 +86,18 @@ func PutTask(c *gin.Context) {
data.Store("label", label) data.Store("label", label)
data.Store("content_condition", c.PostForm("content_condition")) data.Store("content_condition", c.PostForm("content_condition"))
if waitcapture, err := strconv.Atoi(c.PostForm("waitcapture")); err != nil {
data.Store("waitcapture", 1)
} else {
data.Store("waitcapture", waitcapture)
}
if retry, err := strconv.Atoi(c.PostForm("retry")); err != nil {
data.Store("retry", 1)
} else {
data.Store("retry", retry)
}
taskQueue.Push(tid, data) taskQueue.Push(tid, data)
oplog.Write(data) oplog.Write(data)
c.JSON(http.StatusOK, Response{Code: 200, Message: "ok", Data: data}) c.JSON(http.StatusOK, Response{Code: 200, Message: "ok", Data: data})
@ -110,7 +123,6 @@ func ContentTask(c *gin.Context) {
task.Store("status", "ready") task.Store("status", "ready")
readyQueue.Push(tid, task) // 进入回调发送队列.TODO: 内容持久化 readyQueue.Push(tid, task) // 进入回调发送队列.TODO: 内容持久化
c.JSON(200, Response{Code: 200, Data: task}) c.JSON(200, Response{Code: 200, Data: task})
// log.Println("start callback")
if label, ok := task.Load("label"); ok { if label, ok := task.Load("label"); ok {
log.Println(label.(string), tid) log.Println(label.(string), tid)
} }

2
screenlog.0 Normal file
View File

@ -0,0 +1,2 @@
Cannot exec './proxyserver': 权限不够
^C

View File

@ -1,6 +1,8 @@
DISPLAY=:99 DISPLAY=:99
screen -L -dmS google-chrome-web google-chrome --load-extension=../chromeproxy --user-data-dir=/tmp/chromeproxy-userdata --ignore-certificate-errors --disable-dev-shm-usage --mute-audio --safebrowsing-disable-auto-update --disable-gpu --no-sandbox --disable-blink-features=AutomationControlled --disable-infobars --allow-running-insecure-content --disable-features=TranslateUI --test-type --no-report-upload --display=$DISPLAY rm /tmp/chromeproxy-userdata/ -rf
screen -L -dmS google-chrome-web google-chrome --load-extension=../chromeproxy --user-data-dir=/tmp/chromeproxy-userdata --ignore-certificate-errors --disable-dev-shm-usage --mute-audio --safebrowsing-disable-auto-update --disable-gpu --no-sandbox --disable-blink-features=AutomationControlled --disable-infobars --allow-running-insecure-content --disable-features=TranslateUI --test-type --no-report-upload --disable-breakpad --no-first-run --display=$DISPLAY
sleep 2s sleep 2s
echo "启动浏览器..." echo "启动浏览器..."
screen -L -dmS proxyserver ./proxyserver screen -L -dmS proxyserver ./proxyserver