SpawnDev.BlazorJS 1.2.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package SpawnDev.BlazorJS --version 1.2.3                
NuGet\Install-Package SpawnDev.BlazorJS -Version 1.2.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SpawnDev.BlazorJS" Version="1.2.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SpawnDev.BlazorJS --version 1.2.3                
#r "nuget: SpawnDev.BlazorJS, 1.2.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SpawnDev.BlazorJS as a Cake Addin
#addin nuget:?package=SpawnDev.BlazorJS&version=1.2.3

// Install SpawnDev.BlazorJS as a Cake Tool
#tool nuget:?package=SpawnDev.BlazorJS&version=1.2.3                

SpawnDev.BlazorJS

NuGet

Supports .Net 7

An easy Javascript interop library desgined specifcally for client side Blazor.

  • Use Javascript libraries in Blazor without writing any Javascript code.

  • Alternative access to IJSRuntime JS is globally available without injection and is usable on the first line of Program.cs

  • Get and set global proeprties via JS.Set and JS.Get

  • Create new Javascript objects with JS.New

  • Get and set object properties via IJSInProcessObjectReference extended methods

  • Create Callbacks that can be sent to Javascript event listeners or assigned to javascript variables

  • Easily call Services in separate threads with WebWorkers and SharedWebWorkers

NOTE: The below code shows quick examples. Some objects implement IDisposable, such as all JSObject, IJSInProcessObjectReference, and Callback, and need to be disposed when no longer used.

JS

// Get Set
var innerHeight = JS.Get<int>("window.innerHeight");
JS.Set("document.title", "Hello World!");

// Call
var item = JS.Call<string?>("localStorage.getItem", "itemName");
JS.CallVoid("addEventListener", "resize", Callback.Create(() => Console.WriteLine("WindowResized"), _callBacks));

IJSInProcessObjectReference extended

// Get Set
var window = JS.Get<IJSInProcessObjectReference>("window");
window.Set("myVar", 5);
var myVar = window.Get<int>("myVar");

// Call
window.CallVoid("addEventListener", "resize", Callback.Create(() => Console.WriteLine("WindowResized")));

Create a new Javascript object

var worker = JS.New("Worker", myWorkerScript);

Pass callbacks to Javascript

JS.Set("testCallback", Callback.Create<string>((strArg) => {
    Console.WriteLine($"Javascript sent: {strArg}");
    // this prints "Hello callback!"
}));
// in Javascript
testCallback('Hello callback!');

JSObject

Use the extended functions of IJSInProcessObjectReference to work with Javascript objects or use the growing library of over 100 of the most common Javascript objects, including ones for Window, HTMLDocument, WebStorage (locaStorage and sessionStorage), WebGL, WebRTC, and more in SpawnDev.BlazorJS.JSObjects. JSObjects are wrappers around IJSInProcessObjectReference that allow strongly typed use.

Custom JSObjects

Implement your own JSObject classes for Javascript objects not already available in the BlazorJS.JSObjects library.

Instead of this (simple but not as reusable)

var audio = JS.New("Audio", "https://some_audio_online");
audio.CallVoid("play");

Do this...
Create a custom JSObject class

[JsonConverter(typeof(JSObjectConverter<Audio>))]
public class Audio : JSObject
{
    public Audio(IJSInProcessObjectReference _ref) : base(_ref) { }
    public Audio(string url) : base(JS.New("Audio", url)) { }
    public void Play() => JSRef.CallVoid("play");
}

Then use your new object

var audio = new Audio("https://some_audio_online");
audio.Play();

SpawnDev.BlazorJS.WebWorkers

(Nuget coming soon...)
Run CPU intensive tasks on a dedicated worker or on a shared worker with WebWorkers!

Example WebWorkerService setup and usage

// Program.cs
...
using SpawnDev.BlazorJS;
using SpawnDev.BlazorJS.WebWorkers;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
if (JS.IsWindow)
{
    // we can skip adding dom objects in non UI threads
    builder.RootComponents.Add<App>("#app");
    builder.RootComponents.Add<HeadOutlet>("head::after");
}
// add services
builder.Services.AddSingleton((sp) => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// SpawnDev.BlazorJS.WebWorkers
builder.Services.AddSingleton<WebWorkerService>();
// app specific services...
builder.Services.AddSingleton<MathsService>();
// build 
WebAssemblyHost host = builder.Build();
// init WebWorkerService
var workerService = host.Services.GetRequiredService<WebWorkerService>();
await workerService.InitAsync();
await host.RunAsync();

WebWorker


// Create a WebWorker
var webWorker = await workerService.GetWebWorker();

// Call a registered service on the worker thread with your arguments
// Action types can be passed for progress reporting
var result = await webWorker.InvokeAsync<MathsService, string>("CalculatePiWithActionProgress", piDecimalPlaces, new Action<int>((i) =>
{
    piProgress = i;
    StateHasChanged();
}));

SharedWebWorker

Calling GetSharedWebWorker in another window with the same sharedWorkerName will return the same SharedWebWorker

// Create or get SHaredWebWorker with the provided sharedWorkerName
var sharedWebWorker = await workerService.GetSharedWebWorker("workername");

// Just like WebWorker but shared
// Call a registered service on the worker thread with your arguments
var result = await webWorker.InvokeAsync<MathsService, string>("CalculatePiWithActionProgress", piDecimalPlaces, new Action<int>((i) =>
{
    piProgress = i;
    StateHasChanged();
}));

Send events

// Optionally listen for event messages
worker.OnMessage += (sender, msg) =>
{
    if (msg.TargetName == "progress")
    {
        PiProgress msgData = msg.GetData<PiProgress>();
        piProgress = msgData.Progress;
        StateHasChanged();
    }
};

// From SharedWebWorker or WebWorker threads send an event to conencted parents
workerService.SendEventToParents("progress", new PiProgress { Progress = piProgress });

// Or on send an event to a connected worker
webWorker.SendEvent("progress", new PiProgress { Progress = piProgress });

Inspired by Tewr's BlazorWorker implementation. Thank you! I wrote my implementation from scratch as I needed workers in .Net 7.
https://github.com/Tewr/BlazorWorker

BlazorJS and WebWorkers Demo
https://blazorjs.spawndev.com/

Buy me a coffee

paypal

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (10)

Showing the top 5 NuGet packages that depend on SpawnDev.BlazorJS:

Package Downloads
SpawnDev.BlazorJS.WebWorkers

Call Services and static methods in separate threads with WebWorkers and SharedWebWorkers. Run Blazor WASM in the ServiceWorker.

SpawnDev.BlazorJS.WebTorrents

WebTorrents in Blazor WebAssembly

SpawnDev.BlazorJS.VisNetwork

VisNetwork in Blazor WebAssembly

SpawnDev.BlazorJS.FFmpegWasm

SpawnDev.BlazorJS.FFmpegWasm is a Blazor WASM wrapper around ffmpeg.wasm and contains only the base ffmpeg.js and 814.ffmpeg.js files.

SpawnDev.BlazorJS.SimplePeer

SimplePeer WebRTC video, voice, and data channels for Blazor WebAssembly

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.5.18 23 11/17/2024
2.5.17 38 11/16/2024
2.5.16 31 11/15/2024
2.5.15 34 11/15/2024
2.5.14 42 11/14/2024
2.5.13 77 11/13/2024
2.5.12 82 11/10/2024
2.5.11 178 10/31/2024
2.5.10 949 10/9/2024
2.5.9 181 9/27/2024
2.5.8 2,910 8/13/2024
2.5.7 95 8/13/2024
2.5.6 79 8/8/2024
2.5.5 257 8/7/2024
2.5.4 125 8/6/2024
2.5.3 89 8/5/2024
2.5.2 93 8/5/2024
2.5.1 230 7/26/2024
2.5.0 108 7/26/2024
2.4.7 115 7/24/2024
2.4.6 213 7/22/2024
2.4.5 178 7/19/2024
2.4.4 138 7/18/2024
2.4.3 253 7/16/2024
2.4.2 96 7/15/2024
2.4.0 95 7/15/2024
2.3.8 101 7/14/2024
2.3.7 1,107 7/9/2024
2.3.6 123 7/8/2024
2.3.5 136 7/6/2024
2.3.4 292 7/4/2024
2.3.3 383 6/23/2024
2.3.2 317 6/16/2024
2.3.1 243 6/13/2024
2.3.0 178 6/12/2024
2.2.106 247 6/5/2024
2.2.105 285 5/31/2024
2.2.104 170 5/30/2024
2.2.103 158 5/29/2024
2.2.102 168 5/28/2024
2.2.101 137 5/22/2024
2.2.100 163 5/17/2024
2.2.99 124 5/17/2024
2.2.98 130 5/16/2024
2.2.97 137 5/15/2024
2.2.96 97 5/14/2024
2.2.95 120 5/13/2024
2.2.94 468 5/11/2024
2.2.93 130 5/7/2024
2.2.92 125 5/7/2024
2.2.91 148 5/3/2024
2.2.90 91 5/3/2024
2.2.89 79 5/2/2024
2.2.88 90 5/2/2024
2.2.87 311 4/26/2024
2.2.86 136 4/26/2024
2.2.85 421 4/18/2024
2.2.84 121 4/18/2024
2.2.83 154 4/16/2024
2.2.82 484 4/8/2024
2.2.81 133 4/8/2024
2.2.80 143 4/7/2024
2.2.79 137 4/6/2024
2.2.78 136 4/5/2024
2.2.77 146 4/5/2024
2.2.76 144 4/4/2024
2.2.75 127 4/4/2024
2.2.73 126 4/3/2024
2.2.72 130 4/3/2024
2.2.71 152 4/3/2024
2.2.70 129 4/2/2024
2.2.69 321 4/1/2024
2.2.68 159 3/29/2024
2.2.67 292 3/27/2024
2.2.66 183 3/24/2024
2.2.65 151 3/21/2024
2.2.64 210 3/11/2024
2.2.63 176 3/9/2024
2.2.62 141 3/7/2024
2.2.61 146 3/6/2024
2.2.60 137 3/6/2024
2.2.58 186 3/2/2024
2.2.57 663 2/24/2024
2.2.56 172 2/18/2024
2.2.55 129 2/17/2024
2.2.53 135 2/15/2024
2.2.52 148 2/15/2024
2.2.51 131 2/15/2024
2.2.50 120 2/13/2024
2.2.49 908 2/2/2024
2.2.48 1,396 12/29/2023
2.2.47 188 12/20/2023
2.2.46 198 12/15/2023
2.2.45 159 12/10/2023
2.2.44 139 12/10/2023
2.2.42 152 12/9/2023
2.2.41 155 12/9/2023
2.2.40 130 12/8/2023
2.2.38 1,184 11/21/2023
2.2.37 461 11/16/2023
2.2.36 127 11/16/2023
2.2.35 163 11/14/2023
2.2.34 113 11/13/2023
2.2.33 85 11/10/2023
2.2.32 103 11/10/2023
2.2.31 83 11/9/2023
2.2.28 99 11/7/2023
2.2.27 156 10/31/2023
2.2.26 212 10/22/2023
2.2.25 106 10/20/2023
2.2.24 92 10/20/2023
2.2.23 97 10/20/2023
2.2.22 90 10/20/2023
2.2.21 100 10/20/2023
2.2.20 95 10/19/2023
2.2.19 87 10/19/2023
2.2.18 87 10/19/2023
2.2.17 191 10/13/2023
2.2.16 484 10/12/2023
2.2.15 90 10/12/2023
2.2.14 133 10/5/2023
2.2.13 111 10/5/2023
2.2.12 88 10/5/2023
2.2.11 253 10/3/2023
2.2.10 218 9/18/2023
2.2.9 86 9/18/2023
2.2.8 278 9/14/2023
2.2.7 111 9/13/2023
2.2.6 6,589 9/6/2023
2.2.5 131 8/30/2023
2.2.4 165 8/26/2023
2.2.3 125 8/20/2023
2.2.2 112 8/18/2023
2.2.1 120 8/11/2023
2.2.0 206 7/17/2023
2.1.15 126 5/26/2023
2.1.14 107 5/20/2023
2.1.13 125 4/26/2023
2.1.12 187 4/21/2023
2.1.11 112 4/19/2023
2.1.10 119 4/19/2023
2.1.8 152 4/10/2023
2.1.7 173 3/27/2023
2.1.6 165 3/24/2023
2.1.5 137 3/23/2023
2.1.4 135 3/23/2023
2.1.3 137 3/23/2023
2.1.2 141 3/21/2023
2.1.0 139 3/21/2023
2.0.3 140 3/21/2023
2.0.2 137 3/20/2023
2.0.1 142 3/20/2023
2.0.0 143 3/20/2023
1.9.2 149 3/14/2023
1.8.1 136 3/11/2023
1.8.0 132 3/10/2023
1.7.1 139 3/10/2023
1.7.0 137 3/8/2023
1.6.4 152 3/1/2023
1.6.3 359 1/31/2023
1.6.2 373 1/24/2023
1.6.1 397 1/11/2023
1.6.0 392 1/11/2023
1.5.0 454 12/23/2022
1.4.0 407 12/20/2022
1.3.0 409 12/16/2022
1.2.7 352 12/16/2022
1.2.5 367 12/14/2022
1.2.4.1 350 12/13/2022
1.2.4 350 12/13/2022
1.2.3 348 12/13/2022
1.2.1 336 12/10/2022
1.2.0 337 12/10/2022
1.1.0 331 12/6/2022
1.0.0 335 12/6/2022