public override CfrV8Value OnExtensionExecute(string nativeFunctionName, CfrV8Value[] arguments, CfrV8Value @this, ref string exception) { CfrV8Value retval = null; Console.WriteLine($"[FUNC]:{nativeFunctionName}"); switch (nativeFunctionName) { case nameof(Version): var accessor = new CfrV8Accessor(); var versionObject = CfrV8Value.CreateObject(accessor); versionObject.SetValue("nanui", CfrV8Value.CreateString(Version), CfxV8PropertyAttribute.DontDelete | CfxV8PropertyAttribute.ReadOnly); versionObject.SetValue("cef", CfrV8Value.CreateString(CefVersion), CfxV8PropertyAttribute.DontDelete | CfxV8PropertyAttribute.ReadOnly); versionObject.SetValue("chromium", CfrV8Value.CreateString(ChromiumVersion), CfxV8PropertyAttribute.DontDelete | CfxV8PropertyAttribute.ReadOnly); retval = versionObject; break; } return(retval); }
private void GetConfigFormCSFunc_Execute(object sender, CfrV8HandlerExecuteEventArgs e) { var config = JsonConvert.SerializeObject(CommonUtils.ConfigInstance); var jsObjectAccessor = new CfrV8Accessor(); var jsObject = CfrV8Value.CreateObject(jsObjectAccessor); jsObject.SetValue("config", config, CfxV8PropertyAttribute.DontDelete); e.SetReturnValue(jsObject); }
public void GetFlowPicSize(object func, CfrV8HandlerExecuteEventArgs args) { var jsObjectAccessor = new CfrV8Accessor(); var jsObject = CfrV8Value.CreateObject(jsObjectAccessor); jsObject.SetValue("width", CfrV8Value.CreateInt(GlobDef.sz_image.width), CfxV8PropertyAttribute.ReadOnly); jsObject.SetValue("height", CfrV8Value.CreateInt(GlobDef.sz_image.height), CfxV8PropertyAttribute.ReadOnly); args.SetReturnValue(jsObject); }
private void GetNoteContentFormCSFunc_Execute(object sender, Chromium.Remote.Event.CfrV8HandlerExecuteEventArgs e) { var noteID = e.Arguments.FirstOrDefault(p => p.IsString).ToString(); var noteContent = noteUtils.GetNoteContent(Convert.ToInt32(noteID)); var jsObjectAccessor = new CfrV8Accessor(); var jsObject = CfrV8Value.CreateObject(jsObjectAccessor); if (noteContent == null) { return; } jsObject.SetValue("id", noteContent.id, CfxV8PropertyAttribute.DontDelete); jsObject.SetValue("n_content", noteContent.n_content, CfxV8PropertyAttribute.DontDelete); e.SetReturnValue(jsObject); }
private void GetHistoryContentFormCSFunc_Execute(object sender, CfrV8HandlerExecuteEventArgs e) { var htime = e.Arguments.FirstOrDefault(p => p.IsString).ToString(); foreach (var item in noteContentHistoryList) { if (item != null && item.create_time.Equals(htime)) { var jsObjectAccessor = new CfrV8Accessor(); var jsObject = CfrV8Value.CreateObject(jsObjectAccessor); jsObject.SetValue("n_content", item.n_content, CfxV8PropertyAttribute.DontDelete); e.SetReturnValue(jsObject); break; } } }
internal override CfrV8Value CreateV8Value() { v8Accessor = new CfrV8Accessor(); v8Accessor.Get += v8Accessor_Get; v8Accessor.Set += v8Accessor_Set; var o = CfrV8Value.CreateObject(v8Accessor); foreach (var p in jsProperties) { if (p.Value.PropertyType == JSPropertyType.Dynamic) { o.SetValue(p.Key, CfxV8AccessControl.Default, CfxV8PropertyAttribute.DontDelete); } else { o.SetValue(p.Key, p.Value.GetV8Value(v8Context), CfxV8PropertyAttribute.DontDelete | CfxV8PropertyAttribute.ReadOnly); } } return(o); }
private void GetNoteContentHistoryFormCSFunc_Execute(object sender, CfrV8HandlerExecuteEventArgs e) { var noteID = e.Arguments.FirstOrDefault(p => p.IsString).ToString(); noteContentHistoryList = noteUtils.GetHistoryNoteContent(Convert.ToInt32(noteID)); if (noteContentHistoryList != null && noteContentHistoryList.Count > 0) { var jsObjectAccessor = new CfrV8Accessor(); var jsObject = CfrV8Value.CreateObject(jsObjectAccessor); var noteListArray = CfrV8Value.CreateArray(noteContentHistoryList.Count); int i = 0; foreach (var note in noteContentHistoryList) { noteListArray.SetValue(i, note.create_time); i++; } jsObject.SetValue("historyArray", noteListArray, CfxV8PropertyAttribute.DontDelete); e.SetReturnValue(jsObject); } }
private void GetTitleFormCSFunc_Execute(object sender, Chromium.Remote.Event.CfrV8HandlerExecuteEventArgs e) { var noteList = noteUtils.GetNotesTitle(); var jsObjectAccessor = new CfrV8Accessor(); var jsObject = CfrV8Value.CreateObject(jsObjectAccessor); var noteListArray = CfrV8Value.CreateArray(noteList.Count); int i = 0; foreach (var note in noteList) { //jsObject.SetValue(note.id.ToString(), CfrV8Value.CreateString(note.n_title), CfxV8PropertyAttribute.ReadOnly); var jsArray = CfrV8Value.CreateArray(5); jsArray.SetValue("id", note.id, CfxV8PropertyAttribute.DontDelete); jsArray.SetValue("n_title", note.n_title, CfxV8PropertyAttribute.DontDelete); jsArray.SetValue("n_length", note.n_length, CfxV8PropertyAttribute.DontDelete); jsArray.SetValue("create_time", note.create_time, CfxV8PropertyAttribute.DontDelete); jsArray.SetValue("update_time", note.update_time, CfxV8PropertyAttribute.DontDelete); noteListArray.SetValue(i, jsArray); i++; } jsObject.SetValue("noteArray", noteListArray, CfxV8PropertyAttribute.DontDelete); e.SetReturnValue(jsObject); }
public static CfrV8Value V8Serialize(this object o, HashSet <object> refin = null) { if (Accessor == null) { Accessor = new CfrV8Accessor(); } var propattr = CfxV8PropertyAttribute.DontDelete | CfxV8PropertyAttribute.ReadOnly; switch (o) { case null: return(CfrV8Value.CreateNull()); case bool onb: return(CfrV8Value.CreateBool(onb)); case string os: return(CfrV8Value.CreateString(os)); case Enum oenum: return(CfrV8Value.CreateString(oenum.ToString())); case int oni: return(CfrV8Value.CreateInt(oni)); case sbyte oni: return(CfrV8Value.CreateInt(oni)); case short oni: return(CfrV8Value.CreateInt(oni)); case long oni: return(CfrV8Value.CreateInt((int)oni)); case uint onui: return(CfrV8Value.CreateUint(onui)); case byte onui: return(CfrV8Value.CreateUint(onui)); case ushort onui: return(CfrV8Value.CreateUint(onui)); case double ond: return(CfrV8Value.CreateDouble(ond)); case float onf: return(CfrV8Value.CreateDouble(onf)); case ObjectBuilder oob: return(oob.Serialize(refin)); case RGBAColor ocv: var vcolres = CfrV8Value.CreateObject(Accessor); vcolres.SetValue("r", CfrV8Value.CreateInt(ocv.Color.R), propattr); vcolres.SetValue("g", CfrV8Value.CreateInt(ocv.Color.G), propattr); vcolres.SetValue("b", CfrV8Value.CreateInt(ocv.Color.B), propattr); vcolres.SetValue("a", CfrV8Value.CreateDouble(ocv.A), propattr); vcolres.SetValue("name", CfrV8Value.CreateString("#" + ocv.Color.Name.Remove(0, 2)), propattr); return(vcolres); case Color4 ocdx: var dxcolres = CfrV8Value.CreateObject(Accessor); dxcolres.SetValue("r", CfrV8Value.CreateDouble(ocdx.Red * 255), propattr); dxcolres.SetValue("g", CfrV8Value.CreateDouble(ocdx.Green * 255), propattr); dxcolres.SetValue("b", CfrV8Value.CreateDouble(ocdx.Blue * 255), propattr); dxcolres.SetValue("a", CfrV8Value.CreateDouble(ocdx.Alpha), propattr); dxcolres.SetValue("name", CfrV8Value.CreateString(ocdx.ToString()), propattr); return(dxcolres); case TimeSpan ots: return(CfrV8Value.CreateDate(new CfrTime() { Year = 0, Month = 0, DayOfMonth = ots.Days, DayOfWeek = ots.Days, Hour = ots.Hours, Minute = ots.Minutes, Second = ots.Seconds, Millisecond = ots.Milliseconds })); case DateTime odt: return(CfrV8Value.CreateDate(new CfrTime() { Year = odt.Year, Month = odt.Month, DayOfMonth = odt.Day, DayOfWeek = (int)odt.DayOfWeek, Hour = odt.Hour, Minute = odt.Minute, Second = odt.Second, Millisecond = odt.Millisecond })); case IEnumerable oenum: lock (oenum) { var reslist = (from object obj in oenum select obj.V8Serialize()).ToList(); var res = CfrV8Value.CreateArray(reslist.Count); for (int i = 0; i < reslist.Count; i++) { res.SetValue(i, reslist[i]); } return(res); } default: var referenced = refin; if (referenced != null && referenced.Contains(o)) { return(CfrV8Value.CreateNull()); } else if (referenced == null) { referenced = new HashSet <object> { o }; } else { referenced.Add(o); } var oT = o.GetType(); if (oT.IsClass || (oT.IsValueType && !oT.IsPrimitive)) { var reso = CfrV8Value.CreateObject(Accessor); foreach (var prop in oT.GetProperties().Where(p => p.CanRead && p.GetMethod.IsPublic && !p.GetMethod.IsStatic && !p.PropertyType.IsPointer) ) { var cobj = prop.GetValue(o).V8Serialize(referenced); reso.SetValue(prop.Name, cobj, propattr); } foreach (var field in oT.GetFields().Where(f => f.IsPublic && !f.IsStatic && !f.FieldType.IsPointer) ) { var cobj = field.GetValue(o).V8Serialize(referenced); reso.SetValue(field.Name, cobj, propattr); } return(reso); } else { return(CfrV8Value.CreateNull()); } } }
public Form1() : base("http://res.app.local/www/index.html") { InitializeComponent(); LoadHandler.OnLoadEnd += LoadHandler_OnLoadEnd; //register the "my" object var myObject = GlobalObject.AddObject("my"); //add property "name" to my, you should implemnt the getter/setter of name property by using PropertyGet/PropertySet events. var nameProp = myObject.AddDynamicProperty("name"); nameProp.PropertyGet += (prop, args) => { // getter - if js code "my.name" executes, it'll get the string "NanUI". args.Retval = CfrV8Value.CreateString("NanUI"); args.SetReturnValue(true); }; nameProp.PropertySet += (prop, args) => { // setter's value from js context, here we do nothing, so it will store or igrone by your mind. var value = args.Value; args.SetReturnValue(true); }; //add a function showCSharpMessageBox var showMessageBoxFunc = myObject.AddFunction("showCSharpMessageBox"); showMessageBoxFunc.Execute += (func, args) => { //it will be raised by js code "my.showCSharpMessageBox(`some text`)" executed. //get the first string argument in Arguments, it pass by js function. var stringArgument = args.Arguments.FirstOrDefault(p => p.IsString); if (stringArgument != null) { MessageBox.Show(this, stringArgument.StringValue, "C# Messagebox", MessageBoxButtons.OK, MessageBoxIcon.Information); } }; //add a function getArrayFromCSharp, this function has an argument, it will combind C# string array with js array and return to js context. var friends = new string[] { "Mr.JSON", "Mr.Lee", "Mr.BONG" }; var getArrayFromCSFunc = myObject.AddFunction("getArrayFromCSharp"); getArrayFromCSFunc.Execute += (func, args) => { var jsArray = args.Arguments.FirstOrDefault(p => p.IsArray); if (jsArray == null) { jsArray = CfrV8Value.CreateArray(friends.Length); for (int i = 0; i < friends.Length; i++) { jsArray.SetValue(i, CfrV8Value.CreateString(friends[i])); } } else { var newArray = CfrV8Value.CreateArray(jsArray.ArrayLength + friends.Length); for (int i = 0; i < jsArray.ArrayLength; i++) { newArray.SetValue(i, jsArray.GetValue(i)); } var jsArrayLength = jsArray.ArrayLength; for (int i = 0; i < friends.Length; i++) { newArray.SetValue(i + jsArrayLength, CfrV8Value.CreateString(friends[i])); } jsArray = newArray; } //return the array to js context args.SetReturnValue(jsArray); //in js context, use code "my.getArrayFromCSharp()" will get an array like ["Mr.JSON", "Mr.Lee", "Mr.BONG"] }; //add a function getObjectFromCSharp, this function has no arguments, but it will return a Object to js context. var getObjectFormCSFunc = myObject.AddFunction("getObjectFromCSharp"); getObjectFormCSFunc.Execute += (func, args) => { //create the CfrV8Value object and the accssor of this Object. var jsObjectAccessor = new CfrV8Accessor(); var jsObject = CfrV8Value.CreateObject(jsObjectAccessor); //create a CfrV8Value array var jsArray = CfrV8Value.CreateArray(friends.Length); for (int i = 0; i < friends.Length; i++) { jsArray.SetValue(i, CfrV8Value.CreateString(friends[i])); } jsObject.SetValue("libName", CfrV8Value.CreateString("NanUI"), CfxV8PropertyAttribute.ReadOnly); jsObject.SetValue("friends", jsArray, CfxV8PropertyAttribute.DontDelete); args.SetReturnValue(jsObject); //in js context, use code "my.getObjectFromCSharp()" will get an object like { friends:["Mr.JSON", "Mr.Lee", "Mr.BONG"], libName:"NanUI" } }; //add a function with callback var callbackTestFunc = GlobalObject.AddFunction("callbackTest"); callbackTestFunc.Execute += (func, args) => { var callback = args.Arguments.FirstOrDefault(p => p.IsFunction); if (callback != null) { var callbackArgs = CfrV8Value.CreateObject(new CfrV8Accessor()); callbackArgs.SetValue("success", CfrV8Value.CreateBool(true), CfxV8PropertyAttribute.ReadOnly); callbackArgs.SetValue("text", CfrV8Value.CreateString("Message from C#"), CfxV8PropertyAttribute.ReadOnly); callback.ExecuteFunction(null, new CfrV8Value[] { callbackArgs }); } }; //add a function with async callback var asyncCallbackTestFunc = GlobalObject.AddFunction("asyncCallbackTest"); asyncCallbackTestFunc.Execute += async(func, args) => { //save current context var v8Context = CfrV8Context.GetCurrentContext(); var callback = args.Arguments.FirstOrDefault(p => p.IsFunction); //simulate async methods. await Task.Delay(5000); if (callback != null) { //get render process context var rc = callback.CreateRemoteCallContext(); //enter render process rc.Enter(); //create render task var task = new CfrTask(); task.Execute += (_, taskArgs) => { //enter saved context v8Context.Enter(); //create callback argument var callbackArgs = CfrV8Value.CreateObject(new CfrV8Accessor()); callbackArgs.SetValue("success", CfrV8Value.CreateBool(true), CfxV8PropertyAttribute.ReadOnly); callbackArgs.SetValue("text", CfrV8Value.CreateString("Message from C#"), CfxV8PropertyAttribute.ReadOnly); //execute callback callback.ExecuteFunction(null, new CfrV8Value[] { callbackArgs }); v8Context.Exit(); //lock task from gc lock (task) { Monitor.PulseAll(task); } }; lock (task) { //post task to render process v8Context.TaskRunner.PostTask(task); } rc.Exit(); GC.KeepAlive(task); } }; }
// 浏览器Javascript环境初始化完成 protected override void OnRegisterGlobalObject(JSObject global) { // 可以在此处将C#对象注入到当前窗口的JS上下文中 //add a function with callback function Console.Write(global); var callbackTestFunc = global.AddFunction("callbackTest"); callbackTestFunc.Execute += (func, args) => { var callback = args.Arguments.FirstOrDefault(p => p.IsFunction); if (callback != null) { WebBrowser.ExecuteJavascript("ChangeTitle()"); var callbackArgs = CfrV8Value.CreateObject(new CfrV8Accessor()); callbackArgs.SetValue("success", CfrV8Value.CreateBool(true), CfxV8PropertyAttribute.ReadOnly); callbackArgs.SetValue("text", CfrV8Value.CreateString("Message from Windows Client"), CfxV8PropertyAttribute.ReadOnly); callback.ExecuteFunction(null, new CfrV8Value[] { callbackArgs }); } }; //register the "my" object var myObject = global.AddObject("my"); //add property "name" to my, you should implemnt the getter/setter of name property by using PropertyGet/PropertySet events. var nameProp = myObject.AddDynamicProperty("name"); nameProp.PropertyGet += (prop, args) => { string computerName = Environment.MachineName; string value = $"My Computer Name is JoyNop :)\n{computerName}"; // getter - if js code "my.name" executes, it'll get the string "NanUI". args.Retval = CfrV8Value.CreateString(value); args.SetReturnValue(true); }; nameProp.PropertySet += (prop, args) => { // setter's value from js context, here we do nothing, so it will store or igrone by your mind. var value = args.Value; args.SetReturnValue(true); }; //add a function showCSharpMessageBox var showMessageBoxFunc = myObject.AddFunction("showCSharpMessageBox"); showMessageBoxFunc.Execute += (func, args) => { //it will be raised by js code "my.showCSharpMessageBox(`some text`)" executed. //get the first string argument in Arguments, it pass by js function. var stringArgument = args.Arguments.FirstOrDefault(p => p.IsString); if (stringArgument != null) { string osVersionName = Environment.OSVersion.ToString(); MessageBox.Show(osVersionName, "Windows 内核版本", MessageBoxButtons.OK, MessageBoxIcon.Information); } }; var friends = new string[] { "Mr.JSON", "Mr.Lee", "Mr.BONG" }; var getObjectFormCSFunc = myObject.AddFunction("getObjectFromCSharp"); getObjectFormCSFunc.Execute += (func, args) => { //create the CfrV8Value object and the accssor of this Object. var jsObjectAccessor = new CfrV8Accessor(); var jsObject = CfrV8Value.CreateObject(jsObjectAccessor); //create a CfrV8Value array var jsArray = CfrV8Value.CreateArray(friends.Length); for (int i = 0; i < friends.Length; i++) { jsArray.SetValue(i, CfrV8Value.CreateString(friends[i])); } jsObject.SetValue("libName", CfrV8Value.CreateString("NanUI"), CfxV8PropertyAttribute.ReadOnly); jsObject.SetValue("friends", jsArray, CfxV8PropertyAttribute.DontDelete); args.SetReturnValue(jsObject); //in js context, use code "my.getObjectFromCSharp()" will get an object like { friends:["Mr.JSON", "Mr.Lee", "Mr.BONG"], libName:"NanUI" } }; }
public static CfrV8Value GetCfrObject(this JSObject _, object item) { var nameDict = new Dictionary <string, string>(); var accessor = new CfrV8Accessor(); var o = CfrV8Value.CreateObject(accessor); var t = item.GetType(); foreach (var p in t.GetProperties()) { var name = p.Name.Substring(0, 1).ToLower() + p.Name.Substring(1); nameDict[name] = p.Name; o.SetValue(name, CfxV8AccessControl.Default, CfxV8PropertyAttribute.DontDelete); } accessor.Get += (s, e) => { var name = nameDict[e.Name]; var p = t.GetProperty(name); var value = p.GetValue(item, null); var valueType = value?.GetType(); if (value == null) { e.Retval = CfrV8Value.CreateNull(); } else if (valueType == typeof(string) || valueType == typeof(Guid)) { e.Retval = CfrV8Value.CreateString((string)value); } else if (valueType == typeof(int) || valueType == typeof(short) || valueType == typeof(long)) { e.Retval = CfrV8Value.CreateInt((int)value); } else if (valueType == typeof(decimal)) { e.Retval = CfrV8Value.CreateDouble(Convert.ToDouble(value)); } else if (valueType == typeof(float) || valueType == typeof(double)) { e.Retval = CfrV8Value.CreateDouble(Convert.ToDouble(value)); } else if (valueType == typeof(bool)) { e.Retval = CfrV8Value.CreateBool(Convert.ToBoolean(value)); } else if (valueType == typeof(DateTime)) { e.Retval = CfrV8Value.CreateDate(CfrTime.FromUniversalTime((DateTime)value)); } else { e.Retval = CfrV8Value.CreateNull(); } e.SetReturnValue(true); }; return(o); }