Exemplo n.º 1
0
 private JavaScriptValue SetWnBrowerTileAndSubtitle(Formium owner, JavaScriptValue[] arguments)
 {
     try
     {
         if (arguments.Length == 0)
         {
             return(JavaScriptValue.CreateString("无参数"));
         }
         if (arguments.Length > 1)
         {
             var strTitle    = arguments[0].GetString();
             var strSubtitle = arguments[1].GetString();
             if (!string.IsNullOrWhiteSpace(strTitle) && !string.IsNullOrWhiteSpace(strSubtitle))
             {
                 owner.Title    = strTitle;
                 owner.Subtitle = strSubtitle;
                 Wnconfig.SetAppsettingValue("Title", strTitle);
                 Wnconfig.SetAppsettingValue("Subtitle", strSubtitle);
             }
         }
         else
         {
             JavaScriptValue.CreateString("调用失败!SetWnBrowerTileAndSubtitle至少需要两个参数!(Title,Subtitle)");
         }
     }
     catch (Exception ex)
     {
         return(JavaScriptValue.CreateString(ex.Message));
     }
     return(JavaScriptValue.CreateString("调用SetWnBrowerTileAndSubtitle成功"));
 }
Exemplo n.º 2
0
        // Example: Map the CLR objects to NanUI's JavaScript context.
        private void MapClrObjectToJavaScript()
        {
            var obj = JavaScriptValue.CreateObject();

            // Readonly property
            obj.SetValue("now", JavaScriptValue.CreateProperty(() =>
            {
                return(JavaScriptValue.CreateDateTime(DateTime.Now));
            }));

            // Value
            obj.SetValue("version", JavaScriptValue.CreateString(Assembly.GetExecutingAssembly().GetName().Version?.ToString()));

            // Readable and writable property
            obj.SetValue("subtitle", JavaScriptValue.CreateProperty(() => JavaScriptValue.CreateString(Subtitle), title => Subtitle = title.GetString()));

            // Sync method
            obj.SetValue("messagebox", JavaScriptValue.CreateFunction(args =>
            {
                var msg = args.FirstOrDefault(x => x.IsString);

                var text = msg?.GetString();

                InvokeIfRequired(() =>
                {
                    MessageBox.Show(HostWindow, text, "Message from JS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                });

                return(JavaScriptValue.CreateString(text));
            }));

            // Async method
            obj.SetValue("asyncmethod", JavaScriptValue.CreateFunction((args, callback) =>
            {
                Task.Run(async() =>
                {
                    var rnd = new Random(DateTime.Now.Millisecond);

                    var rndValue = rnd.Next(1000, 2000);

                    await Task.Delay(rndValue);
                    var obj = JavaScriptValue.CreateObject();

                    obj.SetValue("delayed", JavaScriptValue.CreateNumber(rndValue));
                    obj.SetValue("message", JavaScriptValue.CreateString($"Delayed {rndValue} milliseconds"));


                    callback.Success(obj);
                });
            }));

            RegisterExternalObjectValue("tester", obj);
        }
Exemplo n.º 3
0
        private JavaScriptValue GetCultureInfo(Formium owner, JavaScriptValue[] arguments)
        {
            var retval = JavaScriptValue.CreateObject();

            retval.SetValue("name", JavaScriptValue.CreateString($"{Thread.CurrentThread.CurrentCulture.Name}"));

            retval.SetValue("displayName", JavaScriptValue.CreateString($"{Thread.CurrentThread.CurrentCulture.DisplayName}"));
            retval.SetValue("englishName", JavaScriptValue.CreateString($"{Thread.CurrentThread.CurrentCulture.EnglishName}"));

            retval.SetValue("lcid", JavaScriptValue.CreateNumber(Thread.CurrentThread.CurrentCulture.LCID));

            return(retval);
        }
        // Register async method without communicating with Formium object.
        private void Delay(JavaScriptValue[] arguments, JavaScriptRendererSideAsyncFunctionCallback callback)
        {
            var time = arguments.FirstOrDefault(x => x.IsNumber)?.GetInt() ?? 1000;

            var msg = arguments.FirstOrDefault(x => x.IsString)?.GetString() ?? "hello world";


            Task.Run(async() =>
            {
                await Task.Delay(time);

                callback.Success(JavaScriptValue.CreateString($"Say: {msg}! after {time / 1000f} sec."));
            });
        }
Exemplo n.º 5
0
        private JavaScriptValue UpdateWnBrowerUrlIp(Formium owner, JavaScriptValue[] arguments)
        {
            if (!Wnconfig.GetGrayFlag())
            {
                return(JavaScriptValue.CreateString("启动灰度地址失败! 已经在配置文件WinningBrowserApp.config中已经禁用灰度更新"));
            }

            if (arguments.Length == 0)
            {
                return(JavaScriptValue.CreateString("启动灰度地址失败!未传Ip参数"));
            }
            if (arguments.Length > 0)
            {
                var strNewIp  = arguments[0].GetString();
                var strUserid = "无用户名";
                var strPw     = "无密码";
                if (arguments.Length > 1)
                {
                    strUserid = arguments[1].GetString();
                    strPw     = arguments[2].GetString();
                }

                if (!string.IsNullOrEmpty(strNewIp) && !Wnconfig.GetUrl().Contains(strNewIp))
                {
                    string strGrayIp = strNewIp;
                    MessageBox.Show(string.Format("启用灰度地址:{0}", strGrayIp));
                    string msgresault = Wnconfig.SetUrlIp(strGrayIp, strUserid, strPw, true);
                    if (msgresault.Contains("失败"))
                    {
                        return(JavaScriptValue.CreateString(msgresault));
                    }

                    Thread thread = new Thread(() =>
                    {
                        try
                        {
                            MainForm.delRestart();
                        }
                        catch (Exception ex)
                        {
                        }
                    });
                    //thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    return(JavaScriptValue.CreateString("启动灰度地址成功(UpdateWnBrowerUrlIp)"));
                }
            }
            return(JavaScriptValue.CreateString("启动灰度地址失败!"));
        }
        // Register async method that need communicate with Formium object.
        private void HelloAsync(Formium owner, JavaScriptValue[] arguments, JavaScriptAsyncFunctionCallback callback)
        {
            var time = arguments.FirstOrDefault(x => x.IsNumber)?.GetInt() ?? 1000;
            var msg  = arguments.FirstOrDefault(x => x.IsString)?.GetString() ?? "hello world";



            Task.Run(async() =>
            {
                await Task.Delay(time);

                MessageBox.Show($"Delayed {time / 1000f} sec.");

                callback.Success(JavaScriptValue.CreateString(msg));
            });
        }
Exemplo n.º 7
0
        private JavaScriptValue CreateShortcutOnDesktop(Formium owner, JavaScriptValue[] arguments)
        {
            try
            {
                if (arguments.Length == 0)
                {
                    return(JavaScriptValue.CreateString("无参数"));
                }
                if (arguments.Length > 0 && !string.IsNullOrWhiteSpace(arguments[0].GetString()))
                {
                    //卫宁基层卫生信息系统V5.6

                    ShortCutHelper.SetWinningBrowerShortcut(arguments[0].GetString(), Application.ExecutablePath);
                }
            }
            catch (Exception ex)
            {
                return(JavaScriptValue.CreateString(ex.Message));
            }
            return(JavaScriptValue.CreateString("调用CreateShortcutOnDesktop成功"));
        }
Exemplo n.º 8
0
        private JavaScriptValue GrayEnable(Formium owner, JavaScriptValue[] arguments)
        {
            try
            {
                if (arguments.Length == 0)
                {
                    return(JavaScriptValue.CreateString("无参数"));
                }
                if (arguments.Length > 0 && !string.IsNullOrWhiteSpace(arguments[0].GetString()))
                {
                    var bGrayEnable = Convert.ToBoolean(arguments[0].GetString());

                    Wnconfig.SetAppsettingValue("GrayFlag", bGrayEnable.ToString());
                }
            }
            catch (Exception ex)
            {
                return(JavaScriptValue.CreateString(ex.Message));
            }
            return(JavaScriptValue.CreateString("调用GrayEnable成功"));
        }
Exemplo n.º 9
0
        // Example: Register JavaScript object in current frame.
        private void RegisterJavaScriptExampleObject()
        {
            var jsDemo = JavaScriptValue.CreateObject();

            jsDemo.SetValue("executeJavaScriptWithoutRetval", JavaScriptValue.CreateFunction(args =>
            {
                InvokeIfRequired(() =>
                {
                    ExecuteJavaScript("alert(1+1)");
                });
                return(null);
            }));

            jsDemo.SetValue("executeJavaScriptWithRetval", JavaScriptValue.CreateFunction(args =>
            {
                InvokeIfRequired(async() =>
                {
                    var result = await EvaluateJavaScriptAsync("(function(){ return {a:1,b:'hello',c:(s)=>alert(s)}; })()");
                    if (result.Success)
                    {
                        var retval = result.ResultValue;
                        MessageBox.Show($"a={retval.GetInt("a")} b={retval.GetString("b")}", "Value from JS");
                        await retval.GetValue("c").ExecuteFunctionAsync(GetMainFrame(), new JavaScriptValue[] { JavaScriptValue.CreateString("Hello from C#") });
                    }
                });
                return(null);
            }));

            RegisterExternalObjectValue("jsExamples", jsDemo);
        }
Exemplo n.º 10
0
        private JavaScriptValue ProcessInvok(Formium owner, JavaScriptValue[] arguments)
        {
            try
            {
                if (arguments.Length == 0)
                {
                    return(JavaScriptValue.CreateString("无参数"));
                }
                if (arguments.Length > 1)
                {
                    var strExePath = arguments[0].GetString();

                    var strArgs = arguments[1].GetString();
                    if (string.IsNullOrEmpty(strExePath))
                    {
                        return(JavaScriptValue.CreateString("无参数"));
                    }
                    if (!System.IO.File.Exists(strExePath))
                    {
                        return(JavaScriptValue.CreateString(string.Format("{0}路径不存在! 请检查路径!", strExePath)));
                    }
                    //System.Windows.Forms.MessageBox.Show(string.Format("执行路径:{0}", strExePath));
                    //System.Windows.Forms.MessageBox.Show(string.Format("传入参数:{0}", strArgs));
                    Process exep = new System.Diagnostics.Process();
                    exep.StartInfo.FileName       = strExePath;
                    exep.StartInfo.Arguments      = strArgs;
                    exep.StartInfo.CreateNoWindow = true;

                    //exep.StartInfo.UseShellExecute = false;
                    // exep.StartInfo.RedirectStandardOutput = true;
                    exep.Start();
                    //exep.WaitForExit();
                    return(JavaScriptValue.CreateString("调用成功!"));
                }
                if (arguments.Length == 1)
                {
                    string str = arguments[0].GetString();

                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.FileName               = "cmd.exe";
                    p.StartInfo.UseShellExecute        = false; //是否使用操作系统shell启动
                    p.StartInfo.RedirectStandardInput  = true;  //接受来自调用程序的输入信息
                    p.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
                    p.StartInfo.RedirectStandardError  = true;  //重定向标准错误输出
                    p.StartInfo.CreateNoWindow         = true;  //不显示程序窗口
                    p.Start();                                  //启动程序

                    //向cmd窗口发送输入信息
                    // p.StandardInput.WriteLine(str);
                    p.StandardInput.WriteLine(str);
                    p.StandardInput.WriteLine("exit");//结束执行,很重要的
                    p.StandardInput.AutoFlush = true;

                    //获取cmd窗口的输出信息
                    string output = p.StandardOutput.ReadToEnd();
                    p.WaitForExit();//等待程序执行完退出进程
                    p.Close();
                    return(JavaScriptValue.CreateString(output));
                }
            }
            catch (System.Exception ex)
            {
                return(JavaScriptValue.CreateString(ex.Message));
            }
            return(null);
        }
Exemplo n.º 11
0
 // Register sync method without communicating with Formium object.
 private JavaScriptValue Test(JavaScriptValue[] arguments)
 {
     return(JavaScriptValue.CreateString("OK"));
 }
Exemplo n.º 12
0
 // Register sync method that need communicate with Formium object.
 private JavaScriptValue GetTitle(Formium owner, JavaScriptValue[] arguments)
 {
     return(JavaScriptValue.CreateString(owner.Subtitle));
 }
Exemplo n.º 13
0
 private JavaScriptValue GetWindowState(Formium owner, JavaScriptValue[] arguments)
 {
     return(JavaScriptValue.CreateString(owner.WindowState.ToString().ToLower()));
 }
Exemplo n.º 14
0
 private JavaScriptValue GetChromiumVersion(JavaScriptValue[] _)
 {
     return(JavaScriptValue.CreateString($"{CefRuntime.ChromeVersion}"));
 }
Exemplo n.º 15
0
 private JavaScriptValue GetWinFormiumVersion(JavaScriptValue[] _)
 {
     return(JavaScriptValue.CreateString($"{Assembly.GetExecutingAssembly().GetName().Version}"));
 }