开发者

Android - Use WebView to evaluate a javascript string and return the value

开发者 https://www.devze.com 2022-12-13 03:08 出处:网络
Given that scripting is not natively supported in Android and wrapping libraries likejavax.script.ScriptEngineinto your app will make it too large, is it possible to send a javascript string to an inv

Given that scripting is not natively supported in Android and wrapping libraries like javax.script.ScriptEngine into your app will make it too large, is it possible to send a javascript string to an invisible WebView and have it evaluate the string 开发者_如何学JAVAand return you the results (another string)?

I want to go this route because I want to save all my scripts to disk so my app can remain small.

Edit

I need Java code to evaluate javascript strings not the other way around. addJavascriptInterface() doesn't help.


Can it be done? Yes, via addJavascriptInterface() and sending the browser a javascript: URL, akin to a bookmarklet.

A far simpler answer, one that will use much less memory and will execute much faster, is to not use Javascript.

0

精彩评论

暂无评论...
验证码 换一张
取 消