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

Blazor Hybrid - Allow window.open() from JavaScript to automatically open a WebView #20622

Open
mrlife opened this issue Feb 15, 2024 · 11 comments
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView Blazor ❤️ MAUI Issues in MAUI functionality that affect Blazor, but are not bugs in Blazor itself proposal/open t/enhancement ☀️ New feature or request
Milestone

Comments

@mrlife
Copy link

mrlife commented Feb 15, 2024

Description

Allow Blazor Hybrid apps to respond to window.open() from JavaScript and open in a WebView.

Right now, window.open() does not run.

Public API Changes

The following button conforms to a third party API's spec for launching an authentication workflow via window.open().

BlazorWebView should open a WebView just as a browser would open a window.

<button id="ThirdPartyAuthButton">Authenticate with third party</button>

Intended Use-Case

When using a third party JavaScript API, sometimes the API opens a new browser window using window.open() to facilitate authentication with the third party.

This works well with Blazor but does not work with Blazor Hybrid.

@PureWeen PureWeen added area-blazor Blazor Hybrid / Desktop, BlazorWebView Blazor ❤️ MAUI Issues in MAUI functionality that affect Blazor, but are not bugs in Blazor itself labels Feb 15, 2024
@PureWeen PureWeen added this to the Backlog milestone Feb 15, 2024
@ghost
Copy link

ghost commented Feb 15, 2024

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@Eilon Eilon removed this from the Backlog milestone Mar 7, 2024
@Eilon
Copy link
Member

Eilon commented Apr 1, 2024

@mrlife - Can you clarify exactly what you expect to happen with window.open? Also, can you show the exact call to window.open that you'd like to use?

I tried this:

<button id="ThirdPartyAuthButton" onclick="window.open('https://bing.com/')">Authenticate with third party</button>

And on Windows it worked fine. On Android interestingly it crashed the app (more details below). But a lot of this can depend on exactly what you want to happen, and exactly which parameters are passed to window.open.

Details on the Android crash (which is certainly a bug in BlazorWebView):

With <button onclick="window.open('https://bing.com/')">Open</button> it ends up getting a Java exception in this call to the Java Uri.Parse (this is not the .NET Uri type) because the requestUrl variable is null. I debugged it to see if there's some other way to get the URL in this scenario but I couldn't find it. The view parameter doesn't seem to have anything interesting on it. So I think it's easy to make it not throw an exception (just check for null and ignore), I'm not sure how to make this scenario work. I also haven't tested other variations of window.open because that might affect whether this works.

@mrlife
Copy link
Author

mrlife commented Apr 4, 2024

Hi @Eilon, thanks for checking this out!

On Windows, did it open in a WebView or the external browser?

Can you clarify exactly what you expect to happen with window.open?

An in-app WebView that loads the URL would be best since it won't take the user away from the app.

Also, can you show the exact call to window.open that you'd like to use?

Here is an example of what the window.open call would look like:

window.open("https://example.com?a=1&b=2", "example-window-name", "height=500,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,width=500");

@mkArtakMSFT mkArtakMSFT added the t/enhancement ☀️ New feature or request label Apr 26, 2024
@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone Apr 26, 2024
@f4n0
Copy link

f4n0 commented May 30, 2024

any news?

@mrlife
Copy link
Author

mrlife commented Jun 14, 2024

Hi @Eilon, I was wondering if you thought this was something that could be implemented. I really appreciate your time.

@rkokal
Copy link

rkokal commented Jul 18, 2024

@Eilon I am also looking towards a solution to this.

@Eilon
Copy link
Member

Eilon commented Jul 31, 2024

Hi, I'm not sure when I'll have more time to investigate, but there's a workaround that I think in general will work fine:

  1. Handle the button/link's 'click' event in C#, for example: <button @onclick="HandleWindowOpen">Open Link</button>
  2. In the C# event handler launch whatever window/browser you want: void HandleWindowOpen(object sender, EventArgs e) => <run code to launch browser/window>

@mrlife
Copy link
Author

mrlife commented Aug 1, 2024

Hi @Eilon, thanks for the suggestion. For the purpose behind creating this issue, the workaround does not seem to apply, as the window.open() is called by a third party script. Please let me know if I misunderstood something.

@Eilon
Copy link
Member

Eilon commented Aug 5, 2024

@mrlife ah understood, then indeed the workaround won't quite apply there. We'll keep this bug open so that we can investigate a potential fix.

@f4n0
Copy link

f4n0 commented Sep 4, 2024

Hi @Eilon
Is there something that we may do to speed up the resolution of this issue?

@Eilon
Copy link
Member

Eilon commented Sep 19, 2024

@f4n0 hmm I'm not sure. Is it possible to monkey-patch the window.open() API so that it does call into C#, and then open a new window?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView Blazor ❤️ MAUI Issues in MAUI functionality that affect Blazor, but are not bugs in Blazor itself proposal/open t/enhancement ☀️ New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants