Example #1
0
        protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
        {
            if (frame.IsMain)
            {
                LogManager.CefLog("-> Setting main context!");

                Browser father = CefUtil.GetBrowserFromCef(browser);
                if (father != null)
                {
                    if (!CefUtil.DISABLE_CEF)
                    {
                        father._mainContext = context;
                    }
                    LogManager.CefLog("-> Main context set!");
                }
            }

            CefV8Value global = context.GetGlobal();

            CefV8Value func = CefV8Value.CreateFunction("resourceCall", new V8Bridge(browser));

            global.SetValue("resourceCall", func, CefV8PropertyAttribute.None);

            CefV8Value func2 = CefV8Value.CreateFunction("resourceEval", new V8Bridge(browser));

            global.SetValue("resourceEval", func2, CefV8PropertyAttribute.None);

            base.OnContextCreated(browser, frame, context);
        }
 public CefV8Value CreateObject()
 {
     Handler = CefV8Value.CreateObject();
     Handler.SetValue("query", CefV8Value.CreateFunction("query", this), CefV8PropertyAttribute.None);
     Handler.SetValue("reader", CefV8Value.CreateFunction("reader", this), CefV8PropertyAttribute.None);
     return(Handler);
 }
Example #3
0
        public CefV8Value CreateObject()
        {
            V8Value = CefV8Value.CreateObject();
            V8Value.SetValue("log", CefV8Value.CreateFunction("log", V8Handler), CefV8PropertyAttribute.None);

            return(V8Value);
        }
Example #4
0
 public void Bind(CefV8Value cefV8Value)
 {
     using (var func = CefV8Value.CreateFunction(descriptor.Name, new FunctionHandler(objectId, descriptor, v8Serializer, functionCallRegistry)))
     {
         cefV8Value.SetValue(descriptor.Name, func);
     }
 }
Example #5
0
        protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
        {
            /*缓存数据库*/
            string extensionCode =
                "var cachedb;" +
                "if(!cachedb)" +
                "	cachedb={};"+
                "(function() {" +
                " cachedb.Connect = function(dbName) {" +
                "	native function Connect(dbName);"+
                "	return Connect(dbName);"+
                " };" +

                " cachedb.Execute = function(commandText) {" +
                "	native function Execute(commandText);"+
                "	return Execute(commandText);"+
                " };" +

                " cachedb.Query = function(commandText) {" +
                "	native function Query(commandText);"+
                "	return Query(commandText);"+
                " };" +

                " cachedb.Close = function() {" +
                "	native function Close();"+
                "	return Close();"+
                " };" +

                "})();";
            CefV8Handler ExtendsionHandler = new CwbJsExtendHandler(browser);

            CefRuntime.RegisterExtension("v8/cachedb", extensionCode, ExtendsionHandler);

            /*屏幕分辨率设置*/
            int w = webBrowser.screenWidth;
            int h = webBrowser.screenHeight;

            if (w > 0 && h > 0)
            {
                string jscode =
                    "Object.defineProperty(window.screen, 'height', {" +
                    "    get: function() {" +
                    "        return " + h + ";" +
                    "    }" +
                    "});" +
                    "Object.defineProperty(window.screen, 'width', {" +
                    "    get: function() {" +
                    "        return " + w + ";" +
                    "    }" +
                    "});";
                frame.ExecuteJavaScript(jscode, frame.Url, 0);
            }
            /*注册执行C#方法*/
            CefV8Value   globalValue = context.GetGlobal();
            CefV8Handler callHandler = new CwbJsExtendHandler(browser);
            CefV8Value   callMethod  = CefV8Value.CreateFunction("CallCSharpMethod", callHandler);

            globalValue.SetValue("CallCSharpMethod", callMethod, CefV8PropertyAttribute.None);
            base.OnContextCreated(browser, frame, context);
        }
        public CefV8Value CreateObject()
        {
            CefV8Value Handler = CefV8Value.CreateObject();

            Handler.SetValue("Protocols", CefV8Value.CreateFunction("Protocols", this), CefV8PropertyAttribute.None);
            return(Handler);
        }
    protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
    {
        CefV8Value global = context.GetGlobal();
        CefV8Value func   = CefV8Value.CreateFunction("magic", new V8Handler());

        global.SetValue("magic", func, CefV8PropertyAttribute.None);
        base.OnContextCreated(browser, frame, context);
    }
Example #8
0
        protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
        {
            CefV8Value cefV8Value = context.GetGlobal();

            cefV8Value.SetValue("appName", CefV8Value.CreateString("CMCS.Monitor.Win"), CefV8PropertyAttribute.ReadOnly);

            base.OnContextCreated(browser, frame, context);
        }
Example #9
0
 private void BasicUpdateObject(CefV8Value ires)
 {
     if (ires != null)
     {
         ires.SetValue("_MappedId", CefV8Value.CreateUInt(_Count++),
                       CefV8PropertyAttribute.ReadOnly | CefV8PropertyAttribute.DontEnum | CefV8PropertyAttribute.DontDelete);
     }
 }
Example #10
0
        private void CreateFunction(CefV8Value parent, CefV8Handler method, String name)
        {
            // Create the {name} function.
            CefV8Value value = CefV8Value.CreateFunction(name, method);

            // Add the {name} function to the {parent} object.
            parent.SetValue(name, value, CefV8PropertyAttribute.None);
        }
Example #11
0
 public static void Bind(this CefV8Value @this, string functionname, IWebView iCefV8Context, Action <string, CefV8Value, CefV8Value[]> iaction)
 {
     iCefV8Context.RunAsync(() =>
     {
         var function = CefV8Value.CreateFunction(functionname, new CefV8Handler_Action(iaction));
         @this.SetValue(functionname, function, CefV8PropertyAttribute.None);
     });
 }
Example #12
0
        public void SetCefValue(CefV8Value cefObject, object nativeObject)
        {
            var fieldValue = _fieldInfo.GetValue(nativeObject);
            var cefValue   = fieldValue != null
                ? CefNativeValueConverter.ToCef(fieldValue, _fieldInfo.FieldType)
                : CefV8Value.CreateNull();

            cefObject.SetValue(MemberName, cefValue, CefV8PropertyAttribute.None);
        }
        public void SetCefValue(CefV8Value cefObject, object nativeObject)
        {
            var propertyValue = _propertyInfo.GetValue(nativeObject, null);
            var cefValue      = propertyValue != null
                ? CefNativeValueConverter.ToCef(propertyValue, _propertyInfo.PropertyType)
                : CefV8Value.CreateNull();

            cefObject.SetValue(MemberName, cefValue, CefV8PropertyAttribute.None);
        }
        /// <summary>
        /// Create a new V8 plugin adapter.
        /// </summary>
        /// <param name="pluginObject">
        /// The local or remote native plugin to adapt to V8.
        /// </param>
        /// <param name="v8HostObject">
        /// Optional - if supplied, the adapter will add the native methods and events to this
        /// existing object instead of creating a new one with a property accessor.
        /// </param>
        private V8PluginAdapter(IV8Plugin pluginObject, CefV8Value v8HostObject = null)
        {
            Plugin   = pluginObject;
            V8Object = v8HostObject;
            if (V8Object == null)
            {
                V8Object = CefV8Value.CreateObject(new PluginV8Accessor());
                V8Object.SetUserData(this);
            }

            // Create a single method handler configured to handle all methods on the plugin
            var methodHandler = new PluginMethodV8Handler(Plugin);

            foreach (var methodDescriptor in Plugin.Descriptor.Methods)
            {
                V8Object.SetValue(methodDescriptor.MethodName,
                                  CefV8Value.CreateFunction(methodDescriptor.MethodName, methodHandler),
                                  CefV8PropertyAttribute.None);
            }

            foreach (var eventName in Plugin.Descriptor.Events)
            {
                // For each event, create a child object exposed as a property named after the event
                // The child object is a syntactic placeholder for the addListener/removeListener/hasListener/hasListeners methods

                var eventObject  = CefV8Value.CreateObject(null);
                var eventHandler = new PluginEventV8Handler(Plugin, eventName);

                eventObject.SetValue(PluginEventV8Handler.MethodNameAddListener,
                                     CefV8Value.CreateFunction(PluginEventV8Handler.MethodNameAddListener, eventHandler),
                                     CefV8PropertyAttribute.None);
                eventObject.SetValue(PluginEventV8Handler.MethodNameRemoveListener,
                                     CefV8Value.CreateFunction(PluginEventV8Handler.MethodNameRemoveListener, eventHandler),
                                     CefV8PropertyAttribute.None);
                eventObject.SetValue(PluginEventV8Handler.MethodNameHasListener,
                                     CefV8Value.CreateFunction(PluginEventV8Handler.MethodNameHasListener, eventHandler),
                                     CefV8PropertyAttribute.None);
                eventObject.SetValue(PluginEventV8Handler.MethodNameHasListeners,
                                     CefV8Value.CreateFunction(PluginEventV8Handler.MethodNameHasListeners, eventHandler),
                                     CefV8PropertyAttribute.None);

                V8Object.SetValue(eventName, eventObject, CefV8PropertyAttribute.None);
            }
        }
Example #15
0
        /// <summary>
        /// 创建JavaScript方法
        /// </summary>
        /// <param name="handler">处理程序</param>
        /// <param name="javascriptObject">经过V8 JS引擎处理后的对象</param>
        /// <param name="methodNames">方法键值对集合</param>
        public static void CreateJavascriptMethods(CefV8Handler handler, CefV8Value javascriptWrapper, IList <String> methodNames)
        {
            var unmanagedWrapper = (UnmanagedWrapper)(javascriptWrapper.GetUserData());

            foreach (string methodName in methodNames)
            {
                string jsMethodName = LowercaseFirst(methodName);
                //unmanagedWrapper.AddMethodMapping(methodName, jsMethodName);
                javascriptWrapper.SetValue(jsMethodName, CefV8Value.CreateFunction(jsMethodName, handler), CefV8PropertyAttribute.None);
            }
        }
 public CefV8Value CreateObject()
 {
     Handler = CefV8Value.CreateObject();
     Handler.SetValue("compile", CefV8Value.CreateFunction("compile", this), CefV8PropertyAttribute.None);
     Handler.SetValue("consoleExecute", CefV8Value.CreateFunction("consoleExecute", this), CefV8PropertyAttribute.None);
     Handler.SetValue("execute", CefV8Value.CreateFunction("execute", this), CefV8PropertyAttribute.None);
     Handler.SetValue("loadScript", CefV8Value.CreateFunction("loadScript", this), CefV8PropertyAttribute.None);
     Handler.SetValue("loadScriptCompiled", CefV8Value.CreateFunction("loadScriptCompiled", this), CefV8PropertyAttribute.None);
     Handler.SetValue("terminateExecution", CefV8Value.CreateFunction("terminateExecution", this), CefV8PropertyAttribute.None);
     Handler.SetValue("verboseConsoleExecute", CefV8Value.CreateFunction("verboseConsoleExecute", this), CefV8PropertyAttribute.None);
     Handler.SetValue("runStartupFile", CefV8Value.CreateFunction("runStartupFile", this), CefV8PropertyAttribute.None);
     return(Handler);
 }
Example #17
0
        /// <summary>
        /// 创建JavaScript属性
        /// </summary>
        /// <param name="javascriptWrapper">经过V8 JS引擎处理后的对象</param>
        /// <param name="properties">属性键值对集合</param>
        private static void CreateJavascriptProperties(CefV8Value javascriptWrapper, IList <string> properties)
        {
            var unmanagedWrapper = (UnmanagedWrapper)(javascriptWrapper.GetUserData());

            foreach (string property in properties)
            {
                string jsPropertyName = LowercaseFirst(property);
                var    PropertyInfo   = unmanagedWrapper.ClrObject.GetType().GetProperty(property, BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.DeclaredOnly);
                var    v8Value        = V8ValueClrMap.ConvertToV8Value(PropertyInfo.GetValue(unmanagedWrapper.ClrObject, null));
                javascriptWrapper.SetValue(jsPropertyName, v8Value, CefV8PropertyAttribute.None);
            }
        }
Example #18
0
        private void CefWebBrowserMainView_CreateJSObject(object sender, Xilium.CefGlue.WindowsForms.CefWebBrowser.CefWebCreateJSObjectEventArgs e)
        {
            // Retrieve the context's window object.
            CefV8Value window = e.Context.GetGlobal();

            // window.pos
            CefV8Value pos = CefV8Value.CreateObject(null);

            window.SetValue("pos", pos, CefV8PropertyAttribute.DontDelete | CefV8PropertyAttribute.ReadOnly);

            // window.pos.Message
            CefV8Value pos_Message = CefV8Value.CreateString("Hello Vue.js 讀書會");

            pos.SetValue("Message", pos_Message, CefV8PropertyAttribute.DontDelete | CefV8PropertyAttribute.ReadOnly);
        }
Example #19
0
        /// <summary>
        /// 绑定数据并注册对象
        /// 说明:已经过滤特殊名称,即不含系统自动生成的属性、方法
        /// </summary>
        /// <param name="name">对象名称</param>
        /// <param name="obj">需要绑定的对象</param>
        /// <param name="window">用于注册的V8 JS引擎对象,类似于整个程序的窗口句柄</param>
        public static void Bind(string name, object obj, CefV8Value window)
        {
            var unmanagedWrapper = new UnmanagedWrapper(obj);

            var propertyAccessor = new PropertyAccessor();
            //javascript对象包
            CefV8Value javascriptWrapper = CefV8Value.CreateObject(propertyAccessor);

            //将非托管对象放到javascript对象包
            javascriptWrapper.SetUserData(unmanagedWrapper);

            var handler = new BindingHandler();

            unmanagedWrapper.Properties = GetProperties(obj.GetType());
            CreateJavascriptProperties(javascriptWrapper, unmanagedWrapper.Properties);

            IList <string> methodNames = GetMethodNames(obj.GetType(), unmanagedWrapper.Properties);

            CreateJavascriptMethods(handler, javascriptWrapper, methodNames);

            window.SetValue(name, javascriptWrapper, CefV8PropertyAttribute.None);
        }
 public void SetValue(string AttributeName, IJavascriptObject element, CreationOption option = CreationOption.None)
 {
     _CefV8Value.SetValue(AttributeName, Convert(element), Convert(option));
 }
Example #21
0
        /// <summary>
        /// Add a synchronous method to the supplied JavaScript object.
        /// </summary>
        /// <param name="jsObject"></param>
        /// <param name="functionName"></param>
        /// <param name="func"></param>
        private void RegisterFunction(CefV8Value jsObject, string functionName, Func <CefV8Value[], CefV8Value> func)
        {
            var handler = CefV8Value.CreateFunction(functionName, new HandlerProxy(func));

            jsObject.SetValue(functionName, handler);
        }
Example #22
0
 private void RegisterAsyncFunction(CefV8Value jsObject, string functionName, CefV8Handler handler)
 {
     jsObject.SetValue(functionName, CefV8Value.CreateFunction(functionName, handler));
 }
Example #23
0
        /// <summary>
        /// Add an asynchronous method to the supplied JavaScript object
        /// </summary>
        /// <param name="jsObject"></param>
        /// <param name="functionName"></param>
        /// <param name="func"></param>
        private void RegisterAsyncFunction(CefV8Value jsObject, string functionName, Delegate func)
        {
            var handler = CefV8Value.CreateFunction(functionName, new AsyncHandlerProxy(func, this.promiseFactory));

            jsObject.SetValue(functionName, handler);
        }
Example #24
0
 private void RegisterFunction(CefV8Value jsObject, string functionName, Delegate func)
 {
     jsObject.SetValue(functionName, CefV8Value.CreateFunction(functionName, new HandlerProxy(func)));
 }
Example #25
0
 public CefV8Value CreateObject()
 {
     Handler = CefV8Value.CreateObject();
     Handler.SetValue("setTimeout", CefV8Value.CreateFunction("setTimeout", this), CefV8PropertyAttribute.None);
     Handler.SetValue("setInterval", CefV8Value.CreateFunction("setInterval", this), CefV8PropertyAttribute.None);
     Handler.SetValue("back", CefV8Value.CreateFunction("back", this), CefV8PropertyAttribute.None);
     Handler.SetValue("copy", CefV8Value.CreateFunction("copy", this), CefV8PropertyAttribute.None);
     Handler.SetValue("cut", CefV8Value.CreateFunction("cut", this), CefV8PropertyAttribute.None);
     Handler.SetValue("delete", CefV8Value.CreateFunction("delete", this), CefV8PropertyAttribute.None);
     Handler.SetValue("find", CefV8Value.CreateFunction("find", this), CefV8PropertyAttribute.None);
     Handler.SetValue("forward", CefV8Value.CreateFunction("forward", this), CefV8PropertyAttribute.None);
     Handler.SetValue("getSource", CefV8Value.CreateFunction("getSource", this), CefV8PropertyAttribute.None);
     Handler.SetValue("getZoomLevel", CefV8Value.CreateFunction("getZoomLevel", this), CefV8PropertyAttribute.None);
     Handler.SetValue("load", CefV8Value.CreateFunction("load", this), CefV8PropertyAttribute.None);
     Handler.SetValue("loadString", CefV8Value.CreateFunction("loadString", this), CefV8PropertyAttribute.None);
     Handler.SetValue("paste", CefV8Value.CreateFunction("paste", this), CefV8PropertyAttribute.None);
     Handler.SetValue("print", CefV8Value.CreateFunction("print", this), CefV8PropertyAttribute.None);
     Handler.SetValue("redo", CefV8Value.CreateFunction("redo", this), CefV8PropertyAttribute.None);
     Handler.SetValue("refresh", CefV8Value.CreateFunction("refresh", this), CefV8PropertyAttribute.None);
     Handler.SetValue("selectAll", CefV8Value.CreateFunction("selectAll", this), CefV8PropertyAttribute.None);
     Handler.SetValue("showDevTools", CefV8Value.CreateFunction("showDevTools", this), CefV8PropertyAttribute.None);
     Handler.SetValue("stop", CefV8Value.CreateFunction("stop", this), CefV8PropertyAttribute.None);
     Handler.SetValue("undo", CefV8Value.CreateFunction("undo", this), CefV8PropertyAttribute.None);
     Handler.SetValue("viewSource", CefV8Value.CreateFunction("viewSource", this), CefV8PropertyAttribute.None);
     Handler.SetValue("shutdown", CefV8Value.CreateFunction("shutdown", this), CefV8PropertyAttribute.None);
     Handler.SetValue("printToPDF", CefV8Value.CreateFunction("printToPDF", this), CefV8PropertyAttribute.None);
     Handler.SetValue("getWindowSize", CefV8Value.CreateFunction("getwindowsize", this), CefV8PropertyAttribute.None);
     Handler.SetValue("update", CefV8Value.CreateFunction("update", this), CefV8PropertyAttribute.None);
     Handler.SetValue("checkForUpdates", CefV8Value.CreateFunction("checkforupdates", this), CefV8PropertyAttribute.None);
     return(Handler);
 }
Example #26
0
        public void CreateObject(CefV8Value handler, Assembly assmebly, string @namespace = "")
        {
            if (assmebly != null)
            {
                foreach (var type in assmebly.GetTypes())
                {
                    if (string.IsNullOrEmpty(@namespace) || (!string.IsNullOrEmpty(@namespace) && type.Namespace == @namespace))
                    {
                        CefV8Value methodHandler = null;
                        var        methods       = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
                        foreach (var method in methods)
                        {
                            if (validTypes.Contains(method.ReturnType))
                            {
                                var parameters = method.GetParameters();
                                var p          = parameters.Where(ss => validTypes.Contains(ss.ParameterType)).ToList();
                                if (parameters.Length == p.Count)
                                {
                                    if (methodHandler == null)
                                    {
                                        methodHandler = CefV8Value.CreateFunction(type.FullName, this);
                                    }
                                    methodHandler.SetValue(method.Name, CefV8Value.CreateFunction(type.FullName + "." + method.Name, this), CefV8PropertyAttribute.None);
                                }
                            }
                        }

                        if (methodHandler != null)
                        {
                            CefV8Value current  = handler;
                            var        sections = type.FullName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                            for (int i = 0; i < sections.Length; i++)
                            {
                                var section = sections[i];
                                if (current.GetKeys().Contains(section))
                                {
                                    if (i + 1 == sections.Length)
                                    {
                                        current.SetValue(section, methodHandler, CefV8PropertyAttribute.None);
                                    }
                                    else
                                    {
                                        current = current.GetValue(section);
                                    }
                                }
                                else
                                {
                                    if (i + 1 == sections.Length)
                                    {
                                        current.SetValue(section, methodHandler, CefV8PropertyAttribute.None);
                                    }
                                    else
                                    {
                                        var temp = CefV8Value.CreateObject();
                                        current.SetValue(section, temp, CefV8PropertyAttribute.None);
                                        current = temp;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Example #27
0
        public CefV8Value AsV8Value()
        {
            CefV8Value result = null;

            if (this.Value is IList)
            {
                var v = (IList)this.Value;

                result = CefV8Value.CreateArray(v.Count);

                for (var i = 0; i < v.Count; i++)
                {
                    result.SetValue(i, new JSValue(v[i]).AsV8Value());
                }
            }
            else if (this.Value is Boolean)
            {
                result = CefV8Value.CreateBool((Boolean)this.Value);
            }
            else if (this.Value is DateTime)
            {
                result = CefV8Value.CreateDate((DateTime)this.Value);
            }
            else if (this.Value is Single || this.Value is Double || this.Value is Decimal || this.Value is UInt64 || this.Value is Int64)
            {
                result = CefV8Value.CreateDouble((Double)this.Value);
            }
            else if (this.Value is CefV8Handler)
            {
                result = CefV8Value.CreateFunction(null, (CefV8Handler)this.Value);
            }
            else if (this.Value is SByte || this.Value is Int16 || this.Value is Int32)
            {
                result = CefV8Value.CreateInt((Int32)this.Value);
            }
            else if (this.Value == null)
            {
                result = CefV8Value.CreateNull();
            }
            else if (this.Value is IDictionary)
            {
                var v = (IDictionary)this.Value;

                Debug.Assert(v.Keys.Count == v.Values.Count);

                var vKeys   = new String[v.Keys.Count];
                var vValues = new CefV8Value[v.Values.Count];

                result = CefV8Value.CreateObject(null);

                for (var i = 0; i < vKeys.Length; i++)
                {
                    result.SetValue(vKeys[i], new JSValue(vValues[i]).AsV8Value(), CefV8PropertyAttribute.None);
                }
            }
            else if (this.Value is String)
            {
                result = CefV8Value.CreateString((String)this.Value);
            }
            else if (this.Value is Byte || this.Value is UInt16 || this.Value is UInt32)
            {
                result = CefV8Value.CreateUInt((UInt32)this.Value);
            }

            if (result == null)
            {
                result = CefV8Value.CreateUndefined();
            }

            return(result);
        }
Example #28
0
 private static void SetAttribute(CefV8Value value, string name, CefV8Value propertyValue)
 {
     value.SetValue(name, propertyValue, CefV8PropertyAttribute.ReadOnly | CefV8PropertyAttribute.DontEnum | CefV8PropertyAttribute.DontDelete);
 }