如何使用可以访问 Chromium 中本机代码的自定义方法来扩展 JavaScript API
我正在制作 Chromium 的自定义构建,以便以自定义方式访问语音到文本功能,但不确定如何通过 JavaScript 公开对新本机方法的访问。
在其他版本的 WebKit 中,有像 addToJavaScriptWindowObject (在 Qt 中)这样的方法,但我不确定如何在 Chromium 中执行相同的操作。
I am making a custom build of Chromium to get access to speech to text functionality in a custom way and am not sure how to expose access to a new native method through JavaScript.
In other versions of WebKit there are methods like addToJavaScriptWindowObject (that is in Qt) but I am not sure how to do the same thing in Chromium.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
进程间 API 是一个狭窄的接口,因此基本上具有最少的调用次数。
首先,我想向您指出向跨浏览器 API 添加新功能的记录方法(您可以在列表中搜索“carnitas”以了解更多信息):
https://sites.google.com/a/chromium.org/dev/developers/design-documents/multi-process-architecture/how-to-add-new-features
就我而言,我来了提出一个挂钩现有 API 调用的解决方案,基本上,将 javascript 代码注入到页面中以调用 javascript 的“prompt()”函数进行同步调用(冻结整个选项卡直到完成),和/或“externalHost.postMessage”进行异步调用消息。在浏览器端,我连接这两个调用并处理它们,可以选择返回响应。
您可以在此处查看我为 Berkelium(一个 Chromium 包装器库)编写的代码:
https://github.com/sirikata/berkelium/blob/chromium8/src /WindowImpl.cpp
(搜索所有引用“javascriptCall”的地方,看看我如何挂钩这些 API)如果您愿意修改 render_view_host.cc 或 TabContents,则可以将相同的技术应用于 Chromium 本身。
您应该考虑什么级别的集成对于您的 API 来说是正确/必要的,如果您有兴趣让您的语音转文本功能被 Chrome 代码库接受,那么您最好遵循其他功能的实现方式。
The interprocess API is a narrow interface, so has essentially the minimal number of calls.
First off, I would like to point you to the documented way to add new features to the cross-browser API (you can search for "carnitas" on the list to learn more about this):
https://sites.google.com/a/chromium.org/dev/developers/design-documents/multi-process-architecture/how-to-add-new-features
In my case, I came up with a solution which hooks into existing API calls, basically, injecting javascript code into the page to call javascript's "prompt()" function for synchronous calls (freezing the whole tab until complete), and/or "externalHost.postMessage" for asynchronous messages. On the browser side, I hook into these two calls, and handle them, optionally returning a response.
You can see the code I wrote for Berkelium, a Chromium wrapper library here:
https://github.com/sirikata/berkelium/blob/chromium8/src/WindowImpl.cpp
(search for all the places where "javascriptCall" is referenced, to see how I hook into these APIs) The same technique can be applied to Chromium itself if you are willing to modify render_view_host.cc or the TabContents.
You should consider what level of integration is right/necessary for your API, and if you are interested in getting your speech-to-text functionality accepted into the Chrome codebase, you might be better off following the way that other features are implemented.