This is possible. To execute JavaScript and get response you can do as follows:
Define JavaScript callback interface in your code:
class MyJavaScriptInterface {
public void someCallback(String jsResult) {
// this will be passed the local storage name variable
}
}
Attach this callback to your WebView:
MyJavaScriptInterface javaInterface = new MyJavaScriptInterface();
webView.addJavascriptInterface(javaInterface, "HTMLOUT");
Run your JavaScript calling window.HTMLOUT.someCallback from the script:
webView.loadUrl("javascript:( function () { var name = window.localStorage['name']; window.HTMLOUT.someCallback(name); } ) ()");
Note - window.localStorage['name']
is the same as window.localStorage.getItem('name')
This post here on stackoverflow helped me
You might need to use super.webView.addJavascriptInterface
or super.addJavascriptInterface
to add the interface. You might need to use super.webView.loadUrl
or super.loadUrl
to invoke this. It all depends on where you are going to be calling these from.