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

1

8

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 and 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.

Escuage answered 1/12, 2009 at 15:59 Comment(1)
How is the javascript going to going to see your program in order to act upon it? Just executing scripts in a vacuum isn't going to do much good.Darciedarcy
N
6

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.

Noddle answered 1/12, 2009 at 18:31 Comment(2)
I don't think addJavascriptInterface() will return a String valueEscuage
addJavascriptInterface() lets Javascript code call Java code, and Javascript should be able to pass a String parameter to said Java code.Noddle

© 2022 - 2024 — McMap. All rights reserved.