private void LoadHandler_OnLoadEnd(object sender, Chromium.Event.CfxOnLoadEndEventArgs e) { if (e.Frame.IsMain) { // 来这里干点有趣的事情,让必应网站能够支持我们拖动移动窗口 // 通过JS注入下面的CSS,修改必应的菜单栏,加上背景虚化部分,并让背景虚化部分支持拖动 WebBrowser.ExecuteJavascript(@" var style = document.createElement(""style""); style.type = ""text/css""; style.innerHTML=` .hp_hor_hdr { -webkit-app-region: drag; background-color:rgba(255,255,255,0.1) !important; backdrop-filter:blur(15px); } .hp_hor_hdr ul, .hp_hor_hdr #id_h { -webkit-app-region: no-drag; } .b_header{ -webkit-app-region: drag; } .b_header > * { -webkit-app-region: no-drag; } `; document.getElementsByTagName(""HEAD"").item(0).appendChild(style); "); } }
private void HtmlTitleMouseDown_Execute(object sender, Chromium.Remote.Event.CfrV8HandlerExecuteEventArgs e) { //为当前应用程序释放鼠标捕获 ReleaseCapture(); SendMessage((IntPtr)ContainerForm.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); if (ContainerForm.WindowState != FormWindowState.Maximized) { WebBrowser.ExecuteJavascript("setFull()"); } }
private void sendBigStringToJSToolStripMenuItem_Click(object sender, EventArgs e) { if (CfxRuntime.PlatformArch == CfxPlatformArch.x86) { if (MessageBox.Show("This test may crash the render process in 32-bit mode. Continue?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) { return; } } WebBrowser.ExecuteJavascript("var bigString = bigStringFunction(); testlog('big string received: ' + bigString.substring(0, 20) + '(...) (' + bigString.length + ' chars)'); bigStringFunction(bigString);"); }
private void RemoteLayerStressTest() { int domCount = 0; int evalCount = 0; for (int i = 0; i < 100; ++i) { WebBrowser.ExecuteJavascript("document.getElementById('testbutton1').click()"); WebBrowser.ExecuteJavascript("document.getElementById('testbutton2').click()"); WebBrowser.EvaluateJavascript("document.getElementById('testbutton3').click()", (value, exception) => { LogWriteLine("EVAL {0}: document.getElementById('testbutton3').click() = {1}", ++evalCount, value.StringValue); }); WebBrowser.ExecuteJavascript("document.getElementById('testbutton4').click()"); WebBrowser.EvaluateJavascript("document.getElementById('testbutton5').click()", (value, exception) => { LogWriteLine("EVAL {0}: document.getElementById('testbutton5').click() = {1}", ++evalCount, value.StringValue); }); WebBrowser.ExecuteJavascript("document.getElementById('testbutton6').click()"); WebBrowser.EvaluateJavascript("doubleCallback('foo', 123)", (value, exception) => { LogWriteLine("EVAL {0}: doubleCallback('foo', 123) = {1}", ++evalCount, value.StringValue); }); WebBrowser.VisitDom((doc, b) => { //LogWriteLine("VisitDom {0} {1}", doc.ToString(), b.ToString()); var n = doc.GetElementById("testbutton6"); var bounds = n.ElementBounds; LogWriteLine("DOM {0} - testbutton6 = {1} ({2}, {3})", ++domCount, n.AsMarkup, bounds.Width, bounds.Height); // must dispose node, otherwise finalizer crash in the render process. n.Dispose(); }); //WebBrowser.VisitDom(VisitDOMCallback); //for(int ii = 0; ii < 100; ++ii) // VisitDomButton_Click(null, null); //MessageBox.Show("GC.WaitForPendingFinalizers();"); //GC.Collect(); //GC.WaitForPendingFinalizers(); // give the browser some time to render and do message loop work //Thread.Sleep(20); } }
public void LoginByToken() { //string a = WebService.SHA1("123"); Random rand = new Random(); string r = "" + rand.Next(100000, 999999); string queryString = "branchCenterId=" + args[1] + "&username="******"&r=" + r; string md = SHA1(queryString + "&masterKey=701CAD396352B8C8EF1B1C758F337E344D535E03"); JObject jo = new JObject(); jo["md"] = md; jo["branchCenterId"] = 0; jo["username"] = "******"; jo["r"] = r; var jsText = "tlogin(" + jo.ToString() + ")"; //var jsText = "tlogin(" + jo.ToString() + ")"; WebBrowser.ExecuteJavascript(jsText); }
private void RemoteLayerStressTest() { for (int i = 0; i < 100; ++i) { WebBrowser.ExecuteJavascript("document.getElementById('testbutton1').click()"); WebBrowser.ExecuteJavascript("document.getElementById('testbutton2').click()"); WebBrowser.EvaluateJavascript("document.getElementById('testbutton3').click()", (value, exception) => { LogWriteLine("document.getElementById('testbutton3').click()"); LogWriteLine(value.StringValue); }); WebBrowser.ExecuteJavascript("document.getElementById('testbutton4').click()"); WebBrowser.EvaluateJavascript("document.getElementById('testbutton5').click()", (value, exception) => { LogWriteLine("document.getElementById('testbutton5').click()"); LogWriteLine(value.StringValue); }); WebBrowser.ExecuteJavascript("document.getElementById('testbutton6').click()"); WebBrowser.EvaluateJavascript("doubleCallback('foo', 123)", (value, exception) => { LogWriteLine("doubleCallback('foo', 123)"); LogWriteLine(value.StringValue); }); WebBrowser.VisitDom((doc, b) => { LogWriteLine("VisitDom {0} {1}", doc.ToString(), b.ToString()); LogWriteLine("testbutton6", doc.GetElementById("testbutton6").AsMarkup); }); // give the browser some time to render and do message loop work //Thread.Sleep(20); } }
// Testing async use of v8 values passed in per JSFunction. private void asyncUseOfV8ValuesToolStripMenuItem_Click(object sender, EventArgs e) { WebBrowser.ExecuteJavascript(" function AsyncTestFunction(text) { alert(text); return 'The alert was shown'; } SubmitAsyncTestFunction(AsyncTestFunction); "); }
private void listKeysInDocumentObjectToolStripMenuItem_Click(object sender, EventArgs e) { WebBrowser.ExecuteJavascript("ListKeysInDocumentObject(document)"); }
private void executeArrayTestFunctionToolStripMenuItem_Click(object sender, EventArgs e) { // Related to issue #65 WebBrowser.ExecuteJavascript("ArrayTestCallback([document.body.scrollWidth, document.body.scrollHeight])"); }
private void executeSleepFunctionToolStripMenuItem_Click(object sender, EventArgs e) { WebBrowser.ExecuteJavascript("SleepFunction(0);"); }
// 浏览器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" } }; }