Skip to content

JS交互使用方法:

在应用程序的主入口点mian()中注册JS交互

using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using BaseTest;

namespace _034JS交互
{
    static class Program
    {

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            FBroSharpInitSet set = new FBroSharpInitSet
            {
                //设置缓存目录
                cache_path = Directory.GetCurrentDirectory() + "\\Cache\\CachePath\\",

                //设置用户目录
                user_data_path = Directory.GetCurrentDirectory() + "\\Cache\\UserPath\\",

                //设置根缓存目录,必须是上面缓存目录的父目录,否则采用独立缓存可能会失效
                root_cache_path = Directory.GetCurrentDirectory() + "\\Cache\\",

                //启用事件消息循环
                multi_threaded_message_loop = true,

                //设置执行子进程,不设置他会默认以当前进程作为子进程
                browser_subprocess_path = Directory.GetCurrentDirectory() + "\\FBroSubprocess.exe",

                //本地化语言
                locale = "zh-CN",

                //US
                //user_agent = "Mozilla / 5.0(iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit / 605.1.15(KHTML, like Gecko) Version / 16.6 Mobile / 15E148 Safari / 604.1",

                //日志模式,只有出现错误的时候才记录日志
                log_severity = FBroSharpLogSeverity.ERROR
            };

            //创建初始化事件
            InitEvent init_event = new InitEvent();

            //注册JS交互
            FBroSharpInitControl.QueryFunctions("cefQuery", "cefQueryCancel", new FBroQueryHandlerDis(0));
            FBroSharpInitControl.QueryFunctions("cefQuerytest", "cefQueryCanceltest", new FBroQueryHandlerDis(1));

            //执行初始化
            if (!FBroSharpInitControl.InitPro(set, init_event))
                return;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());


            //关闭框架释放资源
            FBroSharpInitControl.Shutdown(false);

        }
    }
}

FBroQueryHandlerDis的定义

using FBroSharp.Callback;
using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp.Event;
using FBroSharp.Lib;
using FBroSharp.Value;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace BaseTest
{
    public class FBroQueryHandlerDis : FBroSharpQueryHandler
    {
        public int type_;
        public FBroQueryHandlerDis(int type)
        {
            type_ = type;
        }

        public override bool OnQuery(IFBroSharpBrowser browser, IFBroSharpFrame frame, long query_id, string request, bool persistent, IFBroSharpQueryCallback callback)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name + type_ + query_id + request);

            //switch (type_)
            //{
            //    case 0:
            //        callback.Success("执行成功" + request);
            //        break;
            //    case 1:
            //        callback.Failure(111, "执行失败" + request);
            //        break;
            //}
            callback.Success("执行成功" + request);
            return true;
        }

        public override void OnQueryCanceled(IFBroSharpBrowser browser, IFBroSharpFrame frame, long query_id)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name + query_id);

        }
    }

}

如果文档对您有帮助,欢迎 请喝咖啡 ☕ | 软件发布 | 源码购买