How to disable JavaScript using WebView2 when a button is clicked

john zyd 421 Reputation points
2021-01-30T14:44:47.12+00:00

Hello:
In my project, I have to login to one web page, and after login, I want to disable JavaScript, as it has some JavaScript to logout automatically after 30 minutes, no mater what you are doing in the page, even if you are trying to click on some buttons, but it logs you out and you have to login again by typing user name and password.
So I want to add one button to disable the JavaScript on the page after I login, but login take some time, as it may involve resolving some Captcha challenges, so I can click the button after I have successfully logged in.
I created one Windows Form App (.NET) for target .Net 5.0; install WebView2 nuget package:
Install-Package Microsoft.Web.WebView2 -Version 1.0.705.50
I have the following code: (IDE: VS2019 Version 16.8.4)

using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WebView2Buttons
{
    public partial class Form1 : Form
    {
        private readonly WebView2 BingWebView = new WebView2();
        private readonly Button Button1 = new Button();

        private async void Button1_Click(object sender, EventArgs e)
        {
// How I can set CoreWebView2Settings.IsScriptEnabled to false?
        }

        public Form1()
        {
            InitializeComponent();
            BingWebView.CreationProperties = null;
            BingWebView.Location = new Point(10, 100);
            BingWebView.Name = "BingWebView";
            BingWebView.Size = new Size(1700, 700);
            BingWebView.Source = new Uri("https://www.bing.com/", UriKind.Absolute);
            BingWebView.TabIndex = 0;
            BingWebView.ZoomFactor = 1D;
            BingWebView.Visible = true;
            Controls.Add(BingWebView);
            this.SuspendLayout();
            ClientSize = new Size(1800, 800);

            Button1.Location = new Point(50, 10);
            Button1.Name = "Button1";
            Button1.Size = new Size(250, 50);
            Button1.TabIndex = 2;
            Button1.Text = "Button1";
            Button1.UseVisualStyleBackColor = true;
            Button1.Click += new EventHandler(Button1_Click);
            Controls.Add(Button1);

            Name = "Form1";
            Text = "Form1";
            ResumeLayout(false);
        }

        private async void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Please advice!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,129 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-02-01T06:34:11.837+00:00

    Do you mean this?

       BingWebView.CoreWebView2.Settings.IsScriptEnabled = false;  
    

    It should be noted that, according to the introduction in the document, setting changes made after NavigationStarting event do not apply until the next top-level navigation.

    So you may need to call BingWebView.CoreWebView2.Navigate("") after setting it.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.