使用 V8 JavaScript 引擎在没有 web view 的情况下执行 JS lib [英] Use V8 JavaScript engine to execute JS lib without web view

查看:22
本文介绍了使用 V8 JavaScript 引擎在没有 web view 的情况下执行 JS lib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 JavaScript 组件,它负责向服务器发出请求并将结果分派到 UI.通过在 JavaScript 中执行此操作,我可以在多种类型的 UI 中使用我的组件:Android 应用程序、iOS 应用程序、桌面应用程序 (QT)、网络应用程序...

所有这些 UI 都实例化了一个 Web 视图,因此当 UI 加载专用 URL (webview.load("file://myfirstWebPage.html")) 时,我的组件就会启动.>

这第一个网页加载了所有的 JavaScript 组件,当它完成时,UI 能够向 JavaScript 组件发出一些请求,它向服务器发出请求,当它有响应时,它将它分派回客户端(UI Android,UI iOS ...)

这种架构运行良好,但我想知道是否有另一种方法可以在不使用每个客户端上的 Web 视图的情况下加载 JavaScript 组件?

V8 引擎能帮我吗?

解决方案

如果我理解您的问题,您正在寻找一种方法来跨多个平台(iOS、Android 等)执行 JavaScript,而无需使用一个网络视图.该解决方案将是特定于平台的,因为即使是每个平台的底层 WebView 实现也是不同的.

对于Android,只要设备自带V8,你就可以创建一个新的V8 Context通过它的 API 并使用它来执行你的 JavaScript.该设备必须实际附带 V8.这个答案可能会进一步帮助您.

对于使用 JavaScriptCore 的 iOS,iOS7 的最新发展已经允许您加载和运行任意 JavaScript 代码.在此处阅读更多信息.

I am developing a JavaScript component which is responsible for making requests to the server and dispatching results to the UI. By doing this in JavaScript, I am able to use my component in several types of UI: Android app, iOS app, desktop app (QT), web app...

All these UI have instantiated a web view, so my component is started when the UI loads the dedicated URL (webview.load("file://myfirstWebPage.html")).

This first web page loads all the JavaScript components, and when it's done, the UI is able to make some requests to the JavaScript component, which makes a request to the server and when it has the response, it dispatches it back to the client (UI Android, UI iOS ...)

This architecture works fine, but I would like to know if there is another way to load the JavaScript component without using a web view on each client?

Can the V8 engine help me?

解决方案

If I'm understanding your question, you're looking for a way to execute JavaScript across many platforms (iOS, Android, etc.) without the use of a WebView. The solution will be platform-specific, since even the underlying WebView implementations are different for each platform.

For Android, so long as the device ships with V8, you can create a new V8 Context via its API and use that to execute your JavaScript. The device must actually ship with V8. This answer may help you further.

For iOS, which uses JavaScriptCore, recent developments in iOS7 have been made to allow you load and run arbitrary JavaScript code. Read more here.

这篇关于使用 V8 JavaScript 引擎在没有 web view 的情况下执行 JS lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆