插件代理模式.

This commit is contained in:
eson
2020-11-16 19:04:53 +08:00
parent e4923eaf8d
commit c01e6b4717
7 changed files with 301 additions and 27 deletions

View File

@@ -1,22 +1,70 @@
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"]
);
function GetTask() {
fetch(TaskUrl)
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.type) {
case MsgType.CONTENT:
console.log(request);
case MsgType.TIMEOUT:
// sendResponse({type: "error"})
}
sendResponse({type: MsgType.NEWURL, url: "https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"});
});
// 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']);

10
myblock/base.js Normal file
View File

@@ -0,0 +1,10 @@
const TaskUrl = "http://localhost:8820";
const MsgType = {
CONTENT: 'content',
NEWURL: 'newurl',
WAIT: 'wait',
TIMEOUT: 'timeout',
ERROR: 'error',
}

View File

@@ -1 +1,57 @@
function Execute(response) {
window.location.href = response.url;
window.location.hash = response.tid
}
function TimeoutClient(tm) {
setTimeout(function () {
chrome.runtime.sendMessage({ type: MsgType.TIMEOUT }, function (response) {
switch (response.type) {
case MsgType.NEWURL:
Execute(response)
case MsgType.WAIT:
TimeoutClient(1000);
}
});
}, tm);
}
var tm = 6000;
try {
var href = window.location.href;
var tid = window.location.hash;
if (href.startsWith("https://playerduo.com")) {
var content = document.documentElement.innerHTML;
if (
content.startsWith('<head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">')
) {
chrome.runtime.sendMessage(
{
type: MsgType.CONTENT,
tid: tid,
content: content,
},
function (response) {
console.log(response.type, response);
switch (response.type) {
case MsgType.NEWURL:
Execute(response);
case MsgType.WAIT:
tm = 1000;
default:
}
}
);
}
}
} catch (error) {
chrome.runtime.sendMessage({type: MsgType.ERROR, msg: error})
} finally {
TimeoutClient(tm);
}

View File

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