[Webkit-unassigned] [Bug 255716] Feature request: Implement `BeforeInstallPrompt` event for programmatic Add to Home Screen

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Thu Apr 20 03:11:23 PDT 2023


https://bugs.webkit.org/show_bug.cgi?id=255716

--- Comment #3 from Thomas Steiner <tomac at google.com> ---
It's actually independent of whether the browser would auto-prompt for installation. The way people would use this like so:

```js
// The install button, hidden by default.
import { installButton } from './domrefs.js';

// Variable to stash a reference to the event.
let installEvent = null;

// When UA-defined criteria are met, the UA fires this event.
// For example, when the UA detects a sufficiently complete
// manifest exists. The code now stashes the event and shows
// UI to the user, so they can manually trigger installation.
window.addEventListener('beforeinstallprompt', (event) => {
  event.preventDefault();
  installEvent = event;
  installButton.style.display = 'flex';
});

// When the install button is clicked, trigger the actual
// installation procedure (on iOS, the Add to Home Screen
// flow). The user can still cancel here, so hide or not the
// button, depending on the outcome.
installButton.addEventListener('click', async () => {
  if (!installEvent) {
    return;
  }
  installEvent.prompt();
  const result = await installEvent.userChoice;
  if (result.outcome === 'accepted') {
    installButton.style.display = 'none';
    installEvent = null;
  }
});

// After the app is installed, hide the button again.
window.addEventListener('appinstalled', (event) => {
  installButton.style.display = 'none';
  installEvent = null;
});
```

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20230420/5a6d1d1c/attachment.htm>


More information about the webkit-unassigned mailing list