//private static List<jsNativeFunction> jsnaviteList = new List<jsNativeFunction>(); public void BindJsFunc() { if (GlobalObjectJs == null) { GlobalObjectJs = this; } var att = GlobalObjectJs.GetType().GetMethods(); //jsnaviteList.Clear(); var result = new ArrayList(); foreach (var item in att) { var xx = item.GetCustomAttributes(typeof(JSFunctin), true); if (xx != null && xx.Length != 0) { var jsnav = new wkeJsNativeFunction((es, _param) => { var xp = item.GetParameters(); var argcount = BlinkBrowserPInvoke.jsArgCount(es); long param = 0L; if (xp != null && xp.Length != 0 && argcount != 0) { object[] listParam = new object[BlinkBrowserPInvoke.jsArgCount(es)]; for (int i = 0; i < argcount; i++) { Type tType = xp[i].ParameterType; var paramnow = BlinkBrowserPInvoke.jsArg(es, i); param = paramnow; if (tType == typeof(int)) { listParam[i] = Convert.ChangeType(BlinkBrowserPInvoke.jsToInt(es, paramnow), tType); } else if (tType == typeof(double)) { listParam[i] = Convert.ChangeType(BlinkBrowserPInvoke.jsToDouble(es, paramnow), tType); } else if (tType == typeof(float)) { listParam[i] = Convert.ChangeType(BlinkBrowserPInvoke.jsToFloat(es, paramnow), tType); } else if (tType == typeof(bool)) { listParam[i] = Convert.ChangeType(BlinkBrowserPInvoke.jsToBoolean(es, paramnow), tType); } else { listParam[i] = Convert.ChangeType((BlinkBrowserPInvoke.jsToString(es, paramnow)).Utf8IntptrToString(), tType); } } try { var res = item.Invoke(GlobalObjectJs, listParam); if (res != null) { var mStr = Marshal.StringToHGlobalUni(res.ToString()); return(BlinkBrowserPInvoke.jsStringW(es, mStr));//返回JS字符串 } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } else { var res = item.Invoke(GlobalObjectJs, null); if (res != null) { var mStr = Marshal.StringToHGlobalUni(res.ToString()); return(BlinkBrowserPInvoke.jsStringW(es, mStr));//返回JS字符串 } } return(param); }); BlinkBrowserPInvoke.wkeJsBindFunction(item.Name, jsnav, IntPtr.Zero, (uint)item.GetParameters().Length); listObj.Add(jsnav); } } }
public JsValue(IntPtr jsExecState, int argIndex) { this.intptr_0 = jsExecState; this.long_0 = BlinkBrowserPInvoke.jsArg(this.intptr_0, argIndex); }
/// <summary> /// 初始化 MB /// </summary> public void CreateCore() { BlinkBrowserPInvoke.wkeInitialize(); //BlinkBrowserPInvoke.wkeInitializeExWrap(new wkeSettings() //{ // proxy = new wkeProxy // { // hostname = "127.0.0.1", // port = 8888, // type = wkeProxyType.WKE_PROXY_HTTP, // password = "", // username = "" // }, // mask = wkeSettingMask.WKE_SETTING_PROXY //}); //BlinkBrowserPInvoke.wkeInitialize(); //BlinkBrowserPInvoke.wkeInitializeExWrap(new wkeSettings() //{ // proxy = new wkeProxy // { // hostname = "127.0.0.1", // port = 8888, // type = wkeProxyType.WKE_PROXY_HTTP, // password = "", // username = "" // }, // mask = 1 //}); handle = BlinkBrowserPInvoke.wkeCreateWebView(); //只有开启才会触发 wkeOnCreateView BlinkBrowserPInvoke.wkeSetNavigationToNewWindowEnable(handle, true); BlinkBrowserPInvoke.wkeSetHandle(this.handle, this.Handle); BlinkBrowserPInvoke.wkeSetHandleOffset(handle, Location.X - 2, 0); //BlinkBrowserPInvoke.wkeSetTransparent(handle, true); BlinkBrowserPInvoke.wkeSetCookieEnabled(handle, true); CookiePath = Application.StartupPath + "\\cookie\\"; if (!Directory.Exists(CookiePath)) { Directory.CreateDirectory(CookiePath); } BlinkBrowserPInvoke.wkeSetCookieJarPath(handle, CookiePath); BlinkBrowserPInvoke.wkeResize(handle, Width, Height); BlinkBrowserPInvoke.wkeSetUserAgentW(this.handle, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36"); BindJsFunc(); AlertBoxCallback = new AlertBoxCallback((a, b, c) => { MessageBox.Show(Marshal.PtrToStringUni(BlinkBrowserPInvoke.wkeToStringW(c)), Url + "提示"); }); BlinkBrowserPInvoke.wkeOnAlertBox(handle, AlertBoxCallback, IntPtr.Zero); //设置声音 //BlinkBrowserPInvoke.wkeSetMediaVolume(handle, 20); _wkeNavigationCallback = OnwkeNavigationCallback; BlinkBrowserPInvoke.wkeOnNavigation(handle, _wkeNavigationCallback, IntPtr.Zero); listObj.Add(_wkeNavigationCallback); //BlinkBrowserPInvoke.wkeSetCookieEnabled(handle, false); titleChangeCallback = OnTitleChangedCallback; BlinkBrowserPInvoke.wkeOnTitleChanged(this.handle, titleChangeCallback, IntPtr.Zero); listObj.Add(titleChangeCallback); titleChangeCallback2 = OnTitleChangedCallback2; BlinkBrowserPInvoke.wkeOnMouseOverUrlChanged(this.handle, titleChangeCallback2, IntPtr.Zero); listObj.Add(titleChangeCallback2); _wkeDocumentReadyCallback = OnwkeDocumentReadyCallback; BlinkBrowserPInvoke.wkeOnDocumentReady(this.handle, _wkeDocumentReadyCallback, IntPtr.Zero); listObj.Add(_wkeDocumentReadyCallback); urlChangedCallback = OnUrlChangedCallback; BlinkBrowserPInvoke.wkeOnURLChanged(this.handle, urlChangedCallback, IntPtr.Zero); listObj.Add(urlChangedCallback); urlChangedCallback2 = OnUrlChangedCallback2; BlinkBrowserPInvoke.wkeOnURLChanged2(this.handle, urlChangedCallback2, IntPtr.Zero); listObj.Add(urlChangedCallback2); _wkeConsoleMessageCallback = OnwkeConsoleMessageCallback; BlinkBrowserPInvoke.wkeOnConsole(this.handle, _wkeConsoleMessageCallback, IntPtr.Zero); listObj.Add(_wkeConsoleMessageCallback); _wkePaintUpdatedCallback = OnWkePaintUpdatedCallback; BlinkBrowserPInvoke.wkeOnPaintUpdated(this.handle, _wkePaintUpdatedCallback, IntPtr.Zero); listObj.Add(_wkePaintUpdatedCallback); _wkeDocumentReadyCallback = OnwkeDocumentReadyCallback; //var pa = Marshal.StringToCoTaskMemUni("我传的值:::::"); //BlinkBrowserPInvoke.wkeOnDocumentReady(this.handle, _wkeDocumentReadyCallback, pa); BlinkBrowserPInvoke.wkeOnDocumentReady(this.handle, _wkeDocumentReadyCallback, IntPtr.Zero); listObj.Add(_wkeDocumentReadyCallback); _wkeLoadingFinishCallback = OnwkeLoadingFinishCallback; BlinkBrowserPInvoke.wkeOnLoadingFinish(this.handle, _wkeLoadingFinishCallback, IntPtr.Zero); listObj.Add(_wkeLoadingFinishCallback); //会导致 taobao 加载图片异常 _wkeDownloadFileCallback = OnwkeDownloadFileCallback; BlinkBrowserPInvoke.wkeOnDownload(this.handle, _wkeDownloadFileCallback, IntPtr.Zero); listObj.Add(_wkeDownloadFileCallback); _wkeCreateViewCallback = OnwkeCreateViewCallback; BlinkBrowserPInvoke.wkeOnCreateView(this.handle, _wkeCreateViewCallback, handle); listObj.Add(_wkeCreateViewCallback); _wkeLoadUrlBeginCallback = OnwkeLoadUrlBeginCallback; BlinkBrowserPInvoke.wkeOnLoadUrlBegin(this.handle, _wkeLoadUrlBeginCallback, handle); listObj.Add(_wkeLoadUrlBeginCallback); #region JS 动态绑定,并返回值 wkeJsNativeFunction jsnav = new wkeJsNativeFunction((es, param) => { string s = BlinkBrowserPInvoke.jsToString(es, BlinkBrowserPInvoke.jsArg(es, 0)).Utf8IntptrToString(); IntPtr strPtr = Marshal.StringToCoTaskMemUni("这是C#后台返回值:" + s); Int64 result = BlinkBrowserPInvoke.jsStringW(es, strPtr); Marshal.FreeCoTaskMem(strPtr); return(result); }); BlinkBrowserPInvoke.wkeJsBindFunction("jsReturnValueTest", jsnav, IntPtr.Zero, 1); listObj.Add(jsnav); #endregion // get wkeJsNativeFunction jsnavGet = new wkeJsNativeFunction((es, param) => { Console.WriteLine("call jsBindGetter"); return(BlinkBrowserPInvoke.jsStringW(es, Marshal.StringToCoTaskMemUni("{ \"name\": \"he\" }"))); }); BlinkBrowserPInvoke.wkeJsBindGetter("testJson", jsnavGet, IntPtr.Zero); listObj.Add(jsnavGet); // set wkeJsNativeFunction jsnavSet = new wkeJsNativeFunction((es, _param) => { Console.WriteLine("call jsBindSetter"); Int64 testJson = BlinkBrowserPInvoke.jsArg(es, 0); IntPtr argStr = BlinkBrowserPInvoke.jsToStringW(es, testJson); string argString = Marshal.PtrToStringUni(argStr); //MessageBox.Show(argString, "alert setter"); return(BlinkBrowserPInvoke.jsUndefined(es)); }); BlinkBrowserPInvoke.wkeJsBindSetter("testJson", jsnavSet, IntPtr.Zero); listObj.Add(jsnavSet); _wkeLoadUrlEndCallback = OnwkeLoadUrlEndCallback; BlinkBrowserPInvoke.wkeOnLoadUrlEnd(this.handle, _wkeLoadUrlEndCallback, handle); listObj.Add(_wkeLoadUrlEndCallback); //BlinkBrowserPInvoke.wkeShowDevtools() //readFileCallback = new ReadFileCallback(LoadMemoryData); //wkeOnReadFile(readFileCallback); //listObj.Add(readFileCallback); //文件拖放 //AllowDrop = true; //DragEnter += BlinkBrowser_DragEnter; //DragDrop += BlinkBrowser_DragDrop; //timer.Interval = 25; //timer.Tick += Timer_Tick; //timer.Enabled = true; }