question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to block popups in BlazorWebView

See original GitHub issue

Description

When trying to manually handle CoreWebViewOnNewWindowRequested Event we are unable to supress the starting of an external process opening the file dragged into the webiew. It will always open for example Notepad when dragging a .txt file into it.

I managed to locate why this is happening: https://github.com/dotnet/maui/commit/78c1ffe593965d16b5aacd9d320e19c1cac55031#diff-75c4562a2bc68615b8d28c071b33a112272d1e08e4ba087c461736787ff8f534

I think it would be better to check if args.Handled is already set to true and do not proceed if condition matches.

    private void CoreWebView2_NewWindowRequested(object? sender, CoreWebView2NewWindowRequestedEventArgs args)
    {
        // Intercept _blank target <a> tags to always open in device browser.
        // The ExternalLinkCallback is not invoked.

        if (!args.Handled && Uri.TryCreate(args.Uri, UriKind.RelativeOrAbsolute, out var uri))
        {
            LaunchUriInExternalBrowser(uri);
            args.Handled = true;
        }
    }

I did not test it and it may depend on the order of execution of the event listeners. But there has to be a way to disable it. This also prevents any js popup to appear through the webview.

Steps to Reproduce

  1. Create a WinForms app with BlazorWebView.
  2. Drag any file into the webview
  3. A Process will start

Version with bug

6.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows, macOS, Other (Tizen, Linux, etc. not supported by Microsoft directly)

Affected platform versions

Any Version of Windows, MacOS or Linux

Did you find any workaround?

If the page has loaded just do this. It will remove any event handler that was not registered by current class:

    var coreWebView = _blazorWebView.WebView.CoreWebView2;
         
    var eventHandlerField = (EventHandler<CoreWebView2NewWindowRequestedEventArgs>) coreWebView.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
            .First(it => it.Name == "newWindowRequested").GetValue(coreWebView);

    foreach (EventHandler<CoreWebView2NewWindowRequestedEventArgs> subscriber in eventHandlerField.GetInvocationList())
    {
        if (subscriber.Method.DeclaringType == GetType()) continue;
        coreWebView.NewWindowRequested -= subscriber;
    }

Relevant log output

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:24 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Flaflocommented, Jun 14, 2022

I updated my reproduction steps because they weren’t correct. You have to create a WinForms App and use BlazorWebView.

0reactions
Flaflocommented, Jun 23, 2022

Not sure what you mean by “part in JS”, are you saying your application JS code is manipulating the DOM?

The app is creating a blank popup with “about:blank” and dynamically injects DOM into it.

Regardless, if we do decide to integrate the URL loading strategy with the new window handler, then you can simply Cancel the default handling and it’ll be as if the handler wasn’t there in the first place. I believe this would achieve what you’re looking for.

Sounds good to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Block pop-ups in Microsoft Edge
How to block pop-ups in Microsoft Edge. In Edge, go to Settings and more at the top of your browser. An image showing...
Read more >
Disable Web Browser Pop-up Blockers
Disable Web Browser Pop-up Blockers. To function properly, KSIS requires your web browser be configured to allow pop-ups.
Read more >
Block or allow pop-ups in Chrome - Computer
On your computer, open Chrome. · Go to a page where pop-ups are blocked. · In the address bar, click Pop-up blocked Pop-up...
Read more >
How to Disable or Enable Your Pop-up Blocker
To disable the pop-up blocker, uncheck the Block pop-up windows box below Permissions. To allow specific pop-ups, click Exceptions and enter the URL(s)....
Read more >
How To Create an Icon Bar
Tip: Go to our CSS Navbar Tutorial to learn more about navigation bars. Tip: Go to our Icons Tutorial to learn more about...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found