We can trigger vibration on supported devices like so:
navigator.vibrate(50);
But does the API support any access to user-defaults? In other words, is it possible to get the vibration duration from the OS? So when a user would normally press a button when using the device, get that vibration duration (if any)?
This is currently not possible directly or indirectly.
Per the specification,
Vibration API is not a source of data on its own and as such is not producing any data possible to consume on the Web.
Therefore, no data can be obtained via the vibration API directly.
However, the section also states
[I]t is known that it can serve as a source of events for other APIs. In particular, it is known that certain sensors such as accelerometers or gyroscopes are prone to tiny imperfections during their manufacturing. As such, they provide a fingerprinting surface that can be exploited utilizing the vibration stimuli generated via the Vibration API.
So in theory, yes, you could obtain the actual duration by using the accelerators and/or gyroscopes and transforming the data appropriately.
However, there's another caveat: per the WebIDL, a VibratePattern
(defined as number | number[]
) must be provided as an argument. As such, you can't invoke vibration with the default.
I personally doubt this will be converted to an optional parameter in a future version of the specification due to privacy considerations (this very much allows for fingerprinting).
Keep in mind, though, that not all platforms even support vibration, and will return false
when called. It's also perfectly valid to refuse action (which will also return false
)