Skip to content

Instantly share code, notes, and snippets.

@fabiwlf
Created November 30, 2021 21:50
Show Gist options
  • Select an option

  • Save fabiwlf/bef517877326771dc3efb193f7389bb6 to your computer and use it in GitHub Desktop.

Select an option

Save fabiwlf/bef517877326771dc3efb193f7389bb6 to your computer and use it in GitHub Desktop.
Make web.whatsapp.com and any other website with service worker but no manifest installable from browser
const makeManifest = async (manifest) => {
manifest.description = document.head.children.description?.content;
const icons = await Promise.all(Array.from(document.querySelectorAll(`link[rel$="icon"]`)).map((icon) => new Promise((resolve) => {
const img = new Image()
img.src = icon.href || icon.src;
img.onload = resolve;
img.onerror = () => resolve(null);
})));
for (let iconLoadEvent of icons) {
if (!iconLoadEvent) continue;
let [icon] = iconLoadEvent.path;
manifest.icons.push({
"src": icon.src,
"sizes": icon.width + "x" + icon.height
})
}
console.log(manifest);
const manifestURL = URL.createObjectURL(new Blob([JSON.stringify(manifest)], { type: 'application/manifest+json' }));
document.head.innerHTML += `<link rel="manifest" href="${manifestURL}">`;
}
makeManifest({
"name": window.document.title || "PWA",
"short_name": window.document.title || "PWA",
"icons": [
],
"start_url": window.location.href,
"display": "standalone",
"scope": "/" //not required
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment