aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/autofill-otp.ts
blob: b359cb4ee68b98f475628221555e43c0a4cfd440 (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
type AutofillOTPRule = readonly [string, string]

let otpAutofillList: AutofillOTPRule[] = [];

document.addEventListener("DOMContentLoaded", () => {
	const proxiedPost = $.post;

	$.post = function() {
		for (const rule of otpAutofillList) {
			if (arguments[0].endsWith(rule[0])) {
				const element: HTMLInputElement | null = document.querySelector(rule[1]) as HTMLInputElement | null;

				if (element) {
					const callback = arguments[2];
					arguments[2] = (data: any) => {
						element!.value = data?.messageShow || "";
						callback(data);
					};
				}

				break;
			}
		}

		return proxiedPost.apply(this, Array.from(arguments) as any);
	};
});

export const addAutofillOtps = (addedOTPAutofillList: readonly AutofillOTPRule[]) => {
	otpAutofillList = otpAutofillList.concat(addedOTPAutofillList);
};