From b14615f339efc363e49d038fd6a648111313d8ec Mon Sep 17 00:00:00 2001 From: eson Date: Wed, 25 Nov 2020 16:34:15 +0800 Subject: [PATCH] feat(config): add retry and waitcapture property --- chromeproxy/background/capture.js | 20 +++++----------- chromeproxy/background/worker.js | 39 ++++++++++++++++++++++--------- chromeproxy/base.js | 1 + chromeproxy/content/inject.js | 2 +- proxyserver/router.go | 14 ++++++++++- screenlog.0 | 2 ++ start.sh | 4 +++- 7 files changed, 54 insertions(+), 28 deletions(-) create mode 100644 screenlog.0 diff --git a/chromeproxy/background/capture.js b/chromeproxy/background/capture.js index 7e1b8c1..f1ec9c8 100644 --- a/chromeproxy/background/capture.js +++ b/chromeproxy/background/capture.js @@ -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) { diff --git a/chromeproxy/background/worker.js b/chromeproxy/background/worker.js index 14b449c..b911ed0 100644 --- a/chromeproxy/background/worker.js +++ b/chromeproxy/background/worker.js @@ -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); diff --git a/chromeproxy/base.js b/chromeproxy/base.js index fa0b23e..d99c818 100644 --- a/chromeproxy/base.js +++ b/chromeproxy/base.js @@ -19,6 +19,7 @@ const BackgroundMsgType = { CONTENT: 'content', GETTASK: 'gettask', TIMEOUT: 'timeout', + CLOSETAB: 'closetab', ERROR: 'error', OK: 'ok', }; diff --git a/chromeproxy/content/inject.js b/chromeproxy/content/inject.js index 936d20f..6666528 100644 --- a/chromeproxy/content/inject.js +++ b/chromeproxy/content/inject.js @@ -5,7 +5,7 @@ if(href.startsWith(Host)) { }, 1); } else { setTimeout(function(){ - close(); + chrome.runtime.sendMessage({ type: BackgroundMsgType.CLOSETAB }) }, 15000) } diff --git a/proxyserver/router.go b/proxyserver/router.go index b722616..8c8c30c 100644 --- a/proxyserver/router.go +++ b/proxyserver/router.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/http" + "strconv" "time" "github.com/bwmarrin/snowflake" @@ -85,6 +86,18 @@ func PutTask(c *gin.Context) { data.Store("label", label) 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) oplog.Write(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") readyQueue.Push(tid, task) // 进入回调发送队列.TODO: 内容持久化 c.JSON(200, Response{Code: 200, Data: task}) - // log.Println("start callback") if label, ok := task.Load("label"); ok { log.Println(label.(string), tid) } diff --git a/screenlog.0 b/screenlog.0 new file mode 100644 index 0000000..df6faa3 --- /dev/null +++ b/screenlog.0 @@ -0,0 +1,2 @@ +Cannot exec './proxyserver': 权限不够 +^C \ No newline at end of file diff --git a/start.sh b/start.sh index 19a56bc..5d96d0d 100755 --- a/start.sh +++ b/start.sh @@ -1,6 +1,8 @@ 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 echo "启动浏览器..." screen -L -dmS proxyserver ./proxyserver