Exemple #1
0
        /// <summary>
        /// 执行js V8方法
        /// </summary>
        /// <param name="reString">Js代码</param>
        /// <param name="para">参数字符串(使用逗号分隔)</param>
        /// <param name="MethodName">方法名称</param>
        public static string V8Method(string reString, string para, string MethodName)
        {
            V8Engine engine = V8Engine.Create();        //创建V8对象
            V8Script script = engine.Compile(reString); //编译

            try
            {
                engine.Execute(script);                                                              //将编译的脚本加载到V8引擎中
                string res = engine.Execute(string.Format("{0}({1})", MethodName, para)).ToString(); //执行结果
                return(res);
            }
            catch (Exception ex)
            {
                return(ex.Message);//异常信息
            }
        }
Exemple #2
0
        public void CompileAndExecute(string script)
        {
            if (isDisposing)
            {
                return;
            }

            const string ES_REF = "ecmascript:";
            const string JS_REF = "javascript:";

            script = script.TrimStart();

            string start = script.Substring(0, ES_REF.Length).ToLower();

            script = start.StartsWith(ES_REF) ? script.Remove(0, ES_REF.Length) : script;
            script = start.StartsWith(JS_REF) ? script.Remove(0, JS_REF.Length) : script;

            using (InternalHandle handle = v8.Compile(script, SOURCE_NAME, false).AsInternalHandle)
            {
                if (!handle.IsError)
                {
                    try
                    {
                        Handle result = v8.Execute(handle, true);
                        //handle.Dispose();
                    }
                    catch (Exception ex)
                    {
                        ConsoleColor tmp = Console.ForegroundColor;
                        Console.ForegroundColor = ConsoleColor.Red;

                        Console.WriteLine("(SCRIPT) {0}", ex.Message);

                        Console.ForegroundColor = tmp;
                    }
                }
            }
        }
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            returnValue = CefV8Value.CreateNull();
            exception   = null;

            try
            {
                var context = CefV8Context.GetCurrentContext();
                var browser = context.GetBrowser();
                var frame   = browser.GetMainFrame();

                if (name == "compile" || name == "loadScript" || name == "loadScriptCompiled")
                {
                    if (arguments.Length >= 3)
                    {
                        var script                = arguments[0].GetStringValue();
                        var sourceName            = arguments[1].GetStringValue();
                        var throwExceptionOnError = arguments[2].GetBoolValue();
                        var callback              = (arguments.Length == 4 ? arguments[3] : null);

                        if (!string.IsNullOrEmpty(script) && !string.IsNullOrEmpty(sourceName))
                        {
                            if (callback != null && callback.IsFunction)
                            {
                                var runner = CefTaskRunner.GetForCurrentThread();
                                new Task(() =>
                                {
                                    context.Enter();
                                    if (name == "compile")
                                    {
                                        engine.Compile(script, sourceName, throwExceptionOnError);
                                    }
                                    else if (name == "loadScript")
                                    {
                                        engine.LoadScript(script, sourceName, throwExceptionOnError);
                                    }
                                    else if (name == "loadScriptCompiled")
                                    {
                                        engine.LoadScriptCompiled(script, sourceName, throwExceptionOnError);
                                    }
                                    runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(new StatusHelper("OK", name))));
                                    context.Exit();
                                }).Start();
                            }
                            else
                            {
                                if (name == "compile")
                                {
                                    engine.Compile(script, sourceName, throwExceptionOnError);
                                }
                                else if (name == "loadScript")
                                {
                                    engine.LoadScript(script, sourceName, throwExceptionOnError);
                                }
                                else if (name == "loadScriptCompiled")
                                {
                                    engine.LoadScriptCompiled(script, sourceName, throwExceptionOnError);
                                }
                                returnValue = CefV8Value.CreateString(JsonMapper.ToJson(new StatusHelper("OK", name)));
                            }
                        }
                        else
                        {
                            returnValue = DontMeetRequirements(context, callback);
                        }
                    }
                    else
                    {
                        returnValue = DontMeetRequirements(context, null);
                    }
                }
                else if (name == "consoleExecute" || name == "execute" || name == "verboseConsoleExecute")
                {
                    if (arguments.Length >= 4)
                    {
                        var script                = arguments[0].GetStringValue();
                        var sourceName            = arguments[1].GetStringValue();
                        var throwExceptionOnError = arguments[2].GetBoolValue();
                        var timeOut               = arguments[3].GetIntValue();
                        var callback              = (arguments.Length == 5 ? arguments[4] : null);

                        if (!string.IsNullOrEmpty(script) && !string.IsNullOrEmpty(sourceName))
                        {
                            if (callback != null && callback.IsFunction)
                            {
                                var runner = CefTaskRunner.GetForCurrentThread();
                                new Task(() =>
                                {
                                    context.Enter();
                                    if (name == "consoleExecute")
                                    {
                                        engine.ConsoleExecute(script, sourceName, throwExceptionOnError, timeOut);
                                    }
                                    else if (name == "execute")
                                    {
                                        engine.Execute(script, sourceName, throwExceptionOnError, timeOut);
                                    }
                                    else if (name == "verboseConsoleExecute")
                                    {
                                        engine.VerboseConsoleExecute(script, sourceName, throwExceptionOnError, timeOut);
                                    }
                                    runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(new StatusHelper("OK", name))));
                                    context.Exit();
                                }).Start();
                            }
                            else
                            {
                                if (name == "consoleExecute")
                                {
                                    engine.ConsoleExecute(script, sourceName, throwExceptionOnError, timeOut);
                                }
                                else if (name == "execute")
                                {
                                    engine.Execute(script, sourceName, throwExceptionOnError, timeOut);
                                }
                                else if (name == "verboseConsoleExecute")
                                {
                                    engine.VerboseConsoleExecute(script, sourceName, throwExceptionOnError, timeOut);
                                }
                                returnValue = CefV8Value.CreateString(JsonMapper.ToJson(new StatusHelper("OK", name)));
                            }
                        }
                        else
                        {
                            returnValue = DontMeetRequirements(context, callback);
                        }
                    }
                    else
                    {
                        returnValue = DontMeetRequirements(context, null);
                    }
                }
                else if (name == "terminateExecution")
                {
                    if (arguments.Length >= 0)
                    {
                        var callback = (arguments.Length == 1 ? arguments[0] : null);

                        if (callback != null && callback.IsFunction)
                        {
                            var runner = CefTaskRunner.GetForCurrentThread();
                            new Task(() =>
                            {
                                context.Enter();
                                engine.TerminateExecution();
                                runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(new StatusHelper("OK", "TerminateExecution"))));
                                context.Exit();
                            }).Start();
                        }
                        else
                        {
                            engine.TerminateExecution();
                            returnValue = CefV8Value.CreateString(JsonMapper.ToJson(new StatusHelper("OK", "TerminateExecution")));
                        }
                    }
                    else
                    {
                        returnValue = DontMeetRequirements(context, null);
                    };
                }
                else if (name == "runStartupFile")
                {
                    if (arguments.Length >= 0)
                    {
                        var callback = (arguments.Length == 1 ? arguments[0] : null);

                        if (callback != null && callback.IsFunction)
                        {
                            var runner = CefTaskRunner.GetForCurrentThread();
                            new Task(() =>
                            {
                                context.Enter();

                                var message = "V8 Script Engine : Started";
                                Log.Trace(message, configuration.Verbose);
                                runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(new StatusHelper("OK", message))));

                                context.Exit();
                            }).Start();
                        }
                        else
                        {
                            var message = "V8 Script Engine : Started";
                            Log.Trace(message, configuration.Verbose);
                            returnValue = CefV8Value.CreateString(JsonMapper.ToJson(new StatusHelper("OK", message)));
                        }
                    }
                    else
                    {
                        returnValue = DontMeetRequirements(context, null);
                    };
                }
                return(true);
            }
            catch (Exception ex)
            {
                returnValue = CefV8Value.CreateNull();
                exception   = ex.Message;
                return(true);
            }
        }