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

Failed to disable new tab/window popup on Ctrl/Shift + Click, Ctrl/Shift + Enter or Mid wheel click by link/button #2652

Closed
csauko opened this issue Aug 4, 2022 · 5 comments
Assignees
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@csauko
Copy link

csauko commented Aug 4, 2022

Version: 1.0.1264.42
We use web login form in our WPF app and we need to prevent browser to open new tab/window on Ctrl/Shift + Click, Ctrl/Shift + Enter or Mid wheel click by link/button.
We failed to disable that behavior with CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false
We tried to redirect new window request to current window with CoreWebView2.NewWindowRequested += (s, a) => a.NewWindow = (CoreWebView2)s;, but we got different behavior on form submit button:

  • on submit with Ctrl pressed opens new window and we can see form post request in the fiddler as expected
  • on submit with Ctrl pressed with redirection to current window we can see only get request (refresh), but no form post.

AB#41353848

@Danielx64
Copy link

Winforms or wpf? I haven't been able to reproduce this unless I am trying it on the wrong sort of page

@csauko
Copy link
Author

csauko commented Aug 5, 2022

It's WPF.

    public class LoginWindow : Window
    {
        public string Url { get; set; }

        public LoginWindow()
        {
            InitializeComponent();

            webView.NavigationStarting += OnNavigationStarting;
            webView.NavigationCompleted += OnNavigationCompleted;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (webView.CoreWebView2 is not null) {
                webView.CoreWebView2.NewWindowRequested -= OnNewWindowRequested;
            }
            webView.NavigationStarting -= OnNavigationStarting;
            webView.NavigationCompleted -= OnNavigationCompleted;
            webView.Dispose();
        }

        private void OnNewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
            => e.NewWindow = (CoreWebView2)sender; // reuse current view
        private void OnNavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
            => UrlChanging?.Invoke(this, new (e));
        private void OnNavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
            => UrlChanged?.Invoke(this, e);

        protected override async void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);

            try
            {
                var webView2Environment = await CoreWebView2Environment.CreateAsync(webView2Dir, AppFoldersManager.GetCustomArtifactsFolder());

                await webView.EnsureCoreWebView2Async(webView2Environment);

                webView.CoreWebView2.NewWindowRequested += OnNewWindowRequested;
                webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
                webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
                webView.CoreWebView2.Settings.AreDevToolsEnabled = false;
                webView.CoreWebView2.Settings.IsStatusBarEnabled = false;

                webView.CoreWebView2.Navigate(Url);
            }
            catch (Exception exc)
            {
                ...
            }
        }

Below is a sketch of form script we use (clicking by 'Sign in' with holding Ctrl/Shift will go different way if we redirect new window request to the current window or let it run in the new one):

 <form id="identity-form" action="" method="post">
    <input type="hidden" id="ReturnUrl" name="ReturnUrl" value="/connect/authorize/callback?response_type=code&amp;state=...&amp;code_challenge=...&amp;code_challenge_method=...&amp;client_id=...&amp;scope=openid%...&amp;redirect_uri=http%3A%2F%2F127.0.0.1%2Fsignin-oidc&amp;clientVersion=1.0.0.0&amp;deviceId=...">
    <div id="login-form-fields-container">
        <div class="form-field mb-0">
            <label class="form-field__label" for="Input_Username">Username</label>
            <input id="username-input" class="form-field__input valid" value=... spellcheck="false" autocomplete="off" autofocus="" type="text" data-val="true" data-val-required="Required field" name="Input.Username" aria-describedby="username-input-error" aria-invalid="false">
        </div>

        <div class="form-field mb-0">
            <label class="form-field__label" for="Input_Password">Password</label>
            <div class="password-input js-password-input">
                <input type="password" class="form-field__input form-field__input--password" autocomplete="off" data-val="true" data-val-required="Required field" id="Input_Password" name="Input.Password">
            </div>
        </div>
    </div>

    <div class="form__actions">
        <button type="submit" id="identity-form-submit" class="button" name="button" value="login" disabled="">Sign in</button>
    </div>
</form>

@Danielx64
Copy link

Danielx64 commented Aug 5, 2022

Ok I retest on my end and indeed I can reproduce this and also try control A to select all and control C to copy....

@yildirimcagri-msft yildirimcagri-msft added bug Something isn't working tracked We are tracking this work internally. labels Sep 16, 2022
@yildirimcagri-msft
Copy link
Member

Hi, sorry for the delay, this has fallen off my radar while I was on vacation. Unfortunately, setting the opener WebView as NewWindow may cause some unexpected behavior as internally what is happening is the opener WebView's content is closed and replaced with the opened content. I added this to our bug backlog to investigate this behavior. In the meantime, have you tried handling the Ctrl presses via AcceleratorKeyPressed event to prevent it being processed via WebView2?

@champnic
Copy link
Member

This is fixed in runtimes 109.0.1496.0+. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

4 participants