From 7a9d04d8e1259c4a3f9229c7d5adef12b39bba71 Mon Sep 17 00:00:00 2001
From: Marcos Caceres
DOM events fired by this specification use the application
life-cycle task source.
BeforeInstallPromptEvent
Interface
+ + [Constructor(DOMString typeArg, optional BeforeInstallPromptEventInit eventInit)] + interface BeforeInstallPromptEvent : Event { + readonly attribute Promise<AppBannerPromptOutcome> userChoice; + Promise<UserResponseObject> prompt(); + }; + + dictionary BeforeInstallPromptEventInit : EventInit { + AppBannerPromptOutcome userChoice; + }; + + dictionary UserResponseObject { + AppBannerPromptOutcome userChoice; + }; ++
+ The BeforeInstallPromptEvent is dispatched prior to + activating an automated install prompt, allowing a developer + to prevent the default action for an install prompt. +
+
+ Thus, the default action of the BeforeInstallPromptEvent is to
+ present an automated install
+ prompt to the end-user. Canceling the default action (via
+ .preventDefault()
) prevents the user agent from
+ presenting an automated
+ install prompt until a later time (see
+ BeforeInstallPromptEvent.prompt() method).
+
+ The BeforeInstallPromptEvent has three internal slots, which + are set when the event is constructed: +
+false
. Represents if this event
+ was used to present an install prompt to the end-user.
+ null
. Represents the outcome of presenting an
+ install prompt.
+
+ To construct an instance of the BeforeInstallPromptEvent
+ interface, run the steps to construct a
+ BeforeInstallPromptEvent
event:
+
false
.
+ null
.
+ eventInit.userChoice
is valid, then:
+ eventInit.userChoice
.
+ eventInit.userChoice
.
+ prompt()
method
+ + The prompt method, when called, runs the following + steps: +
+null
,
+ resolve p with [[\userResponsePromise]] and
+ terminate this algorithm.
+ isTrusted
attribute is
+ false
, reject [[\userResponsePromise]] with
+ NotAllowedError, optionally informing the developer
+ that untrusted events can't call prompt()
.
+ false
, then
+ request to present an install prompt and wait, possibly
+ indefinitely, for the end-user to make a choice.
+ + To request to present an install prompt with + BeforeInstallPromptEvent event: +
+userChoice
member is
+ set to event's [[\promptOutcome]].
+ + This example shows how one might prevent an automated install + prompt from showing until the user has finished a set of tasks. + Those tasks are represented as an array of promises, which the + application "awaits" to finish before an install prompt is + presented to the end-user. +
++ window.addEventListener("beforeinstallprompt", async function(event) { + event.preventDefault(); + // await e.g., user composing an email... + await Promise.all(tasksThatPreventsInstallation); + const { userChoice } = await event.prompt(); + console.info(`user selected: ${userChoice}`); + }); ++
Window
object