CEF Debug 增加 开发工具

删除 From 消息
This commit is contained in:
chfei committed 2026-08-21 14:14:47 +08:00
1 parent 772ee833c8
commit 5456e6611d
1 file changed
+226 -214
+226 -214
View File
@@ -16,244 +16,256 @@ using System.Windows.Forms;
namespace Work._29Tools.CefTools
{
public class LocalMenuHandler : IContextMenuHandler
{
public List<CefContextMenu> ContextMenuList;
public Func<List<Cookie>, TData> SaveCookie;
public List<Cookie> CookieList;
public class LocalMenuHandler : IContextMenuHandler
{
public List<CefContextMenu> ContextMenuList;
public Func<List<Cookie>, TData> SaveCookie;
public List<Cookie> CookieList;
void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
model.Clear();
bool hasCopyPaste = false;
model.Clear();
bool hasCopyPaste = false;
if ((parameters.EditStateFlags & ContextMenuEditState.CanCopy).ParseToInt() != 0)
{
model.AddItem(CefMenuCommand.Copy, "复制");
hasCopyPaste = true;
}
if ((parameters.EditStateFlags & ContextMenuEditState.CanPaste).ParseToInt() != 0)
{
model.AddItem(CefMenuCommand.Paste, "粘贴");
hasCopyPaste = true;
}
if (hasCopyPaste)
{
model.AddSeparator();
}
if ((parameters.EditStateFlags & ContextMenuEditState.CanCopy).ParseToInt() != 0)
{
model.AddItem(CefMenuCommand.Copy, "复制");
hasCopyPaste = true;
}
if ((parameters.EditStateFlags & ContextMenuEditState.CanPaste).ParseToInt() != 0)
{
model.AddItem(CefMenuCommand.Paste, "粘贴");
hasCopyPaste = true;
}
if (hasCopyPaste)
{
model.AddSeparator();
}
model.AddItem(CefMenuCommand.Back, "后退");
model.AddItem(CefMenuCommand.Forward, "前进");
model.AddItem(CefMenuCommand.ReloadNoCache, "刷新");
model.AddSeparator();
model.AddItem(CefMenuCommand.Back, "后退");
model.AddItem(CefMenuCommand.Forward, "前进");
model.AddItem(CefMenuCommand.ReloadNoCache, "刷新");
model.AddSeparator();
model.AddItem(CefMenuCommand.ViewSource, "查看页面源代码");
model.AddSeparator();
model.AddItem(CefMenuCommand.ViewSource, "查看页面源代码");
model.AddSeparator();
model.AddItem((CefMenuCommand)26500, "复制页面地址");
model.AddItem((CefMenuCommand)26500, "复制页面地址");
if (parameters.MediaType == ContextMenuMediaType.Image)
{
model.AddSeparator();
model.AddItem((CefMenuCommand)26600, "复制图片地址");
model.AddItem((CefMenuCommand)26601, "保存图片");
if (parameters.MediaType == ContextMenuMediaType.Image)
{
model.AddSeparator();
model.AddItem((CefMenuCommand)26600, "复制图片地址");
model.AddItem((CefMenuCommand)26601, "保存图片");
if (NaiBaGlobalConfig.IsNewFeature)
{
model.AddSeparator();
model.AddItem((CefMenuCommand)26700, "识别图片文字");
}
}
if ((parameters.EditStateFlags & ContextMenuEditState.CanTranslate).ParseToInt() != 0)
{
model.AddSeparator();
model.AddItem((CefMenuCommand)26800, "翻译网页");
}
if (NaiBaGlobalConfig.IsNewFeature)
{
model.AddSeparator();
model.AddItem((CefMenuCommand)26700, "识别图片文字");
}
}
if ((parameters.EditStateFlags & ContextMenuEditState.CanTranslate).ParseToInt() != 0)
{
model.AddSeparator();
model.AddItem((CefMenuCommand)26800, "翻译网页");
}
if (SaveCookie != null)
{
model.AddSeparator();
model.AddItem((CefMenuCommand)26900, "保存 Cookie");
}
if (SaveCookie != null)
{
model.AddSeparator();
model.AddItem((CefMenuCommand)26900, "保存 Cookie");
}
if (ContextMenuList != null && ContextMenuList.Any())
{
model.AddSeparator();
if (ContextMenuList.Where(p => p.CommandCode < 28000 || p.CommandCode > 28500).Any())
{
BaseMsgTipFormHelper.ShowError("Cef右键菜单,无效的 CommandCode,CommandCode 的范围 28000-28500");
return;
}
foreach (var menu in ContextMenuList)
{
model.AddItem((CefMenuCommand)menu.CommandCode, menu.CommandText);
}
}
}
if (ContextMenuList != null && ContextMenuList.Any())
{
model.AddSeparator();
if (ContextMenuList.Where(p => p.CommandCode < 28000 || p.CommandCode > 28500).Any())
{
BaseMsgTipFormHelper.ShowError("Cef右键菜单,无效的 CommandCode,CommandCode 的范围 28000-28500");
return;
}
foreach (var menu in ContextMenuList)
{
model.AddItem((CefMenuCommand)menu.CommandCode, menu.CommandText);
}
}
#if DEBUG
model.AddSeparator();
model.AddItem((CefMenuCommand)99990, "开发者工具");
#endif
}
public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
if (commandId == (CefMenuCommand)26500)
{
string url = parameters.PageUrl;
if (!string.IsNullOrEmpty(url))
{
Clipboard.SetText(url);
BaseMsgTipFormHelper.ShowInfo("复制页面地址到剪贴板成功");
}
else
{
BaseMsgTipFormHelper.ShowError("复制页面地址到剪贴板失败");
}
return true;
}
public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
if (commandId == (CefMenuCommand)26500)
{
string url = parameters.PageUrl;
if (!string.IsNullOrEmpty(url))
{
Clipboard.SetText(url);
//BaseMsgTipFormHelper.ShowInfo("复制页面地址到剪贴板成功");
}
else
{
//BaseMsgTipFormHelper.ShowError("复制页面地址到剪贴板失败");
}
return true;
}
if (commandId == (CefMenuCommand)26600)
{
string url = parameters.SourceUrl;
if (!string.IsNullOrEmpty(url))
{
Clipboard.SetText(url);
BaseMsgTipFormHelper.ShowInfo("复制图片地址到剪贴板成功");
}
else
{
BaseMsgTipFormHelper.ShowError("复制图片地址到剪贴板失败");
}
return true;
}
if (commandId == (CefMenuCommand)26600)
{
string url = parameters.SourceUrl;
if (!string.IsNullOrEmpty(url))
{
Clipboard.SetText(url);
//BaseMsgTipFormHelper.ShowInfo("复制图片地址到剪贴板成功");
}
else
{
//BaseMsgTipFormHelper.ShowError("复制图片地址到剪贴板失败");
}
return true;
}
if (commandId == (CefMenuCommand)26601)
{
string url = parameters.SourceUrl;
SaveFileDialog dialog = FormDialogHelper.SaveFileDialogCustom(Path.GetFileNameWithoutExtension(url), Path.GetExtension(url), false, false);
if (dialog != null)
{
try
{
HttpHelper.DownloadFile(new HttpParam { Url = url }, dialog.FileName);
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
}
return true;
}
if (commandId == (CefMenuCommand)26601)
{
string url = parameters.SourceUrl;
SaveFileDialog dialog = FormDialogHelper.SaveFileDialogCustom(Path.GetFileNameWithoutExtension(url), Path.GetExtension(url), false, false);
if (dialog != null)
{
try
{
HttpHelper.DownloadFile(new HttpParam { Url = url }, dialog.FileName);
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
}
return true;
}
if (commandId == (CefMenuCommand)26700)
{
string url = parameters.SourceUrl;
try
{
if (NaiBaGlobalConfig.ImageOCR != null)
{
var byteArr = HttpHelper.GetByte(new HttpParam { Url = url });
var obj = NaiBaGlobalConfig.ImageOCR(Convert.ToBase64String(byteArr));
if (obj.Tag == 1)
{
FileHelper.OpenNotepad(string.Join(Environment.NewLine, obj.Data));
}
else
{
BaseMsgFormHelper.ShowError(obj.Message);
}
}
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
return true;
}
if (commandId == (CefMenuCommand)26700)
{
string url = parameters.SourceUrl;
try
{
if (NaiBaGlobalConfig.ImageOCR != null)
{
var byteArr = HttpHelper.GetByte(new HttpParam { Url = url });
var obj = NaiBaGlobalConfig.ImageOCR(Convert.ToBase64String(byteArr));
if (obj.Tag == 1)
{
FileHelper.OpenNotepad(string.Join(Environment.NewLine, obj.Data));
}
else
{
BaseMsgFormHelper.ShowError(obj.Message);
}
}
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
return true;
}
if (commandId == (CefMenuCommand)26800)
{
try
{
string fileName = "NaiBa.CefHelper.Resources.google_translate.js";
string js = FileHelper.GetResourceFileContent(Assembly.GetExecutingAssembly(), fileName);
if (commandId == (CefMenuCommand)26800)
{
try
{
string fileName = "NaiBa.CefHelper.Resources.google_translate.js";
string js = FileHelper.GetResourceFileContent(Assembly.GetExecutingAssembly(), fileName);
browser.MainFrame.ExecuteJavaScriptAsync(js);
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
return true;
}
browser.MainFrame.ExecuteJavaScriptAsync(js);
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
return true;
}
if (commandId == (CefMenuCommand)26900)
{
try
{
if (CookieList == null)
{
BaseMsgTipFormHelper.ShowError("未获取到 Cookie,请等待页面加载完后再试");
}
else
{
var obj = SaveCookie(CookieList);
if (obj.Tag == 1)
{
BaseMsgTipFormHelper.ShowInfo("保存 Cookie 成功");
}
else
{
BaseMsgTipFormHelper.ShowError("保存 Cookie 失败:" + obj.Message);
}
}
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
return true;
}
if (commandId == (CefMenuCommand)26900)
{
try
{
if (CookieList == null)
{
BaseMsgTipFormHelper.ShowError("未获取到 Cookie,请等待页面加载完后再试");
}
else
{
var obj = SaveCookie(CookieList);
if (obj.Tag == 1)
{
BaseMsgTipFormHelper.ShowInfo("保存 Cookie 成功");
}
else
{
BaseMsgTipFormHelper.ShowError("保存 Cookie 失败:" + obj.Message);
}
}
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
return true;
}
if ((int)commandId >= 28000 && (int)commandId <= 28500)
{
if (ContextMenuList != null)
{
var contextMenu = ContextMenuList.Where(p => p.CommandCode == (int)commandId).FirstOrDefault();
if (contextMenu != null)
{
try
{
contextMenu.CommandAction?.Invoke(parameters);
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
return true;
}
}
}
if ((int)commandId >= 28000 && (int)commandId <= 28500)
{
if (ContextMenuList != null)
{
var contextMenu = ContextMenuList.Where(p => p.CommandCode == (int)commandId).FirstOrDefault();
if (contextMenu != null)
{
try
{
contextMenu.CommandAction?.Invoke(parameters);
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
return true;
}
}
}
// Return false should ignore the selected option of the user !
return false;
}
void IContextMenuHandler.OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
#if DEBUG
if (commandId == (CefMenuCommand)99990)
{
browser.ShowDevTools();
return true;
}
#endif
// Return false should ignore the selected option of the user !
return false;
}
}
void IContextMenuHandler.OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
bool IContextMenuHandler.RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}
}
public class CefContextMenu
{
public int CommandCode { get; set; }
bool IContextMenuHandler.RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}
public string CommandText { get; set; }
public class CefContextMenu
{
public int CommandCode { get; set; }
public Action<IContextMenuParams> CommandAction { get; set; }
}
public string CommandText { get; set; }
public Action<IContextMenuParams> CommandAction { get; set; }
}
}