272 lines
6.7 KiB
C#
272 lines
6.7 KiB
C#
using CefSharp;
|
||
using NaiBa.Util;
|
||
using NaiBa.Util.Extension;
|
||
using NaiBa.Util.Helper;
|
||
using NaiBa.Util.Model;
|
||
using NaiBa.Util.Winform.Forms;
|
||
using NaiBa.Util.Winform.Helper;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
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;
|
||
|
||
void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
|
||
{
|
||
|
||
|
||
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();
|
||
}
|
||
|
||
model.AddItem(CefMenuCommand.Back, "后退");
|
||
model.AddItem(CefMenuCommand.Forward, "前进");
|
||
model.AddItem(CefMenuCommand.ReloadNoCache, "刷新");
|
||
model.AddSeparator();
|
||
|
||
model.AddItem(CefMenuCommand.ViewSource, "查看页面源代码");
|
||
model.AddSeparator();
|
||
|
||
model.AddItem((CefMenuCommand)26500, "复制页面地址");
|
||
|
||
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 (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 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;
|
||
}
|
||
|
||
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)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);
|
||
|
||
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 ((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 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; }
|
||
|
||
public string CommandText { get; set; }
|
||
|
||
public Action<IContextMenuParams> CommandAction { get; set; }
|
||
}
|
||
}
|