*Major refactor of WebEngine
This commit is contained in:
parent
7b59584c57
commit
a541d89110
|
@ -1,6 +1,7 @@
|
||||||
import {
|
import {
|
||||||
BlockingResponse,
|
BlockingResponse,
|
||||||
OnBeforeRequestDetailsType,
|
OnBeforeRequestDetailsType,
|
||||||
|
OnBeforeSendHeadersDetailsType,
|
||||||
OnHeadersReceivedDetailsType,
|
OnHeadersReceivedDetailsType,
|
||||||
OnRequestDetailsType,
|
OnRequestDetailsType,
|
||||||
} from "../types";
|
} from "../types";
|
||||||
|
@ -27,6 +28,12 @@ export default abstract class BaseProvider {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async handleReqHeaders(
|
||||||
|
details: OnBeforeSendHeadersDetailsType
|
||||||
|
): Promise<BlockingResponse | boolean> {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
async handleProxy(details: OnRequestDetailsType): Promise<any | boolean> {
|
async handleProxy(details: OnRequestDetailsType): Promise<any | boolean> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import BaseProvider from "./contentProviders/baseProvider.js";
|
||||||
import {
|
import {
|
||||||
BlockingResponse,
|
BlockingResponse,
|
||||||
OnBeforeRequestDetailsType,
|
OnBeforeRequestDetailsType,
|
||||||
|
OnBeforeSendHeadersDetailsType,
|
||||||
OnCompletedDetailsType,
|
OnCompletedDetailsType,
|
||||||
OnErrorOccurredDetailsType,
|
OnErrorOccurredDetailsType,
|
||||||
OnHeadersReceivedDetailsType,
|
OnHeadersReceivedDetailsType,
|
||||||
|
@ -12,7 +13,7 @@ import {
|
||||||
|
|
||||||
export default class WebEngine {
|
export default class WebEngine {
|
||||||
private contentProviders: BaseProvider[] = [];
|
private contentProviders: BaseProvider[] = [];
|
||||||
private requests: Map<string, OnBeforeRequestDetailsType> = new Map();
|
private requests: Map<string, BaseProvider> = new Map();
|
||||||
private requestData: Map<string, {}> = new Map();
|
private requestData: Map<string, {}> = new Map();
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -30,6 +31,12 @@ export default class WebEngine {
|
||||||
["blocking"]
|
["blocking"]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
browser.webRequest.onBeforeSendHeaders.addListener(
|
||||||
|
this.reqHeaderHandler.bind(this),
|
||||||
|
{ urls: ["<all_urls>"] },
|
||||||
|
["requestHeaders", "blocking"]
|
||||||
|
);
|
||||||
|
|
||||||
browser.webRequest.onCompleted.addListener(
|
browser.webRequest.onCompleted.addListener(
|
||||||
this.onCompletedHandler.bind(this),
|
this.onCompletedHandler.bind(this),
|
||||||
{
|
{
|
||||||
|
@ -47,26 +54,16 @@ export default class WebEngine {
|
||||||
private async headerHandler(
|
private async headerHandler(
|
||||||
details: OnHeadersReceivedDetailsType
|
details: OnHeadersReceivedDetailsType
|
||||||
): Promise<BlockingResponse> {
|
): Promise<BlockingResponse> {
|
||||||
const def = { responseHeaders: details.responseHeaders };
|
return this.processHandler(details, "handleHeaders", {
|
||||||
if (!this.requests.has(details.requestId)) {
|
responseHeaders: details.responseHeaders,
|
||||||
return def;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
for (const provider of this.contentProviders) {
|
|
||||||
const response = await provider.handleHeaders(details);
|
|
||||||
if (response !== false) {
|
|
||||||
return response as BlockingResponse;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return def;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async proxyHandler(details: OnRequestDetailsType): Promise<any> {
|
private async proxyHandler(details: OnRequestDetailsType): Promise<any> {
|
||||||
let handle = false;
|
let handle = null;
|
||||||
for (const provider of this.contentProviders) {
|
for (const provider of this.contentProviders) {
|
||||||
if (await provider.shouldHandleRequest(details)) {
|
if (await provider.shouldHandleRequest(details)) {
|
||||||
handle = true;
|
handle = provider;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,35 +72,22 @@ export default class WebEngine {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
this.requests.set(details.requestId, details);
|
this.requests.set(details.requestId, handle);
|
||||||
|
|
||||||
for (const provider of this.contentProviders) {
|
return this.processHandler(details, "handleProxy");
|
||||||
const response = await provider.handleProxy(details);
|
|
||||||
if (response !== false) {
|
|
||||||
return response as any;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async requestHandler(
|
private async requestHandler(
|
||||||
details: OnBeforeRequestDetailsType
|
details: OnBeforeRequestDetailsType
|
||||||
): Promise<BlockingResponse> {
|
): Promise<BlockingResponse> {
|
||||||
if (!this.requests.has(details.requestId)) {
|
return this.processHandler(details, "handleRequest");
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const provider of this.contentProviders) {
|
|
||||||
const response = await provider.handleRequest(details);
|
|
||||||
if (response !== false) {
|
|
||||||
return response as BlockingResponse;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async reqHeaderHandler(
|
||||||
|
details: OnBeforeSendHeadersDetailsType
|
||||||
|
): Promise<BlockingResponse> {
|
||||||
|
return this.processHandler(details, "handleReqHeaders");
|
||||||
|
}
|
||||||
private async onCompletedHandler(
|
private async onCompletedHandler(
|
||||||
details: OnCompletedDetailsType
|
details: OnCompletedDetailsType
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
@ -157,4 +141,25 @@ export default class WebEngine {
|
||||||
|
|
||||||
this.requestData.set(requestId, store);
|
this.requestData.set(requestId, store);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async processHandler(
|
||||||
|
details: any,
|
||||||
|
method: string,
|
||||||
|
def = {}
|
||||||
|
): Promise<BlockingResponse> {
|
||||||
|
const provider = this.requests.get(details.requestId) as unknown as {
|
||||||
|
[index: string]: Function;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!provider) {
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
const response = await provider[method](details);
|
||||||
|
|
||||||
|
if (response !== false) {
|
||||||
|
return response as BlockingResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
return def;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue