CefSharp 版本: 84.4.10
public partial class electron : Form { public electron() { Logger.Default.Info("程序启动"); //OpenStart(); //UpdateProgram(); InitializeComponent(); InitializeChromium(); } public ChromiumWebBrowser chromiumBrowser; /// <summary> /// 浏览器初始化方法 /// </summary> public void InitializeChromium() { CefSettings settings = new CefSettings(); settings.IgnoreCertificateErrors = true; settings.LogSeverity = CefSharp.LogSeverity.Verbose; settings.Locale = "zh-CN"; //必须进行初始化,否则就出来页面啦。 CefSharp.Cef.Initialize(settings); //实例化控件 chromiumBrowser = new ChromiumWebBrowser(OperationConfig.GetAppConfig("url")) { MenuHandler = new MenuHandler(), Dock = DockStyle.Fill }; chromiumBrowser.LoadError += _webView_LoadError; chromiumBrowser.KeyboardHandler = new KeyBoardHander(); //该属性就是让WebBrowser与WPF支持js交互,否则不可以建立jsObject 这个对象 CefSharpSettings.LegacyJavascriptBindingEnabled = true; //注册js对象据说,79版本以上的注册方法是下面这句 chromiumBrowser.JavascriptObjectRepository.Register("bound", new JsEvent(), false); // 网页加载完毕时的处理 chromiumBrowser.FrameLoadEnd += (sender, e) => { //string script = "alert('加载完毕!');"; //e.Frame.ExecuteJavaScriptAsync(script); }; //加入到当前窗体中 this.Controls.Add(chromiumBrowser); //chromiumBrowser.ExecuteScriptAsync(""); } public void _webView_LoadError(object sender, CefSharp.LoadErrorEventArgs e) { MessageBox.Show("加载页面失败,请退出客户端进行重试"); chromiumBrowser.Dispose(); } } /// <summary> /// 禁用右键菜单 /// </summary> internal class MenuHandler : IContextMenuHandler { public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model) { model.Clear(); } public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags) { return false; } public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame) { } public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback) { return false; } } /// <summary> /// 调试窗体 /// </summary> public class KeyBoardHander : IKeyboardHandler { public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey) { if (type == KeyType.KeyUp && Enum.IsDefined(typeof(Keys), windowsKeyCode)) { var key = (Keys)windowsKeyCode; switch (key) { case Keys.F12: browser.ShowDevTools(); break; case Keys.F5: if (modifiers == CefEventFlags.ControlDown) { //MessageBox.Show("ctrl+f5"); browser.Reload(true); //强制忽略缓存 } else { //MessageBox.Show("f5"); browser.Reload(); } break; } } return false; } public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut) { return false; const int VK_F5 = 0x74; if (windowsKeyCode == VK_F5) { browser.Reload(); //此处可以添加想要实现的代码段 } return false; } }
新建一个js对象类 JsEvent
public class JsEvent { public string getVideo() { return "http://***.**.*/video/test.mp4";//getImageStr("F:\\test.mp4"); } public string getImageStr(String path) { String contentType = ""; try { var reg = @"[\s\S].+[\\/](.+\..+)"; var matches = Regex.Matches(path, reg); //matches[0].Groups[1]; String fileName = matches[0].Groups[1].ToString(); String suffix = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - (fileName.LastIndexOf(".") + 1)); if ("jpg".Equals(suffix, StringComparison.CurrentCultureIgnoreCase) || "jpeg".Equals(suffix, StringComparison.CurrentCultureIgnoreCase)) { contentType = "data:image/jpeg;base64,"; } else if ("png".Equals(suffix, StringComparison.CurrentCultureIgnoreCase)) { contentType = "data:image/png;base64,"; } else if ("aac".Equals(suffix, StringComparison.CurrentCultureIgnoreCase)) { contentType = "data:audio/aac;base64,"; } else if ("mp3".Equals(suffix, StringComparison.CurrentCultureIgnoreCase)) { contentType = "data:audio/mpeg;base64,"; } else if ("wav".Equals(suffix, StringComparison.CurrentCultureIgnoreCase)) { contentType = "data:audio/x-wav;base64,"; } else if ("mp4".Equals(suffix, StringComparison.CurrentCultureIgnoreCase)) { contentType = "data:video/mp4;base64,"; } else if ("ogv".Equals(suffix, StringComparison.CurrentCultureIgnoreCase)) { contentType = "data:video/ogg;base64,"; } using (FileStream fsRead = new FileStream(path, FileMode.Open)) { int fsLen = (int)fsRead.Length; byte[] heByte = new byte[fsLen]; int r = fsRead.Read(heByte, 0, heByte.Length); string base64Str = Convert.ToBase64String(heByte); return contentType + base64Str; } } catch (IOException e) { throw; } } /// <summary> /// 将文件转换成二进制 /// </summary> /// <param name="Path">文件路径</param> /// <returns></returns> public static byte[] ConvertToBinary(string Path) { FileStream stream = new FileInfo(Path).OpenRead(); byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, Convert.ToInt32(stream.Length)); return buffer; } }
然后前端调用
bound.getVideo()
我这里主要是实现客户端视频在网页上播放所以效果如下:
代码:
<template> <video :src="videoSrc" controls="controls" width="500" height="400"></video> </template> <script> export default { data() { return { videoSrc: "", }; }, created: function() { this.getVideoPath(); }, methods: { sdf(event, props) { console.log(event, props); }, getVideoPath() { console.log(bound.getVideo()); this.videoSrc = bound.getVideo(); } } }; </script>
效果:
评论区