如何将扩展加载到 CefSharp 浏览器?

How to load extension to CefSharp Browser?

提问人:Ivan Demchenko 提问时间:9/11/2021 最后编辑:Ivan Demchenko 更新时间:9/11/2021 访问量:3216

问:

我在将扩展加载到 CefSharp 时遇到问题。

扩展似乎已加载,我在 OnExtensionLoaded() 方法中捕获了断点,但屏幕上没有任何显示,没有警报,也没有控制台日志,我在扩展中做了。

在Google Chrome扩展程序中正常工作,警报和console.log工作正常。

C#

CEF 初始化

var settings = new CefSettings
{
    RootCachePath = PathSettings.RootCachePath,
    PersistSessionCookies = true,
    PersistUserPreferences = true,
    BackgroundColor = 255,
    CachePath = PathSettings.RootCachePath,
    CommandLineArgsDisabled = false,
    IgnoreCertificateErrors = true,
    RemoteDebuggingPort = 8080,
};

settings.CefCommandLineArgs.Add("enable-extension-activity-logging");
settings.CefCommandLineArgs.Add("extensions-on-chrome-urls");
settings.CefCommandLineArgs.Add("extensions-not-webstore");
settings.CefCommandLineArgs.Add("show-component-extension-options");
settings.CefCommandLineArgs.Add("disable-web-security");
settings.CefCommandLineArgs.Add("enable-media-stream");
settings.CefCommandLineArgs.Add("allow-running-insecure-content");

Cef.Initialize(settings, true);

扩展处理程序 .cs

public class ExtensionHandler : IExtensionHandler
{
    public bool CanAccessBrowser(IExtension extension, IBrowser browser, bool includeIncognito, IBrowser targetBrowser)
    {
        return true;
    }

    public void Dispose()
    {
    }

    public IBrowser GetActiveBrowser(IExtension extension, IBrowser browser, bool includeIncognito)
    {
        return browser;
    }

    public bool GetExtensionResource(IExtension extension, IBrowser browser, string file, IGetExtensionResourceCallback callback)
    {
        return true;
    }

    public bool OnBeforeBackgroundBrowser(IExtension extension, string url, IBrowserSettings settings)
    {
        
        return true;
    }

    public bool OnBeforeBrowser(IExtension extension, IBrowser browser, IBrowser activeBrowser, int index, string url, bool active, IWindowInfo windowInfo, IBrowserSettings settings)
    {
        browser.ShowDevTools();
        return true;
    }

    public void OnExtensionLoaded(IExtension extension)
    {
    }

    public void OnExtensionLoadFailed(CefErrorCode errorCode)
    {
    }

    public void OnExtensionUnloaded(IExtension extension)
    {
    }
}

浏览器初始化

Browser.RequestContext = new RequestContext(new RequestContextSettings
    {
        CachePath = cachePath,
        PersistSessionCookies = true,
        PersistUserPreferences = true,
        IgnoreCertificateErrors = true,
        AcceptLanguageList = "en"
    });

Browser.RequestContext.LoadExtensionFromDirectory(path, new ExtensionHandler());

外延

背景:.js

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
      chrome.tabs.executeScript({
        code: 'console.log("console.log from extension");'
      });
      chrome.tabs.executeScript({
        code: 'alert("alert from extension");'
      });
    });

manifest.json

{
  "name": "BrowserExtension",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "Description ...",
  "background": {
    "scripts": [ "background.js" ]
  },
  "permissions": [
    "tabs",
    "background",
    "http://*/*",
    "https://*/*"
  ]
}
C# Google-chrome-扩展 Chromium Cefsharp Chromium-embedded

评论

0赞 amaitland 9/12/2021
扩展支持非常有限,您的扩展可能使用了尚未在 CEF 中实现的 API。内存不支持选项卡。请详细阅读 github.com/cefsharp/CefSharp/issues/2497#issue-354965106
0赞 Vladyslav Lubenskyi 4/18/2023
CEF 最近推出了一个新的 Chrome 运行时,CefSharp 开始着手采用它。github.com/cefsharp/CefSharp/discussions/4123我们 DotNetBrowser 最近发布了一个具有扩展支持的预览版本。一探究竟: dev.to/dotnetbrowser/...

答: 暂无答案