提问人:Ivan Demchenko 提问时间:9/11/2021 最后编辑:Ivan Demchenko 更新时间:9/11/2021 访问量:3216
如何将扩展加载到 CefSharp 浏览器?
How to load extension to CefSharp Browser?
问:
我在将扩展加载到 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://*/*"
]
}
答: 暂无答案
评论