aboutsummaryrefslogtreecommitdiffstats
path: root/src/scripts/functionality/view-khatian/index.ts
blob: 1ca37c50d79a5324b9a9d873d66ce124dd9cf5b3 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { generateWebPage } from "@/shared/generate-web-page";
import { interceptPost } from "@/shared/intercept-jquery-ajax";
import getDownloadInformationPDFPageContent from "@/scripts/functionality/view-khatian/download-information-pdf-page-content.html";

declare function load(): void;

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

document.addEventListener("DOMContentLoaded", () => {
	const submitButtonElement = $("#khbutton");
	const separatorElement = $("#bodycover > div > form > hr:nth-child(2)");
	const formElement = $("#bodycover > div > form");
	let downloadPDFButton: JQuery<HTMLButtonElement> | null = null;

	const submitButtonElementComputedStyles = window.getComputedStyle(submitButtonElement[0]);
	const styles = Array.from(submitButtonElementComputedStyles)
		.reduce(
			(str, property) => `${str}${property}:${submitButtonElementComputedStyles.getPropertyValue(property)};`,
			""
		);

	const getValueOfSelectElement = (selector: string) => {
		const element = document.querySelector<HTMLSelectElement>(selector)!;
		return element.options[element.selectedIndex].text;
	};

	let isPlotInformation: boolean | null = null;

	const downloadInformationPDF = () => {
		generateWebPage(
			getDownloadInformationPDFPageContent({
				sanghaFacilitationCentreBannerUrl,
				isPlotInformation: !!isPlotInformation,
				details: $(isPlotInformation ? "#plotdetails" : "#khdetails").html(),
				district: getValueOfSelectElement("#lstDistrictCode1"),
				block: getValueOfSelectElement("#lstBlockCode1"),
				mouza: getValueOfSelectElement("#lstMouzaList")
			}),
			isPlotInformation
				? "Plot Details"
				: "Khatian Details"
		);
	};

	const showDownloadButton = (args: any) => {
		if (isPlotInformation !== null) {
			const callback = args[2];
			args[2] = (data: any) => {
				callback(data);
				const tableElement = $(isPlotInformation
					? "#plotdetails > div:nth-child(1) > div:nth-child(4) > table > tbody"
					: "#khdetails > table > tbody > tr > td:nth-child(1) > div:nth-child(5) > table > tbody"
				);

				if (tableElement.length > 0) {
					tableElement.css("height", "auto");
					tableElement.css("width", "100%");

					if (!downloadPDFButton) {
						downloadPDFButton = $(document.createElement("button"));
						downloadPDFButton.html("Download PDF");
						downloadPDFButton[0].style.cssText = styles;
						downloadPDFButton.css("width", "auto");
						downloadPDFButton.css("margin-bottom", "1rem");
						downloadPDFButton.click(downloadInformationPDF);
						formElement[0].insertBefore(downloadPDFButton[0], separatorElement[0]);
					}
				} else if (downloadPDFButton) {
					$(downloadPDFButton).remove();
					isPlotInformation = null;
					downloadPDFButton = null;
				}
			}
		}
	};

	interceptPost("plotDetailsAction_LandInfo.action", args => {
		isPlotInformation = true;
		showDownloadButton(args);
	});
	interceptPost("plotDetailsAction_LandInfo.action", args => {
		isPlotInformation = false;
		showDownloadButton(args);
	});
});