gpt4 book ai didi

delphi - Chromium 嵌入式框架 : Creating an object fails when using "ExecuteFunctionWithContext"

转载 作者:行者123 更新时间:2023-12-03 17:59:39 28 4
gpt4 key购买 nike

概览

我在 Delphi 2009 上使用 Chromium 嵌入式框架 (cef),它是最新版本。

错误

我可以使用 ExecuteFunctionWithContext 成功执行 JavaScript 回调例程,并且可以为其提供参数。但是,当我尝试使用 TCefv8ValueRef.CreateObject(nil); 时,libcef 库中发生了访问冲突

假设

  1. 将参数推送到 JavaScript 回调适用于 TCefv8ValueRef.CreateString 和所有其他 Cefv8Value 类型。
  2. TCefv8ValueRef.CreateObject(nil) 函数在用作 chromium 扩展的返回值时工作正常。 (详见 Delphi CEF 的 /demos/guiclient 演示)。
  3. TChromium 对象保存在主窗体上。

可能的解决方案和思考

  1. 我尝试通过主窗体上的 OnClick 事件使用 TCefv8ValueRef.CreateObject(nil);,这也产生了访问冲突。但是,TCefv8ValueRef.CreateString('test'); 可以正常工作。

如有任何帮助,我们将不胜感激。

最佳答案

我在 C++ 上遇到了同样的问题!我用下面的代码解决了它:

CefRefPtr<CefFrame> frame = browser->GetMainFrame();
CefRefPtr<CefV8Context> v8Context = frame->GetV8Context();
if (v8Context.get() && v8Context->Enter())
{
CefRefPtr<CefV8Value> object = CefV8Value::CreateObject(NULL);
// ExecuteFunctionWithContext and other actions

v8Context->Exit();
}

chromiumembedded 文档包含以下内容:

So you should switch on the right contect before your actions with javascript model. If V8 is not currently inside a context, or if you need to retrieve and store a reference to a context, you can use one of two available CefV8Context static methods. GetCurrentContext() returns the context for the frame that is currently executing JS. GetEnteredContext() returns the context for the frame where JS execution began. For example, if a function in frame1 calls a function in frame2 then the current context will be frame2 and the entered context will be frame1.

Arrays, objects and functions may only be created, modified and, in the case of functions, executed, if V8 is inside a context. If V8 is not inside a context then the application needs to enter a context by calling Enter() and exit the context by calling Exit(). The Enter() and Exit() methods should only be used:

  1. When creating a V8 object, function or array outside of an existing context. For example, when creating a JS object in response to a native menu callback.

  2. When creating a V8 object, function or array in a context other than the current context. For example, if a call originating from frame1 needs to modify the context of frame2.

所以这就是为什么你不能创建对象但能够创建 js 字符串的原因。你也可以看到 the general usage example .

关于delphi - Chromium 嵌入式框架 : Creating an object fails when using "ExecuteFunctionWithContext",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9880217/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com