如何公开“本机函数”使用 Chromium 和 Delphi 6 在网页中编写 JavaScript?
在 Delphi Chromium Embedded 的帮助下,我已成功将 Chromium 嵌入到我的 Delphi 6 项目中。现在我希望能够执行 Javascript 代码并将结果返回到我的主机 Delphi 应用程序。我当前的方法是调用 ExecuteJavascript() 并使用 Javascript 调用将其结果写入到的 DOM 元素,并在 Delphi 的 TTimer 方法中轮询该元素以检索结果。然而,我读到了有关使用本机函数和 V8 扩展来让 Javascript 调用“回调”到我的 Delphi 代码中作为接收结果的一种方式:
http://magpcss.org/ceforum/viewtopic.php?f=7&t=180
我想尝试一下,我也想知道如何将基于 Delphi 的事件侦听器附加到网页中的 DOM 元素(onblur、onmousedown 等)。我正在寻找一些示例,如果有人知道在哪里可以找到它们,它们将向我展示如何做这两件事。
I have successfully embedded Chromium into my Delphi 6 projects with the help of Delphi Chromium Embedded. Now I want to be able to execute Javascript code and the have results returned to my host Delphi app. My current method is to call ExecuteJavascript() and use a DOM element that the Javascript call writes its results to, and poll that element in a TTimer method from Delphi to retrieve the results. However, I read about using native functions and V8 extensions to have the Javascript call "call back" into my Delphi code as a way to receive results instead:
http://magpcss.org/ceforum/viewtopic.php?f=7&t=180
I would like to try this and I also would like to know how to attach Delphi based event listeners to DOM elements in the web page (onblur, onmousedown, etc.). I am looking for some samples that would show me how to do these two things if anyone knows where to find them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
附加监听器非常简单(仅在旧版本的 CEF 中):
关于直接获取 JavaScript 结果的扩展函数:主干不包含它们(还?)。似乎工作正在进行中。
编辑:
通过扩展摆脱轮询:
您的 JavaScript 代码确实可以使用扩展回调到您的 Delphi 代码。此外,您可以将值从 JavaScript 发送到 Delphi - 这可用于传输结果而无需轮询。
首先在
initialization
部分注册扩展,该扩展将创建一个稍后在回调时使用的 JavaScript 对象:将调用
TMyHandler
的Execute
之后。TMyHandler
定义为用于演示目的的实现现在很简单:
现在要测试从 JavaScript 调用 Delphi,只需执行以下操作:
这应该显示消息框,显示“执行!”。
我从名为 cefclient 的示例中提取了演示脚本,您可以在组件根目录的 \demos\cefclient 文件夹中找到该示例。扩展示例代码有点隐藏,并与其他演示代码混合在一起。但我们特别感兴趣的是
TExtension.Execute
的实现(相当于我的TMyHandler.Execute
)。在那里您可以找到如何确定正在调用哪个函数以及如何传递参数。 (代码链接。 )Attaching listeners is quite easy (only in older versions of CEF):
Regarding the extended functions for getting JavaScript results directly: the trunk doesn't contain them (yet?). Seems to be work in progress.
Edit:
Getting rid of polling via extensions:
It is indeed possible for your JavaScript code to call back into your Delphi code using extensions. Furthermore you can send values from JavaScript to Delphi - this could be used to transfer results without the need to poll.
First in your
initialization
section register the extension, which creates a JavaScript object later to be used when calling back:TMyHandler
'sExecute
will be invoked later.TMyHandler
is defined asThe implementation for demonstration purposes is simple for now:
Now to test calling into Delphi from JavaScript simply do:
This should display the message box saying "Execute!".
I pulled the demo script from a sample named cefclient which you can find in the \demos\cefclient folder in the component root dir. The extension sample code is a bit hidden and mingled with other demo code. But of special interest for us is the implementation of
TExtension.Execute
(the equivalent to myTMyHandler.Execute
). There you can find how to determine which function is being called and how to pass parameters. (Link to the code.)