diff --git a/chromeproxy/background/worker.js b/chromeproxy/background/worker.js index 3277d77..c398094 100644 --- a/chromeproxy/background/worker.js +++ b/chromeproxy/background/worker.js @@ -8,11 +8,15 @@ function GetTask(sender) { response.json().then(function (task) { if(task.code == 200) { task_manager[sender.tab.id] = task; - var execCode = `window.location.href = "${task.data.url}";` if(task.data.content_condition) { - execCode += `PassContentCondition = ${task.data.content_condition};` + execCode = `PassContentCondition = ${task.data.content_condition};` } - chrome.tabs.executeScript(sender.tab.id, {runAt: "document_end", code: execCode}) + chrome.tabs.executeScript(sender.tab.id, {runAt: "document_end", code: `window.location.href = "${task.data.url}";`}, function (result) { + if(execCode) { + console.log(execCode); + chrome.tabs.executeScript(sender.tab.id, {code: execCode}); + } + }) Tell(sender, InjectMsgType.FETCH); } else { Tell(sender, InjectMsgType.NOTASK); diff --git a/chromeproxy/base.js b/chromeproxy/base.js index 1b7bb04..7999258 100644 --- a/chromeproxy/base.js +++ b/chromeproxy/base.js @@ -8,7 +8,6 @@ var ErrorTaskUrl = `${Host}/task/error`; var WorkerNumber = 10; var PassContentCondition = undefined; - // 默认条件所有内容都获取 var DefaultContentCondition = function () { return true; diff --git a/chromeproxy/content/inject.js b/chromeproxy/content/inject.js index 68fb15a..482ebe9 100644 --- a/chromeproxy/content/inject.js +++ b/chromeproxy/content/inject.js @@ -1,19 +1,19 @@ - + chrome.runtime.onMessage.addListener(function (request) { - switch(request.type) { + switch (request.type) { case InjectMsgType.WAIT: - setTimeout(function(){ + setTimeout(function () { GetTask(); }, 6000); break; case InjectMsgType.NOTASK: - setTimeout(function(){ + setTimeout(function () { GetTask(); }, 4000); break; case InjectMsgType.FETCH: - setTimeout(function(){ + setTimeout(function () { GetTask(); }, 6000) break; @@ -22,23 +22,30 @@ chrome.runtime.onMessage.addListener(function (request) { }; }); - -// var href = window.location.href; -// var content = document.documentElement.innerHTML; -var condition = PassContentCondition != undefined ? PassContentCondition : DefaultContentCondition; -if (condition()) { - Tell(BackgroundMsgType.CONTENT, content); +var href = window.location.href; +var content = document.documentElement.innerHTML; +if (href.startsWith(Host)) { + GetTask(); } else { - Tell(BackgroundMsgType.NOTWANT, content); + var condition = PassContentCondition != undefined ? PassContentCondition : DefaultContentCondition; + if (condition()) { + console.log(condition, PassContentCondition); + Tell(BackgroundMsgType.CONTENT, content); + } else { + console.log(condition, PassContentCondition); + Tell(BackgroundMsgType.NOTWANT, content); + } } + + function Tell(backgroundType, content) { try { - if(content == undefined) { - chrome.runtime.sendMessage({type: backgroundType}); + if (content == undefined) { + chrome.runtime.sendMessage({ type: backgroundType }); } else { - chrome.runtime.sendMessage({type: backgroundType, content: content}); + chrome.runtime.sendMessage({ type: backgroundType, content: content }); } } catch (error) { console.log(error); diff --git a/proxyserver/router.go b/proxyserver/router.go index 2c70a39..3f922d8 100644 --- a/proxyserver/router.go +++ b/proxyserver/router.go @@ -74,6 +74,7 @@ func PutTask(c *gin.Context) { data.Store("url", u) data.Store("ts", now.UnixNano()) data.Store("label", label) + data.Store("content_condition", c.PostForm("content_condition")) if callback := c.PostForm("callback"); callback != "" { data.Store("callback", callback)