首页 > 其他分享 >WPF WebBrowser navigate to website via url and escape script error warning

WPF WebBrowser navigate to website via url and escape script error warning

时间:2024-07-12 12:18:56浏览次数:7  
标签:website via script Windows System webBrowser Navigating using WebBrowser

Copy from https://www.iditect.com/faq/csharp/wpf-webbrowser-control--how-to-suppress-script-errors.html#:~:text=To%20suppress%20these%20script%20errors%2C%20you%20can%20handle,using%20the%20Cancel%20property%20of%20the%20WebBrowserNavigatingEventArgs%20parameter.

 

 

 

 

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp207
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            webBrowser.Navigating += WebBrowser_Navigating;
        }

        private void WebBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            var webBrowser = (WebBrowser)sender;
            webBrowser.Navigating -= WebBrowser_Navigating; // Unsubscribe temporarily to avoid recursion

            try
            {
                // Execute JavaScript to suppress script errors
                dynamic activeX = webBrowser.GetType().InvokeMember("ActiveXInstance",
                    System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic,
                    null, webBrowser, new object[] { });

                activeX.Silent = true;
            }
            catch (Exception ex)
            {
                // Handle any exception that may occur while trying to suppress script errors
                // Optionally, log the error for debugging purposes
                System.Diagnostics.Debug.WriteLine("Error suppressing script errors: " + ex.Message);
            }
            finally
            {
                webBrowser.Navigating += WebBrowser_Navigating; // Re-subscribe to the event
            }
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string url = "https://www.spacex.com/vehicles/starship/";
            webBrowser.Navigate(url); 
        }
    }
}

 

标签:website,via,script,Windows,System,webBrowser,Navigating,using,WebBrowser
From: https://www.cnblogs.com/Fred1987/p/18298088

相关文章

  • WPF display and host pdf via WebBrowser
    //xaml<Windowx:Class="WpfApp206.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • JavaScript 进阶(五)---forEach/map/filterevery/some/includes/reduce的详细用法
    目录1.forEach2.map3.filter4.for...in5.for...of6.every7.some8.includes9.reduce举个例子:使用fliter:使用 map 来筛选并转换数组使用 forEach 来筛选并构建数组总结1.forEach-详解:`forEach`方法对数组的每个元素执行一次提供的函数。这个方......
  • 不用JavaScript实现鼠标移入判断示例
    要点利用了伪元素生成了4个三角形组成了一个正方形,通过hover哪个透明的三角形来判断用户的操作方位。具体实现HTML:<divclass="box"><divclass="box__right">Right→Left</div><divclass="box__left">Left→Right</div><divclas......
  • WPF customize DelegateCommand via implementation interface System.Windows.Input.
    publicclassDelCmd:ICommand{privatereadonlyAction<Object>execute;privatereadonlyPredicate<Object>canExecute;publicDelCmd(Action<object>executeValue,Predicate<object>canExecuteValue){execut......
  • JavaScript简易ATM机功能
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</title>......
  • TypeScript中的交叉类型
            交叉类型:将多个类型合并为一个类型,使用&符号连接。typeAProps={a:string}typeBProps={b:number}typeallProps=AProps&BPropsconstInfo:allProps={a:'小月月',b:7}        我们......
  • JavaScript如何将字符串形式里的img图片添加属性
    方法一:使用正则表达式以下例子中将会在img图片中添加crossorigin属性lethtml=`<p>打撒抠脚大汉噶刷卡机很大凯撒</p><p>规范化是的冯绍峰东风浩荡试试</p><imgsrc="http://s3.v.360xkw.com/yzb/photos/1688536327316_ca0e2e3d.jpg"alt=""......
  • TypeScript的类型谓词与控制流分析
    目录ts封装类型判断的问题类型谓词TypeScript的“控制流分析”ts封装类型判断的问题在union.d.ts中全局声明一个DataTypedeclaretypeDataType=|"RegExp"|"Object"|"Array"|"Function"|"String"|"Boolean"|"......
  • JavaScript复习记录(2)— 浅拷贝&深拷贝
    1、前情概要1.1、基本数据类型    Number、String、Boolean、Null、Undefined、Symbol、BigInt。基本数据类型是直接存储在栈中的数据。1.2、引用数据类型    Object、Array、Function、Date、RegExp、Map、Set、WeekMap、WeekSet、Promise、Error、Buffe......
  • [NodeJS] JavaScript模块化
    JavaScript诞生于1995年,一开始只是用于编写简单的脚本。随着前端开发任务越来越复杂,JavaScript代码也越来越复杂,全局变量冲突、依赖管理混乱等问题变得十分突出,模块化成为一个必不可少的功能。模块化发展史与方案对比YUI与JQuery2006年,雅虎开源了组件库YUILibrary,使用类似......