TODO: 重构inject.js 利用tabs.executeScript 回调通信. 实现对内容的判断与获取.

This commit is contained in:
eson 2020-11-23 19:41:17 +08:00
parent 5c6dc47d47
commit 0ea415ae78
4 changed files with 30 additions and 19 deletions

View File

@ -8,11 +8,15 @@ function GetTask(sender) {
response.json().then(function (task) { response.json().then(function (task) {
if(task.code == 200) { if(task.code == 200) {
task_manager[sender.tab.id] = task; task_manager[sender.tab.id] = task;
var execCode = `window.location.href = "${task.data.url}";`
if(task.data.content_condition) { 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); Tell(sender, InjectMsgType.FETCH);
} else { } else {
Tell(sender, InjectMsgType.NOTASK); Tell(sender, InjectMsgType.NOTASK);

View File

@ -8,7 +8,6 @@ var ErrorTaskUrl = `${Host}/task/error`;
var WorkerNumber = 10; var WorkerNumber = 10;
var PassContentCondition = undefined; var PassContentCondition = undefined;
// 默认条件所有内容都获取 // 默认条件所有内容都获取
var DefaultContentCondition = function () { var DefaultContentCondition = function () {
return true; return true;

View File

@ -23,15 +23,22 @@ chrome.runtime.onMessage.addListener(function (request) {
}); });
// var href = window.location.href; var href = window.location.href;
// var content = document.documentElement.innerHTML; var content = document.documentElement.innerHTML;
if (href.startsWith(Host)) {
GetTask();
} else {
var condition = PassContentCondition != undefined ? PassContentCondition : DefaultContentCondition; var condition = PassContentCondition != undefined ? PassContentCondition : DefaultContentCondition;
if (condition()) { if (condition()) {
console.log(condition, PassContentCondition);
Tell(BackgroundMsgType.CONTENT, content); Tell(BackgroundMsgType.CONTENT, content);
} else { } else {
console.log(condition, PassContentCondition);
Tell(BackgroundMsgType.NOTWANT, content); Tell(BackgroundMsgType.NOTWANT, content);
} }
}
function Tell(backgroundType, content) { function Tell(backgroundType, content) {
try { try {

View File

@ -74,6 +74,7 @@ func PutTask(c *gin.Context) {
data.Store("url", u) data.Store("url", u)
data.Store("ts", now.UnixNano()) data.Store("ts", now.UnixNano())
data.Store("label", label) data.Store("label", label)
data.Store("content_condition", c.PostForm("content_condition"))
if callback := c.PostForm("callback"); callback != "" { if callback := c.PostForm("callback"); callback != "" {
data.Store("callback", callback) data.Store("callback", callback)