summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/frontend/webapk.js
blob: 73e12bf0f9044100e380cf00af975c947410a7d1 (plain) (blame)
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
const getWebApk = async function (fileName, manifestUrl = document.querySelector("link[rel=manifest]").href) {
	Uint8Array.prototype.toBase64 ??= function () {
		return btoa(Array.from(this, v => String.fromCharCode(v)).join(""));
	}

	const manifest = await (await fetch(manifestUrl)).json();
	if (!manifest.start_url) {
		alert('Manifest start_url missing');
		return;
	}
	if (!manifest.icons) {
		alert('Manifest icons missing');
		return;
	}
	const startUrl = new URL(manifest.start_url, manifestUrl);
	const imageData = new Uint8Array(await (await fetch(new URL(manifest.icons.find(v => v.type === "image/png").src, manifestUrl))).arrayBuffer()).toBase64();

	const {downloadUrl} = await (
		await fetch("https://webapk.googleapis.com/v1/webApks/?key=AIzaSyAoI6v-F31-3t9NunLYEiKcPIqgTJIUZBw", {
			method: "POST",
			body: JSON.stringify({
				appKey: startUrl, manifestUrl, requesterApplicationPackage: "com.android.chrome",
				manifest: {
					name: manifest.name,
					startUrl,
					id: startUrl,
					scopes: [new URL(".", startUrl)],
					icons: [{imageData}],
					displayMode: manifest.display
				}
			}),
			headers: {
				"content-type": "application/json",
			},
		})
	).json();

	if (typeof downloadUrl !== 'string') {
		alert('Error generating APK');
		return;
	}

	location = downloadUrl;
	return;
};