后台获取数据. 获取内容完成. TODO: 注册型客户端.

This commit is contained in:
eson
2020-11-19 15:32:43 +08:00
parent 93b9303ee1
commit 5864a2645f
16 changed files with 131 additions and 73 deletions

16
chromeproxy/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"request": "launch",
"name": "Chrome Extension debugging",
"port": 9222,
"type": "chrome",
"url": "http:/localhost:7123",
"webRoot": "${workspaceFolder}"
}
]
}

View File

@@ -0,0 +1,156 @@
var task_manager = {
}
function GetTask(sender) {
fetch(GetTaskUrl).then(function (response) {
if (response.ok) {
response.json().then(function (task) {
if(task.code == 200) {
task_manager[sender.tab.id] = task;
chrome.tabs.executeScript(sender.tab.id, {runAt: "document_end", code: `window.location.href = "${task.data.url}"` }, function (results) {
})
Tell(sender, InjectMsgType.FETCH);
} else {
Tell(sender, InjectMsgType.NOTASK);
}
})
} else {
console.log("error response:",response.text())
}
}).catch(function (reason) {
console.log(reason);
})
}
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);
fetch(FinishTaskUrl, {method: "POST", body: formdata }).then(function (response) {
if (response.ok) {
response.json().then(function (value) {
if (value.code == 200) {
GetTask(sender);
return
}
});
}
}).catch(function(error){
// TODO: 汇报错误
GetTask(sender);
return;
});
}
function ErrorTask(sender, error) {
var task = task_manager[sender.tab.id];
var formdata = new FormData();
formdata.append("taskid", task.data.taskid);
formdata.append("error", error);
fetch(ErrorTaskUrl, {method: "POST", body: formdata});
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
// console.log(request);
sendResponse({});
// console.log(request, sender); // 利用sender tab id 返回正确的任务id
switch (request.type) {
case BackgroundMsgType.CONTENT:
// 重新获取任务
FinishTask(sender, request.content);
break;
case BackgroundMsgType.ERROR:
// 重新获取任务
ErrorTask(sender, request.error)
GetTask(sender);
break;
case BackgroundMsgType.GETTASK:
GetTask(sender);
break;
case BackgroundMsgType.NOTWANT:
Tell(sender,InjectMsgType.WAIT);
break;
default:
break;
}
});
function Tell(sender, jnjectType) {
try {
chrome.tabs.sendMessage(sender.tab.id , {type: jnjectType})
} catch (error) {
console.log(error);
}
}
// 配置使用的监听
chrome.webRequest.onBeforeRequest.addListener(function (details) {
if (details.url.startsWith("http://eson.config")) {
var params = new URLSearchParams(details.url)
GetTaskUrl = params.get("taskurl");
chrome.tabs.remove(details.tabId, function () { });
return { cancel: true };
}
}, { 'urls': ["<all_urls>"] }, ['blocking']);
// chrome.webRequest.onCompleted.addListener(
// function (details) {
// console.log(details);
// console.log(document);
// // return { responseHeaders: details.responseHeaders};
// },
// {
// urls: ["<all_urls>"],
// types: [
// "main_frame",
// "sub_frame",
// "stylesheet",
// "script",
// "image",
// "object",
// "xmlhttprequest",
// "other"
// ]
// },
// ["responseHeaders", "extraHeaders"]
// );
// fetch('https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser').then(r => r.text()).then(result => {
// // Result now contains the response text, do what you want...
// console.log(result);
// })
// chrome.webRequest.onBeforeRequest.addListener(function(data){
// // data contains request_body
// return {redirectUrl: "https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"};
// },{'urls':["<all_urls>"]},['blocking']);

25
chromeproxy/base.js Normal file
View File

@@ -0,0 +1,25 @@
var Host = "http://localhost:7123"
var GetTaskUrl = `${Host}/task/get`;
var FinishTaskUrl = `${Host}/task/content`;
var ErrorTaskUrl = `${Host}/task/error`;
const TaskIDKey = "chrome_proxy_tid_key";
const BackgroundMsgType = {
NOTWANT: 'notwant',
CONTENT: 'content',
GETTASK: 'gettask',
TIMEOUT: 'timeout',
ERROR: 'error',
OK: 'ok',
};
const InjectMsgType = {
FETCH: 'fetch',
WAIT: 'wait',
ERROR: 'error',
NOTASK: 'notask',
OK: 'ok',
};

View File

@@ -0,0 +1,51 @@
chrome.runtime.onMessage.addListener(function (request) {
switch(request.type) {
case InjectMsgType.WAIT:
setTimeout(function(){
GetTask();
}, 6000);
break;
case InjectMsgType.NOTASK:
setTimeout(function(){
GetTask();
}, 4000);
break;
case InjectMsgType.FETCH:
setTimeout(function(){
GetTask();
}, 6000)
break;
default:
break;
};
});
var href = window.location.href;
var content = document.documentElement.innerHTML;
if (href.startsWith("https://playerduo.com") && content.startsWith('<head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">')) {
Tell(BackgroundMsgType.CONTENT, content);
} else {
Tell(BackgroundMsgType.NOTWANT, content);
}
function Tell(backgroundType, content) {
if(content == undefined) {
chrome.runtime.sendMessage({type: backgroundType});
} else {
chrome.runtime.sendMessage({type: backgroundType, content: content});
}
}
function GetTask() {
Tell(BackgroundMsgType.GETTASK);
}

42
chromeproxy/manifest.json Normal file
View File

@@ -0,0 +1,42 @@
{
"background": {
"scripts": [
"base.js",
"background/worker.js"
]
},
"devtools_page": "devtools.html",
"browser_action": {
"default_icon": {
"16": "static/icons/16.png",
"32": "static/icons/32.png",
"48": "static/icons/48.png",
"64": "static/icons/64.png"
},
"default_title": "ChromeProxy"
},
"content_scripts": [ {
"all_frames": true,
"js": [
"base.js",
"content/inject.js"
],
"matches": [ "<all_urls>" ],
"run_at": "document_end"
} ],
"description": "block all",
"homepage_url": "http://474420502.top",
"icons": {
"128": "static/icons/128.png",
"16": "static/icons/16.png",
"32": "static/icons/32.png",
"48": "static/icons/48.png",
"64": "static/icons/64.png"
},
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlurgt0Ykv740tjk1ebeiSX6UVVRRhxVvh+FvRMTe9PKzkAKUDEW2IjNUz1swxZA8ILnMvtyamAfiErTOxUFB8+zCADU/CK2YEhqUJo3tmHCg6EP2XJL220ZXjmIeft1AqJV1BmGRLhm8VnH8dJ2EThfcflx4JEdlh0/aLJr6UVjF2hPVX8JLAMTVjEfiC82KDNGgXDADqBm3E/6n+Dx+3KhgIYTXKIMk+qRVaOhjAJLh8a9OrDBwpylP5RDifTAyVVa9UOyoLNqynzC9oLabWUr1ovWAOiivhYknFCXnl5q971iNSFpmjc+ZW+aK+TRjMnJF84IeA170corVG3KgnwIDAQAB",
"manifest_version": 2,
"name": "ChromeProxy",
"permissions": ["background", "http://*/*", "https://*/*", "storage","tabs" , "activeTab", "webRequest", "webRequestBlocking", "<all_urls>", "webNavigation"],
"short_name": "ChromeProxy",
"version": "0.1.0"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB