tryphotino / photino.Blazor

https://tryphotino.io
Apache License 2.0
332 stars 62 forks source link

Intercept `<a href>` navigation #111

Closed Jinjinov closed 1 week ago

Jinjinov commented 8 months ago

I have a note taking app where users can take notes containing links that get converted to <a href>.

The default behavior of any desktop Blazor app (Photino, MAUI, WPF, WinForms) using any kind of webview is to open the link in the same webview window, but I want external links to open in the default browser.

Adding target="_blank" rel="noopener noreferrer" to <a href> opens the link in a new webview window, which is not what I want.

For: Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView Microsoft.AspNetCore.Components.WebView.Wpf.BlazorWebView Microsoft.AspNetCore.Components.WebView.WindowsForms.BlazorWebView which wrap Microsoft.UI.Xaml.Controls.WebView2 Microsoft.Web.WebView2.Wpf.WebView2 Microsoft.Web.WebView2.WinForms.WebView2

it is possible to intercept <a href> navigation in MAUI Blazor hybrid and open it in the default browser like so:

public MainPage()
{
    InitializeComponent();
    blazorWebView.UrlLoading += OnUrlLoading;
}
private void OnUrlLoading(object? sender, UrlLoadingEventArgs e)
{
    Uri uri = e.Url;
    if (!uri.Host.Contains("0.0.0.0"))
    {
        e.UrlLoadingStrategy = UrlLoadingStrategy.CancelLoad;
        Browser.OpenAsync(uri, BrowserLaunchMode.External);
    }
}

and in WPF like so:

public MainWindow()
{
    InitializeComponent();
    blazorWebView.UrlLoading += OnUrlLoading;
}
private void OnUrlLoading(object? sender, UrlLoadingEventArgs e)
{
    Uri uri = e.Url;
    if (!uri.Host.Contains("0.0.0.0"))
    {
        e.UrlLoadingStrategy = UrlLoadingStrategy.CancelLoad;
        Process.Start(new ProcessStartInfo(uri.ToString()) { UseShellExecute = true });
    }
}

I would like to do the same in Photino, even if only on Linux.

andrew-bedford commented 7 months ago

I'm facing the same issue, except that any link with a target won't open, not even in the same window. I've also tried using onclick="window.open...", but no luck.

MikeYeager commented 1 week ago

Photino is not designed to fire up an external browser. You can spin up a new process in C# to open a browser, but spinning up a new process is not something we're looking at doing in Photino.

Jinjinov commented 12 hours ago

What I am asking in this issue is not to

fire up an external browser

or

spin up a new process in C#

I am asking to intercept the <a href> click, which I believe is within the scope of this library.

It is also something that is supported in

with blazorWebView.UrlLoading += OnUrlLoading;

So I think that my request is not unreasonable. This feature could be used for many things beside spinning up a new process.