-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
post-form.ts
22 lines (19 loc) · 972 Bytes
/
post-form.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
export default async function postForm(form: HTMLFormElement): Promise<Response> {
// `content.fetch` is Firefox’s way to make fetches from the page instead of from a different context
// This will set the correct `origin` header without having to use XMLHttpRequest
// https://stackoverflow.com/questions/47356375/firefox-fetch-api-how-to-omit-the-origin-header-in-the-request
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#XHR_and_Fetch
const contentFetch = typeof window.content === 'object' ? window.content.fetch : window.fetch;
const response = await contentFetch(form.action, {
// TODO: drop `as` after https://github.com/microsoft/TSJS-lib-generator/issues/741
body: new URLSearchParams(new FormData(form) as URLSearchParams),
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
if (!response.ok) {
throw new Error(response.statusText);
}
return response;
}