protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context) { if (frame.IsMain) { // Retrieve the context's window object and install the "cefReportDocumentSize" function // used to tell the node about the document size. using (var window = context.GetGlobal()) { var handler = new CustomCallbackHandler(browser, frame); var reportDocumentSizeFunc = CefV8Value.CreateFunction(CustomCallbackHandler.ReportDocumentSize, handler); window.SetValue(CustomCallbackHandler.ReportDocumentSize, reportDocumentSizeFunc, CefV8PropertyAttribute.None); } } base.OnContextCreated(browser, frame, context); }
protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context) { if (frame.IsMain) { // Retrieve the context's window object and install the "vvvvReportDocumentSize" function // used to tell the node about the document size as well as the "vvvvSend" function // used to tell the node about variables computed inside the frame. using (var window = context.GetGlobal()) { var handler = new CustomCallbackHandler(browser, frame); var reportDocumentSizeFunc = CefV8Value.CreateFunction(CustomCallbackHandler.ReportDocumentSize, handler); window.SetValue(CustomCallbackHandler.ReportDocumentSize, reportDocumentSizeFunc); window.SetValue("vvvvQuery", CefV8Value.CreateFunction("vvvvQuery", new DelegatingHandler((name, obj, args) => { const string argForm = "{ request: 'NAME', arguments: {}, onSuccess: function(response) {}, onError: function(error_message) {} }"; string invalidArg = $"vvvvQuery expects one argument of the form {argForm}"; if (args.Length != 1) { throw new Exception($"vvvvQuery expects one argument"); } var x = args[0]; if (x is null || !x.IsObject) { throw new Exception($"The argument must be of the form {argForm}"); } var requestValue = x.GetValue("request"); if (requestValue.IsUndefined) { throw new Exception($"The request entry is missing"); } if (!requestValue.IsString) { throw new Exception($"The request must be a string"); } var onSuccessValue = x.GetValue("onSuccess"); if (!onSuccessValue.IsUndefined && !onSuccessValue.IsFunction) { throw new Exception($"The onSuccess entry must be a function"); } var onErrorValue = x.GetValue("onError"); if (!onErrorValue.IsUndefined && !onErrorValue.IsFunction) { throw new Exception($"The onError entry must be a function"); } var request = requestValue.GetStringValue(); var id = queryCount++; if (queries.TryAdd((request, id), (context, onSuccessValue, onErrorValue))) { using var message = CefProcessMessage.Create("query-request"); message.Arguments.SetString(0, request); message.Arguments.SetInt(1, id); message.Arguments.SetValue(2, ToValue(x.GetValue("arguments"))); frame.SendProcessMessage(CefProcessId.Browser, message); } return(default);