summaryrefslogtreecommitdiff
path: root/source/libs/post-form.ts
blob: ad8f58512d9cea63a0fe948e259875966c8ddaf3 (plain) (blame)
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, {
		// `as` required until 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;
}