Menu

Call browser APIs from Blazor without writing JavaScript
📰

Call browser APIs from Blazor without writing JavaScript

DEV Community: blazor·Todd Tanner·6 days ago
#Lpcik0fT
#devtoblazor#devto#dev#app#null
Long article (1440 words) — no AI summary yet.
Reading 0:00
15s threshold

Live demo | Full example on GitHub

You are following a recipe on your phone. Your hands are covered in flour, and the screen goes dark.

Browsers have a fix for this, the Screen Wake Lock API. Blazor has no built-in wrapper for it, so the usual route is a JavaScript module, an IJSRuntime import, and a string name for every function you call. A typo in one of those strings shows up at run time.

This article does it in C# instead. We will build a small recipe page that keeps the screen on, takes the lock back when you return to the tab, and copies the ingredients to the clipboard. You need the .NET 10 SDK and a browser.

The usual way

With plain Blazor interop you write a JavaScript file first:

let sentinel ; export async function lock ( ) { sentinel = await navigator . wakeLock . request ( "screen" ) ; } export async function unlock ( ) { await sentinel ? . release ( ) ; }

Enter fullscreen mode

Exit fullscreen mode

Then you import it and call it by name. That is fine for one function.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.