Browser.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Diagnostics;
  3. using System.Windows.Forms;
  4. using UAS_Web.tool;
  5. using CefSharp;
  6. using CefSharp.WinForms;
  7. using System.Text.RegularExpressions;
  8. using System.Collections.Generic;
  9. using System.Net;
  10. using System.Text;
  11. using System.IO;
  12. using System.Web;
  13. using System.Threading;
  14. namespace UAS_Web
  15. {
  16. public partial class Browser : Form
  17. {
  18. private ChromiumWebBrowser webBrowser;
  19. public Browser()
  20. {
  21. InitializeComponent();
  22. Text = "供应商条码打印";
  23. string path = "http://113.108.109.210:8099/ERP/vendbarcode/relogin.action";
  24. webBrowser = new ChromiumWebBrowser(path)
  25. {
  26. Dock = DockStyle.Fill
  27. };
  28. CheckForIllegalCrossThreadCalls = false;
  29. webBrowser.MenuHandler = new MenuHandler();
  30. webBrowser.RequestHandler = new RequestHandler();
  31. webBrowser.DownloadHandler = new DownLoadFile();
  32. Controls.Add(webBrowser);
  33. webBrowser.LoadError += WebBrowser_LoadError;
  34. webBrowser.FrameLoadEnd += FrameLoadEnd;
  35. webBrowser.FrameLoadStart += WebBrowser_FrameLoadStart;
  36. webBrowser.KeyboardHandler = new CEFKeyBoardHander();
  37. }
  38. private void WebBrowser_FrameLoadStart(object sender, FrameLoadStartEventArgs e)
  39. {
  40. }
  41. void FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
  42. {
  43. Thread.Sleep(500);
  44. var cookiemanager = CefSharp.Cef.GetGlobalCookieManager();
  45. CookieVisitor visitor = new CookieVisitor();
  46. visitor.SendCookie += visitor_SendCookie;
  47. cookiemanager.VisitAllCookies(visitor);
  48. if (e.Url.Contains("vendbarcode/login.jsp") || e.Url.Contains("relogin.action"))
  49. {
  50. e.Browser.FocusedFrame.EvaluateScriptAsync("document.getElementById('username').value = '" + BaseUtil.GetCacheData("username") + "';document.getElementById('password').value = '" + BaseUtil.GetCacheData("password") + "';document.getElementById('master').innerHTML = '" + BaseUtil.GetCacheData("master_fun") + "';document.getElementById('master1').value = '" + BaseUtil.GetCacheData("master_name") + "';", new TimeSpan(600000));
  51. return;
  52. }
  53. }
  54. private void visitor_SendCookie(CefSharp.Cookie obj)
  55. {
  56. if (obj.Path.Contains("vendbarcode"))
  57. {
  58. if (obj.Name == "username")
  59. BaseUtil.SetCacheData("username", HttpUtility.UrlDecode(obj.Value));
  60. if (obj.Name == "password")
  61. BaseUtil.SetCacheData("password", HttpUtility.UrlDecode(obj.Value));
  62. if (obj.Name == "master_name")
  63. BaseUtil.SetCacheData("master_name", HttpUtility.UrlDecode(obj.Value));
  64. if (obj.Name == "master_fun")
  65. BaseUtil.SetCacheData("master_fun", HttpUtility.UrlDecode(obj.Value));
  66. }
  67. }
  68. private void WebBrowser_LoadError(object sender, LoadErrorEventArgs e)
  69. {
  70. ////指定放大镜
  71. //if (e.FailedUrl.Contains("vendbarcode/batchdelivery.jsp?whoami=Delivery!Deal#"))
  72. // return;
  73. //if (e.FailedUrl.Contains("common/excel"))
  74. // return;
  75. //if (e.FailedUrl.Contains("common/downloadbyId.action"))
  76. // return;
  77. //MessageBox.Show("当前网络无法访问" + e.ErrorText);
  78. }
  79. private void Browser_FormClosing(object sender, FormClosingEventArgs e)
  80. {
  81. webBrowser.GetBrowser().CloseBrowser(true);
  82. Process.GetCurrentProcess().Kill();
  83. }
  84. private void Browser_Load(object sender, EventArgs e)
  85. {
  86. Process[] pro = Process.GetProcessesByName("UAS_Web");
  87. for (int i = 0; i < pro.Length; i++)
  88. {
  89. if (pro[i].Id != Process.GetCurrentProcess().Id)
  90. {
  91. pro[i].Kill();
  92. }
  93. }
  94. textBox1.Text = webBrowser.Address;
  95. }
  96. private void textBox1_KeyDown(object sender, KeyEventArgs e)
  97. {
  98. if (e.KeyCode == Keys.Enter)
  99. {
  100. webBrowser.Load(textBox1.Text);
  101. }
  102. }
  103. }
  104. public class CEFKeyBoardHander : IKeyboardHandler
  105. {
  106. public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
  107. {
  108. if (type == KeyType.KeyUp && Enum.IsDefined(typeof(Keys), windowsKeyCode))
  109. {
  110. var key = (Keys)windowsKeyCode;
  111. switch (key)
  112. {
  113. case Keys.F12:
  114. browser.ShowDevTools();
  115. break;
  116. }
  117. }
  118. return false;
  119. }
  120. public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
  121. {
  122. return false;
  123. }
  124. }
  125. }