コード例 #1
0
        private void CreateEngine(IWindowEx window, Func <Func <Stream, object>, XmlHttpRequest> createXmlHttpRequest)
        {
            _jsEngine = new JintJsEngine(window);

            _jsEngine.Execute("var window = this");
            _jsEngine.Execute("var self = window");
            _jsEngine.Execute("function Image(w, h) { var img = document.createElement('img'); img.width = w || 0; img.height = h||0; return img;}");

            foreach (var type in ScriptingSettings.Default.GlobalTypes)
            {
                _jsEngine.AddGlobalType(type);
            }

            _jsEngine.AddGlobalType <Event>(new [] { typeof(string), typeof(EventInitOptions) },
                                            args => new Event(window.Document, args[0]?.ToString(), args.Length > 1 ? (EventInitOptions)args[1] : null));

            Func <Stream, object> parseJsonFn = s => _jsEngine.ParseJson(s.ReadToEnd());

            _jsEngine.AddGlobalType("XMLHttpRequest", x => createXmlHttpRequest(parseJsonFn));
        }
コード例 #2
0
        public static IJsScriptExecutor Create(ScriptExecutionContext ctx)
        {
            var jsEngine = new JintJsEngine(ctx.Window);

            jsEngine.Execute("var window = this");
            jsEngine.Execute("var self = window");

            foreach (var type in ctx.Settings.GlobalTypes)
            {
                var name = type.GetCustomAttribute <JsNameAttribute>()?.Name ?? type.Name;
                jsEngine.AddGlobalType(type, name);
            }

            return(jsEngine);
        }