wangzhibo
2023-10-31 86070cd00485c47a2c5c3ffa4a9bae6648dd05c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
 
namespace TestWebview
{
    public class CefCustomObject
    {
        // Declare a local instance of chromium and the main form in order to execute things from here in the main thread
        private static ChromiumWebBrowser _instanceBrowser = null;
        // The form class needs to be changed according to yours
        private static Form1 _instanceMainForm = null;
 
        public CefCustomObject(ChromiumWebBrowser originalBrowser, Form1 mainForm)
        {
            _instanceBrowser = originalBrowser;
            _instanceMainForm = mainForm;
        }
 
        /// <summary>
        /// 窗口加载完毕时需要出发的全局JS对象;
        /// </summary>
        public void OnFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            if (e.Frame.IsMain)
            {
                _instanceBrowser.ExecuteScriptAsync(@"
              document.body.onmouseup = function()
              {
                bound.onSelected(window.getSelection().toString());
              }
            ");
            }
        }
 
       
        /// <summary>
        /// 显示一个弹出对话框,前端JS调用的方法
        /// </summary>
        /// <param name="msg">传入参数</param>
        public void showAlertMsg(string msg)
        {
            MessageBox.Show("Msg From Web JS: [" + msg + "]");
        }
 
    }
}