首页 > 代码库 > cefsharp重写默认js弹窗(alert/confirm/prompt)

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

 1.设置js弹窗控制器

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

 

2.实现接口方法

   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 = http://www.mamicode.com/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;        }