From 4b1717c021481d7e008debd532b7dc6d4852db8e Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 31 Jul 2022 00:52:47 -0400 Subject: [PATCH] *Add requestData map to store arbitrary state information for requests *Add getRequestData and setRequestData methods --- src/webEngine.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/webEngine.ts b/src/webEngine.ts index c14a89e..291242c 100644 --- a/src/webEngine.ts +++ b/src/webEngine.ts @@ -13,6 +13,7 @@ import { export default class WebEngine { private contentProviders: BaseProvider[] = []; private requests: Map = new Map(); + private requestData: Map = new Map(); constructor() { browser.webRequest.onHeadersReceived.addListener( @@ -28,6 +29,7 @@ export default class WebEngine { { urls: [""] }, ["blocking"] ); + browser.webRequest.onCompleted.addListener( this.onCompletedHandler.bind(this), { @@ -108,6 +110,10 @@ export default class WebEngine { if (this.requests.has(details.requestId)) { this.requests.delete(details.requestId); } + + if (this.requestData.has(details.requestId)) { + this.requests.delete(details.requestId); + } } private async onErrorHandler( @@ -116,6 +122,10 @@ export default class WebEngine { if (this.requests.has(details.requestId)) { this.requests.delete(details.requestId); } + + if (this.requestData.has(details.requestId)) { + this.requests.delete(details.requestId); + } } public registerContentProvider(provider: BaseProvider) { @@ -125,4 +135,26 @@ export default class WebEngine { this.contentProviders.push(provider); } + + public getRequestData(requestId: string, key: string) { + if (!this.requestData.has(requestId)) { + return undefined; + } + + const store: any = this.requestData.get(requestId); + + return store[key]; + } + + public setRequestData(requestId: string, key: string, value: any) { + let store: any = {}; + + if (this.requestData.has(requestId)) { + store = this.requestData.get(requestId); + } + + store[key] = value; + + this.requestData.set(requestId, store); + } }