aboutsummaryrefslogtreecommitdiffstats
path: root/src/scripts/functionality/mutation-application.ts
blob: afc133f17dad510172805ddd67b6af73ba355d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { PDFDocument, StandardFonts } from "pdf-lib";
import { observeDOM } from "@/shared/observe-dom";

const sanghaFacilitationCentreBannerUrl = chrome.runtime.getURL("/assets/sangha-facilitation-centre-banner.jpg");

document.addEventListener("DOMContentLoaded", () => {
	observeDOM(() => {
		const formElement = document.querySelector("#form_MutationApplication > div > div:nth-child(14) > div.col-sm-2.btreset > form");
		const submitButtonElement = document.getElementById("btnDeclareForm");
		if (formElement && submitButtonElement) {
			formElement.removeAttribute("action");
			submitButtonElement.addEventListener("click", e => {
				e.preventDefault();

				(async () => {
					const pdfDoc = await PDFDocument.load(await fetch("/BanglarBhumi/MutationDeclarationForm.action", {
						method: "post",
						credentials: "same-origin",
						headers: {
							"Content-Type": "application/x-www-form-urlencoded"
						},
						body: "hdnAppNo="
					}).then(res => res.arrayBuffer()));
					const pages = pdfDoc.getPages();
					const lastPage = pages[pages.length - 1];
					const { width, height } = lastPage.getSize();
					const bannerImage = await pdfDoc.embedJpg(await fetch(sanghaFacilitationCentreBannerUrl).then(res => res.arrayBuffer()));
					const bannerImageHeight = width * (bannerImage.height / bannerImage.width);

					lastPage.drawImage(bannerImage, {
						x: 0,
						y: 0,
						width: width,
						height: bannerImageHeight
					});

					lastPage.drawText(pdfDoc.getCreationDate()!.toLocaleString(), {
						x: 10,
						y: bannerImageHeight,
						size: 12,
						font: await pdfDoc.embedFont(StandardFonts.TimesRoman)
					});

					const link = document.createElement("a");
					link.href = window.URL.createObjectURL(new Blob([new Uint8Array(await pdfDoc.save())], {type: "application/pdf"}));
					link.download = "Declaration.pdf";
					link.click();
				})();
			});

			return true;
		} else {
			return false;
		}
	});
});