-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop using chrome.webRequest.onSendHeaders
- Loading branch information
Showing
7 changed files
with
145 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export class Headers {[name: string] : string} | ||
|
||
export function makeEvent<T>(eventName: string, args: T) { | ||
return new CustomEvent<T>( | ||
eventName, | ||
{detail: args} | ||
) | ||
} | ||
|
||
export function subscribe<T>(eventName: string, callback: (args: T) => void) { | ||
addEventListener(eventName, (e: CustomEvent<T>) => callback(e?.detail)) | ||
} | ||
|
||
|
||
export class HeadersHandlerMessageEvent { | ||
static eventName = 'header-handler-message' | ||
url?: string; | ||
headers: Headers; | ||
|
||
static makeEvent(args: HeadersHandlerMessageEvent) { | ||
return new CustomEvent<HeadersHandlerMessageEvent>( | ||
this.eventName, | ||
{detail: args} | ||
) | ||
} | ||
|
||
static subscribe(callback: (args: HeadersHandlerMessageEvent) => void) { | ||
addEventListener(this.eventName, | ||
(e: CustomEvent<HeadersHandlerMessageEvent>) => callback(e?.detail)) | ||
} | ||
} | ||
|
||
export class RegisterHeadersHandlerEvent{ | ||
urlPattern?: string; | ||
static eventName = 'register-header-handler' | ||
|
||
static makeEvent(args: RegisterHeadersHandlerEvent) { | ||
return new CustomEvent<RegisterHeadersHandlerEvent>( | ||
this.eventName, | ||
{detail: args} | ||
) | ||
} | ||
|
||
static subscribe<T>(callback: (args: RegisterHeadersHandlerEvent) => void) { | ||
addEventListener(this.eventName, | ||
(e: CustomEvent<RegisterHeadersHandlerEvent>) => callback(e?.detail)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
import {HeadersHandlerMessageEvent, RegisterHeadersHandlerEvent} from '../common/XHREvents' | ||
|
||
let headerProcessUrlPattern: RegExp | undefined; | ||
class CustomXMLHttpRequest extends XMLHttpRequest { | ||
headers: {[header: string]: string} = {} | ||
url: string | undefined; | ||
|
||
override open(method: string, url: string | URL): void; | ||
override open( | ||
method: string, url: string | URL, isAsync: boolean, | ||
username?: any): void; | ||
override open( | ||
method: string, url: string | URL, isAsync?: boolean, | ||
username?: any, | ||
password?: any): void { | ||
|
||
this.url = url instanceof URL ? url.toString() : url | ||
const realAsync = isAsync ? isAsync : true; | ||
super.open(method, url, realAsync, username, password); | ||
} | ||
|
||
override setRequestHeader(name: string, value: string): void { | ||
this.headers[name] = value | ||
super.setRequestHeader(name, value) | ||
} | ||
|
||
override send(body?: any): void { | ||
if (!headerProcessUrlPattern || !this.url || this.url.search(headerProcessUrlPattern) >= 0) { | ||
window.dispatchEvent(HeadersHandlerMessageEvent.makeEvent( | ||
{url: this.url, headers: this.headers})) | ||
} | ||
super.send(body) | ||
} | ||
} | ||
|
||
const initHeadersHandler = (urlPattern?: string) => { | ||
XMLHttpRequest = CustomXMLHttpRequest | ||
headerProcessUrlPattern = urlPattern ? new RegExp(urlPattern) : undefined | ||
} | ||
|
||
|
||
export const initScript = () => { | ||
RegisterHeadersHandlerEvent.subscribe((e) => { | ||
const urlPattern = e.urlPattern | ||
initHeadersHandler(urlPattern) | ||
}) | ||
} | ||
|
||
initScript() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters