Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercept <a href> navigation #111

Open
Jinjinov opened this issue Feb 21, 2024 · 8 comments
Open

Intercept <a href> navigation #111

Jinjinov opened this issue Feb 21, 2024 · 8 comments
Labels
enhancement New feature or request wontfix This will not be worked on

Comments

@Jinjinov
Copy link

Jinjinov commented Feb 21, 2024

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
Copy link

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.

andrew-bedford added a commit to andrew-bedford/photino.Blazor that referenced this issue Mar 2, 2024
This commit provides a workaround for issue tryphotino#111. Users were not able to
open links in an external browser window, only inside the WebView itself.
We now intercept web requests that use the application's scheme (e.g., `app://`)
and, if it does not contain `0.0.0.0` or `localhost`, then we open the link
in the browser.

Resolves tryphotino#111
@MikeYeager MikeYeager added enhancement New feature or request wontfix This will not be worked on labels Oct 10, 2024
@MikeYeager
Copy link
Collaborator

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
Copy link
Author

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

  • class BlazorWebView in namespace Microsoft.AspNetCore.Components.WebView.Wpf
  • class BlazorWebView in namespace Microsoft.AspNetCore.Components.WebView.WindowsForms
  • class BlazorWebView in namespace Microsoft.AspNetCore.Components.WebView.Maui

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.

@MikeYeager
Copy link
Collaborator

@Jinjinov I'm confused. You said, "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." If that's not what you want to do, can you explain your use case in more detail? If you want to intercept the click on the link, you can add an event handler in JavaScript. Or you could use a custom scheme for the URL to handle the click in C#.

@MikeYeager MikeYeager reopened this Oct 24, 2024
@Jinjinov
Copy link
Author

Jinjinov commented Oct 24, 2024

I would like you to add an event UrlLoading on the main Blazor window which triggers when the user clicks on <a href>.

The point of Blazor is to use C# instead of JS wherever possible. Yes, I can use JS to handle clicks on <a href>, but even if they are hardcoded the solution requires unnecessary extra work.

But in my case the <a href> are not known at compile time, because they come from the Markdown when users write their notes which are saved in my app.

Right now I am using code that slows down the conversion from Markdown to Html to add JS to <a href> when users save their Markdown notes and I display them as Html.

Subscribing to <a href> clicked event would simplify and speed up my app. This can be used to for any number of useful features, including opening an external browser.

@Jinjinov
Copy link
Author

@philippjbauer Any chance that you take a look at this? Does it still deserve the "wontfix" label?

@andrew-bedford
Copy link

@Jinjinov , I don't know if it will help you but I ended up simply using PyQt with a QWebEngineView (see repository). It's all I really needed in my case.

@Jinjinov
Copy link
Author

@andrew-bedford Thank you!

My project uses Photino in just 5% of the code, the rest relies heavily on C# https://github.com/Jinjinov/OpenHabitTracker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request wontfix This will not be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants