A Blazor library that provides easy access to browser APIs without the need to use any JavaScript.
Since JavaScript functionality for Blazor is primarly used for accessing BrowserAPIs, for most applications there will be no need to write any additional JavaScript.
It supports asynchronous communication as well as synchronous (JSRuntime and JSInProcessRuntime).
You can find the test page at blazor-browserapi.firerocket.de.
- Clipboard
- Console
- CookieStorage
- Dialog
- Download
- Geolocation
- HTMLElement
- HTMLMediaElement (audio/video)
- Language
- LocalStorage
- MediaDevices (microphone/camera)
- SensorAPI
- ServiceWorker
- SessionStorage
- Add PackageReference to your .csproj file
<ItemGroup>
<PackageReference Include="Blazor.BrowserAPI" Version="{latest version}" />
</ItemGroup>
- Register BrowserAPI in your dependency container
using BrowserAPI;
services.AddBrowserAPI();
The necessary JS file is fetched automatically the first time. However, if you use a functionality of BrowserAPI synchronous (JSInProcessRuntime) before the download has finished, you get an error like:
JS-module is not loaded yet. To make sure the module is downloaded, you can await IModuleLoader.ModuleDownload.
To ensure the download has finished, you can use the IModuleManager interface.
There exists 2 namespaces in this Library: BrowserAPI and BrowserAPI.Implementation. Members inside the BrowserAPI.Implementation namespace should not be used directly and there is no guarantee that the members of a class will be stable. However, the class names inside BrowserAPI.Implementation will be stable, so using them for service provider registration is fine.
This package is in preview and breaking changes may occur.
There are more interfaces coming.
- 0.1
- first version, includes 10 BrowserAPIs: Clipboard, Console, CookieStorage, Dialog, Download, HTMLElement, Language, LocalStorage, ServiceWorker, SessionStorage
- 0.1.1
- changed SetCookie parameters: expires to seconds and samesite to string
- 0.1.2
- changed internal classes to public and moved them to namespace BrowserAPI.Implementation
- 0.2.0
- 0.3.0
- added Geolocation (11 BrowserAPIs)
- 0.4.0
- breaking change: removed HTMLElementFactory and DialogFactory and added ElementFactory/ElementFactoryInProcess instead
- 0.5.0
- added HTMLMediaElement (12 BrowserAPIs)
- 0.6.0
- added MediaDevices (13 BrowserAPIs)
- 0.7.0
- added SensorAPI (14 BrowserAPIs)
- changed events to use sync interop if possible
- 0.7.1
- added .NET 9 and removed obsolete versions .NET 6 and .NET 7