mosheng 发表于 2026-2-4 18:00:43

【C#】5.0 获取默认事件的使用

本帖最后由 mosheng 于 2026-2-4 18:00 编辑

1、Program.cs中进行初始化

// 设置授权码
FBroSharpVIPGlobalControl.SetAuthorizationCode("你的授权码");

// 设置DPI感知模式
FBroSharpInitControl.SetProcessDPI(FBroSharpDPI.SYSTEM_AWARE);

// 确定缓存路径
string cachePath;
if (!string.IsNullOrEmpty(cmdArgs.ProfileId))
{
    cachePath = Path.Combine(Directory.GetCurrentDirectory(), "Profiles", cmdArgs.ProfileId);
}
else
{
    cachePath = Path.Combine(Directory.GetCurrentDirectory(), "CacheData");
}

// 确保缓存目录存在
if (!Directory.Exists(cachePath))
{
    Directory.CreateDirectory(cachePath);
}

WriteLog($"缓存路径: {cachePath}");

FBroSharpInitSet set = new FBroSharpInitSet
{
    // 设置缓存目录
    cache_path = cachePath,

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

    // 设置执行子进程
    browser_subprocess_path = Path.Combine(Directory.GetCurrentDirectory(), "FBroSubprocess.exe"),

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

    // 是否启用持久化cookie
    persist_session_cookies = true,

    // 日志模式
    log_severity = FBroSharpLogSeverity.ERROR
};

// 创建初始化事件,传递 config 以便 GetDefaultClient 能为新标签设置默认事件,mdArgs, wsClient, cmdArgs.Config这三个是我自己的需要的参数
InitEvent initEvent = new InitEvent(cmdArgs, wsClient, cmdArgs.Config);

// 执行初始化
if (!FBroSharpInitControl.InitPro(set, initEvent))
{
    Console.WriteLine("FBroSharpInitControl.InitPro 返回 false");
    return false;
}

WriteLog("FBroSharp 初始化成功");
return true;


static/image/hrline/4.gif


2、在InitEvent.cs中的InitEvent
namespace fbro
{
    public class InitEvent : FBroInitEvent
    {
      private readonly CommandLineArgs cmdArgs;
      private readonly WebSocketClient wsClient;
      private readonly FbroConfig config;

      public InitEvent(CommandLineArgs args, WebSocketClient wsClient = null, FbroConfig config = null)
      {
            this.cmdArgs = args;
            this.wsClient = wsClient;
            this.config = config;
      }


      /// <summary>
      /// 获取默认事件 - 谷歌模式下点击+号创建新标签时触发
      /// 在这里设置默认的BrowserEvent,确保新标签也能应用指纹配置
      /// </summary>
      public override void GetDefaultClient(IFBroSharpBrowser browser, string url, IFBroSharpUseExtraData user_settings)
      {
            Console.WriteLine($"GetDefaultClient: 创建新标签, URL={url}");

            // 使用实例配置或全局配置
            var configToUse = this.config ?? GlobalConfig.Config;
            var wsClientToUse = this.wsClient ?? GlobalConfig.WsClient;

            // 创建带有指纹配置的 BrowserEvent
            BrowserEvent browserEvent = new BrowserEvent(wsClientToUse, configToUse);

            // 设置默认事件
            user_settings.SetEvent(browserEvent, default);

            Console.WriteLine($"GetDefaultClient: 已设置默认事件, config={(configToUse != null ? "有效" : "null")}");
      }
}


static/image/hrline/4.gif


如此设置在谷歌原生UI中点击 + 号创建的新标签浏览器可以继承自己自定义的浏览器事件了


页: [1]
查看完整版本: 【C#】5.0 获取默认事件的使用