cefsharp重写默认js弹窗(alert/confirm/prompt)

 1.设置js弹窗控制器

webView.JsDialogHandler = this;  //js弹窗控制

this表示本类对象,所以本类要实现IJsDialogHandler接口

 

2.实现IJsDialogHandler接口接口方法

   public bool OnJSAlert(IWebBrowser browser, string url, string message)

        {

            MessageBox.Show(message);

            return true; //阻止js弹

        }



        public unsafe bool OnJSConfirm(IWebBrowser browser, string url, string message, bool* retval)

        {

            DialogResult result = MessageBox.Show(message, "提示", MessageBoxButtons.YesNo);

            bool value = result == DialogResult.Yes ? true : false;

            //   retval = (bool *)GCHandle.Alloc(value).AddrOfPinnedObject().ToPointer(); //获取托管内存地址,异常

            // retval = &value; //改变指针失败

            *retval = value;

            return true;

        }



        public unsafe bool OnJSPrompt(IWebBrowser browser, string url, string message, string defaultValue, bool* retval, ref string result)

        {

            //交互消息

            string r = string.Empty;

            this.Invoke(new Action(() =>

            {

                r = Microsoft.VisualBasic.Interaction.InputBox(message, "提示", defaultValue);

            }));

            result = r;



            if (result == defaultValue)

            {

                *retval = false;

            }

            else

            {

                *retval = true;

            }

            return true;

        }

 

你可能感兴趣的:(confirm)