*Major refactor of WebEngine

This commit is contained in:
Derrick Hammer 2022-08-13 04:16:51 -04:00
parent 7b59584c57
commit a541d89110
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 49 additions and 37 deletions

View File

@ -1,6 +1,7 @@
import {
BlockingResponse,
OnBeforeRequestDetailsType,
OnBeforeSendHeadersDetailsType,
OnHeadersReceivedDetailsType,
OnRequestDetailsType,
} from "../types";
@ -27,6 +28,12 @@ export default abstract class BaseProvider {
return false;
}
async handleReqHeaders(
details: OnBeforeSendHeadersDetailsType
): Promise<BlockingResponse | boolean> {
return false;
}
async handleProxy(details: OnRequestDetailsType): Promise<any | boolean> {
return false;
}

View File

@ -4,6 +4,7 @@ import BaseProvider from "./contentProviders/baseProvider.js";
import {
BlockingResponse,
OnBeforeRequestDetailsType,
OnBeforeSendHeadersDetailsType,
OnCompletedDetailsType,
OnErrorOccurredDetailsType,
OnHeadersReceivedDetailsType,
@ -12,7 +13,7 @@ import {
export default class WebEngine {
private contentProviders: BaseProvider[] = [];
private requests: Map<string, OnBeforeRequestDetailsType> = new Map();
private requests: Map<string, BaseProvider> = new Map();
private requestData: Map<string, {}> = new Map();
constructor() {
@ -30,6 +31,12 @@ export default class WebEngine {
["blocking"]
);
browser.webRequest.onBeforeSendHeaders.addListener(
this.reqHeaderHandler.bind(this),
{ urls: ["<all_urls>"] },
["requestHeaders", "blocking"]
);
browser.webRequest.onCompleted.addListener(
this.onCompletedHandler.bind(this),
{
@ -47,26 +54,16 @@ export default class WebEngine {
private async headerHandler(
details: OnHeadersReceivedDetailsType
): Promise<BlockingResponse> {
const def = { responseHeaders: details.responseHeaders };
if (!this.requests.has(details.requestId)) {
return def;
}
for (const provider of this.contentProviders) {
const response = await provider.handleHeaders(details);
if (response !== false) {
return response as BlockingResponse;
}
}
return def;
return this.processHandler(details, "handleHeaders", {
responseHeaders: details.responseHeaders,
});
}
private async proxyHandler(details: OnRequestDetailsType): Promise<any> {
let handle = false;
let handle = null;
for (const provider of this.contentProviders) {
if (await provider.shouldHandleRequest(details)) {
handle = true;
handle = provider;
break;
}
}
@ -75,35 +72,22 @@ export default class WebEngine {
return {};
}
this.requests.set(details.requestId, details);
this.requests.set(details.requestId, handle);
for (const provider of this.contentProviders) {
const response = await provider.handleProxy(details);
if (response !== false) {
return response as any;
}
}
return {};
return this.processHandler(details, "handleProxy");
}
private async requestHandler(
details: OnBeforeRequestDetailsType
): Promise<BlockingResponse> {
if (!this.requests.has(details.requestId)) {
return {};
}
for (const provider of this.contentProviders) {
const response = await provider.handleRequest(details);
if (response !== false) {
return response as BlockingResponse;
}
}
return {};
return this.processHandler(details, "handleRequest");
}
private async reqHeaderHandler(
details: OnBeforeSendHeadersDetailsType
): Promise<BlockingResponse> {
return this.processHandler(details, "handleReqHeaders");
}
private async onCompletedHandler(
details: OnCompletedDetailsType
): Promise<void> {
@ -157,4 +141,25 @@ export default class WebEngine {
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;
}
}