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; };