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