FBrowserCEF3Lib浏览器开发技术交流网址

 找回密码
 立即注册
查看: 1546|回复: 1

C#自定义扩展插件事件,踩坑记录

[复制链接]

签到天数: 695 天

[LV.9]以坛为家II

294

主题

606

回帖

1万

积分

超级版主

皇冠皇冠皇冠太阳

积分
17933

活跃会员热心会员永久VIP会员推广达人宣传达人突出贡献优秀版主荣誉管理论坛元老

QQ
发表于 2025-4-10 16:09:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 mosheng 于 2025-4-10 17:24 编辑

注意GetDefaultTabBrowser这里,如果是单窗口浏览器就很好处理,返回默认的前台浏览器即可
如果是创建了多个窗口的浏览器,就需要判断是哪个浏览器打开的浏览器插件,目前我的解决方法是字典存储插件浏览器与主浏览器的关联关系 (浏览器ID -> 主浏览器ID)
如果独立缓存的浏览器加载插件,独立缓存采用的是独立环境,所以独立的环境就要用独立的环境加载插件而不能用全局的

[C#] 查看源码 复制代码
using FBroSharp;
using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp.Event;
using FBroSharp.Lib;
using FBroSharp.Value;
using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace BaseTest
{
    //public class ExtensionHandler : FBroSharpExtensionHandler
    //{
    //    // 基本构造函数
    //    public ExtensionHandler() : base()
    //    {
    //        Console.WriteLine("创建扩展处理器");
    //    }
    //}
    public class ExtensionHandler : FBroSharpExtensionHandler
    {
        //
        // 摘要:
        //     扩展插件_加载错误信息
        public override void OnExtensionLoadFailedMessage(string error, string path)
        {
            Console.WriteLine("扩展插件加载失败:" + error + ",路径:" + path);
        }

        //
        // 摘要:
        //     扩展插件_加载失败
        //
        //     Called if the CefRequestContext::LoadExtension request fails. |result| will be
        //     the error code.
        public override void OnExtensionLoadFailed(int result)
        {
            Console.WriteLine("扩展插件加载失败:" + result);
        }

        //
        // 摘要:
        //     扩展插件_加载成功
        //
        //     Called if the CefRequestContext::LoadExtension request succeeds. |extension|
        //     is the loaded extension.
        public override void OnExtensionLoaded(IFBroSharpExtension extension)
        {
            string extensionId = extension.GetIdentifier();
            string extensionPath = extension.GetPath();
            Console.WriteLine("扩展插件加载成功:" + extensionId + ", " + extensionPath);

            // 存储扩展ID到Form1的静态字段中
            定制软件.Form1.ExtensionId = extensionId;
            
            // 根据插件路径判断是哪个插件
            string lowerPath = extensionPath.ToLower();
            if (lowerPath.Contains("douyin"))
            {
                定制软件.Form1.DouyinExtensionId = extensionId;
                Console.WriteLine("抖音插件ID已保存: " + extensionId);
            }
            else if (lowerPath.Contains("xhs"))
            {
                定制软件.Form1.XhsExtensionId = extensionId;
                Console.WriteLine("小红书插件ID已保存: " + extensionId);
            }
        }

        //
        // 摘要:
        //     扩展插件_卸载
        //
        //     Called after the CefExtension::Unload request has completed.
        public override void OnExtensionUnloaded(IFBroSharpExtension extension)
        {
            Console.WriteLine("扩展插件卸载:" + extension.GetIdentifier(), extension.GetPath());
        }

        //
        // 摘要:
        //     扩展插件_即将加载Background浏览器
        //
        //     Called when an extension needs a browser to host a background script specified
        //     via the "background" manifest key. The browser will have no visible window and
        //     cannot be displayed. |extension| is the extension that is loading the background
        //     script. |url| is an internally generated reference to an HTML page that will
        //     be used to load the background script via a "<script>" src attribute. To allow
        //     creation of the browser optionally modify |client| and |settings| and return
        //     false. To cancel creation of the browser (and consequently cancel load of the
        //     background script) return true. Successful creation will be indicated by a call
        //     to CefLifeSpanHandler::OnAfterCreated, and CefBrowserHost::IsBackgroundHost will
        //     return true for the resulting browser. See https://developer.chrome.com/extensions/event_pages
        //     for more information about extension background script usage.
        public override bool OnBeforeBackgroundBrowser(IFBroSharpExtension extension, string url, IFBroSharpUseExtraData user_settings, ref FBroSharpBrowserSetting settings)
        {
            Console.WriteLine("扩展插件即将加载Background浏览器:" + extension.GetIdentifier(), extension.GetPath());
            return false;
        }

        //
        // 摘要:
        //     扩展插件_即将加载浏览器
        //
        //     Called when an extension API (e.g. chrome.tabs.create) requests creation of a
        //     new browser. |extension| and |browser| are the source of the API call. |active_browser|
        //     may optionally be specified via the windowId property or returned via the GetActiveBrowser()
        //     callback and provides the default |client| and |settings| values for the new
        //     browser. |index| is the position value optionally specified via the index property.
        //     |url| is the URL that will be loaded in the browser. |active| is true if the
        //     new browser should be active when opened. To allow creation of the browser optionally
        //     modify |windowInfo|, |client| and |settings| and return false. To cancel creation
        //     of the browser return true. Successful creation will be indicated by a call to
        //     CefLifeSpanHandler::OnAfterCreated. Any modifications to |windowInfo| will be
        //     ignored if |active_browser| is wrapped in a CefBrowserView.
        public override bool OnBeforeBrowser(IFBroSharpExtension extension, IFBroSharpBrowser browser, IFBroSharpBrowser active_browser, int index, string url, bool active, out FBroSharpWindowsInfo windowInfo, IFBroSharpUseExtraData user_settings, ref FBroSharpBrowserSetting settings)
        {
            Console.WriteLine("扩展插件即将加载浏览器:" + extension.GetIdentifier(), extension.GetPath());
            windowInfo = default(FBroSharpWindowsInfo);
            return false;
        }

        //
        // 摘要:
        //     扩展插件_获取执行浏览器
        //
        //     Called when no tabId is specified to an extension API call that accepts a tabId
        //     parameter (e.g. chrome.tabs.*). |extension| and |browser| are the source of the
        //     API call. Return the browser that will be acted on by the API call or return
        //     NULL to act on |browser|. The returned browser must share the same CefRequestContext
        //     as |browser|. Incognito browsers should not be considered unless the source extension
        //     has incognito access enabled, in which case |include_incognito| will be true.
        public override IFBroSharpBrowser GetActiveBrowser(IFBroSharpExtension extension, IFBroSharpBrowser browser, bool include_incognito)
        {
            Console.WriteLine("扩展插件获取执行浏览器:" + extension.GetIdentifier(), extension.GetPath());
            return null;
        }

        //
        // 摘要:
        //     扩展插件_获取当前标签默认浏览器
        //自定义添加事件,为了兼容tab获取当前默认tab,通过此事件将当前默认浏览器返回给插件,一般用于插件chrome.tab.reload执行的目标,
        //因为cef是没有tab概念无法直接获取默认浏览器所有通过此事件由自己控制返回默认浏览器,
        //如不返回将使用插件本身浏览器作为默认浏览器
        //     返回默认tab浏览器
        public override IFBroSharpBrowser GetDefaultTabBrowser(IFBroSharpExtension extension, IFBroSharpBrowser browser)
        {
            // 获取插件浏览器ID
            int pluginBrowserId = browser.GetIdentifier();
            
            // 尝试查找关联的主浏览器ID
            if (BaseTest.BrowserDis.PluginToMainBrowserMap.TryGetValue(pluginBrowserId, out int mainBrowserId))
            {
                Console.WriteLine($"找到关联的主浏览器: 插件浏览器ID={pluginBrowserId}, 主浏览器ID={mainBrowserId}");
                
                // 查找对应ID的主浏览器实例
                foreach (var mainBrowser in BaseTest.BrowserList.data)
                {
                    if (mainBrowser.GetIdentifier() == mainBrowserId)
                    {
                        Console.WriteLine("成功获取主浏览器实例");
                        return mainBrowser;
                    }
                }
            }
            
            Console.WriteLine($"未找到关联的主浏览器: 插件浏览器ID={pluginBrowserId}");
            return null;
        }

        //
        // 摘要:
        //     扩展插件_获取扩展资源
        //
        //     Called when the tabId associated with |target_browser| is specified to an extension
        //     API call that accepts a tabId parameter (e.g. chrome.tabs.*). |extension| and
        //     |browser| are the source of the API call. Return true to allow access of false
        //     to deny access. Access to incognito browsers should not be allowed unless the
        //     source extension has incognito access enabled, in which case |include_incognito|
        //     will be true.
        public override bool CanAccessBrowser(IFBroSharpExtension extension, IFBroSharpBrowser browser, bool include_incognito, IFBroSharpBrowser target_browser)
        {
            Console.WriteLine("扩展插件获取扩展资源:" + extension.GetIdentifier(), extension.GetPath());
            return false;
        }

        //
        // 摘要:
        //     扩展插件_获取扩展资源
        //
        //     Called to retrieve an extension resource that would normally be loaded from disk
        //     (e.g. if a file parameter is specified to chrome.tabs.executeScript). |extension|
        //     and |browser| are the source of the resource request. |file| is the requested
        //     relative file path. To handle the resource request return true and execute |callback|
        //     either synchronously or asynchronously. For the default behavior which reads
        //     the resource from the extension directory on disk return false. Localization
        //     substitutions will not be applied to resources handled via this method.
        public override bool GetExtensionResource(IFBroSharpExtension extension, IFBroSharpBrowser browser, string file)
        {
            Console.WriteLine("扩展插件获取扩展资源:" + extension.GetIdentifier(), extension.GetPath());
            return false;
        }
    }
} 

签到天数: 183 天

[LV.7]常住居民III

24

主题

201

回帖

1748

积分

中级会员LV5

皇冠星星

积分
1748
QQ
发表于 2026-2-16 21:02:11 | 显示全部楼层
这么细节。你太赞了。!!!
QQ-6145203
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们 在线手册 视频教程 赞助支持 CEF官网 易语言官网 火山开发官网
拒绝任何人以任何形式在本网站发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表本网站立场!

网址帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!

Archiver| 手机版| 小黑屋| 联系QQ:283987887 劰载中...

Powered by Discuz! X5.0 GMT+8, 2026-9-17 06:59 , Processed in 0.021229 second(s), 11 queries , Redis On.

 川公网安备51090302000262 蜀ICP备2023034329号
快速回复 返回顶部 返回列表