This project is an attempt to fix mingyaulee/Blazor.BrowserExtension#43 where Blazor for Chrome Extension cannot use Razor Class Library (RCL) assets due to _content
being prohibited in Chrome Extension folders. It's also unlikely that there would be an official fix per dotnet/aspnetcore#45531.
You can also use this library as a custom interceptor for IJsRuntime
that perform custom logic before calling InvokeAsync
. It's recommended for Blazor WebAssembly only.
See Demo
folder for sample of a WebAssembly project as well as a Blazor Chrome Extension project using mingyaulee/Blazor.BrowserExtension and KristofferStrube/Blazor.FileSystemAccess RCL.
Install this library through Nuget package Blazor.JsRuntimeRedirect
:
dotnet add package Blazor.JsRuntimeRedirect
Call AddJsRuntimeRedirect
to replace current implementation of IJsRuntime
to the library's wrapper:
builder.Services
// Other services
.AddJsRuntimeRedirect();
Warning
AddJsRuntimeRedirect
searches the currentIServiceCollection
forIJsRuntime
'sImplementationInstance
. If there is none, exception would be thrown.
AddJsRuntimeRedirect
wraps the current ImplementationInstance
of IJsRuntime
to perform additional logic before calling their original methods.
By default, the library attempt to redirect all import
calls with _content
in the path to content
. This would solve mingyaulee/Blazor.BrowserExtension#43. You can override the settings using Options pattern's Configure
or simply configure it with AddJsRuntimeRedirect
method:
builder.Services
.AddJsRuntimeRedirect(options =>
{
options.RedirectAfter = "testcontent";
options.RedirectIdentifiers = new(StringComparer.OrdinalIgnoreCase) { "import", };
});
-
ShouldRedirect
(defaulttrue
): Whether the Redirect logic should be executed.RedirectBefore
,RedirectAfter
,RedirectIdentifiers
are ignored if this is set tofalse
. -
RedirectBefore
(default_content
): the name of the path segment that should be replaced. -
RedirectAfter
(defaultcontent
): the name of the path segment that should be replaced with. -
RedirectIdentifiers
(default{ "import" }
): if set (non-null
), the redirect logic is only considered if theidentifier
(i.e. the calling method, for exampleimport
oralert
) is a value in the list. If set tonull
, every path is considered and therefore is not recommended due to performance impact. -
BeginInvokeJsInterceptor
(defaultnull
): A method that is called before any logic is executed. Here you can modify the values ofidentifier
andargs
. If you setCanceled
totrue
, the call would be terminated.
Note
BeginInvokeJsInterceptor
is called beforeShouldRedirect
logic. It's executed even ifShouldRedirect
isfalse
. However if you setCanceled
totrue
,ShouldRedirect
logic is not executed as the call is cancelled anyway.
From the demo project, an example of using BeginInvokeJsInterceptor
which replaces all prompt
JS call into alert
and cancel the call altogether if the first argument is CancelThis
:
options.BeginInvokeJsInterceptor = values =>
{
if (values.Identifier == "prompt")
{
if (values.Args?.FirstOrDefault() as string == "CancelThis")
{
values.Canceled = true;
}
else
{
values.Identifier = "alert";
}
}
};