From 9e2d9d5abc9eed02d466944608a35d4e4895016f Mon Sep 17 00:00:00 2001 From: eson Date: Wed, 25 Nov 2020 17:44:52 +0800 Subject: [PATCH] feat(contentjson): add judge the json of content --- chromeproxy/background/capture.js | 36 ++++++++++++++++++------------- chromeproxy/background/worker.js | 12 +++++++---- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/chromeproxy/background/capture.js b/chromeproxy/background/capture.js index f1ec9c8..721074e 100644 --- a/chromeproxy/background/capture.js +++ b/chromeproxy/background/capture.js @@ -1,6 +1,17 @@ -var href = window.location.href; -var content = document.documentElement.innerHTML; - + +function Tell(backgroundType, content) { + try { + if (content == undefined) { + chrome.runtime.sendMessage({ type: backgroundType }); + } else { + chrome.runtime.sendMessage({ type: backgroundType, content: content }); + } + } catch (error) { + console.log(error); + window.location.href = Host; + } +} + try { if (condition == undefined) { condition = function () { @@ -13,6 +24,13 @@ try { }; } +var href = window.location.href; +var content = ""; +if(document.contentType == "application/json") { + content = document.documentElement.innerText; +} else { + content = document.documentElement.innerHTML; +} if (condition()) { Tell(BackgroundMsgType.CONTENT, content); @@ -20,15 +38,3 @@ if (condition()) { Tell(BackgroundMsgType.NOTWANT, content); } -function Tell(backgroundType, content) { - try { - if (content == undefined) { - chrome.runtime.sendMessage({ type: backgroundType }); - } else { - chrome.runtime.sendMessage({ type: backgroundType, content: content }); - } - } catch (error) { - console.log(error); - window.location.href = Host; - } -} \ No newline at end of file diff --git a/chromeproxy/background/worker.js b/chromeproxy/background/worker.js index b911ed0..dc96515 100644 --- a/chromeproxy/background/worker.js +++ b/chromeproxy/background/worker.js @@ -23,7 +23,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { CaptureContentCurrentPage(sender.tab.id); }, task.data.waitcapture); } else { - + task.data.retry --; if(task.data.retry >= 0) { setTimeout(function(){ @@ -48,7 +48,6 @@ function GetTask(sender) { fetch(GetTaskUrl).then(function (response) { if (response.ok) { response.json().then(function (task) { - CaptureContent(task); }) } else { @@ -62,7 +61,9 @@ function GetTask(sender) { } function CaptureContentCurrentPage(tabid) { - chrome.tabs.executeScript(tabid, { runAt: "document_end", file: "background/capture.js" }); + chrome.tabs.executeScript(tabid, { runAt: "document_end", file: "base.js" }, function(){ + chrome.tabs.executeScript(tabid, { runAt: "document_end", file: "background/capture.js" }); + }); } function CaptureContent(task) { @@ -75,8 +76,11 @@ function CaptureContent(task) { chrome.tabs.create({ url: task.data.url }, function (tab) { task_manager[tab.id] = task; if (task.data.content_condition) { - condition = `${task.data.content_condition}` + condition = `${task.data.content_condition}`; + } else { + condition = undefined; } + if (condition != undefined) { chrome.tabs.executeScript(tab.id, { runAt: "document_end", code: condition }, function () { CaptureContentCurrentPage(tab.id)