I need to get some properties of an html page which is loaded in a webview. Something similar to what is here: stringByEvaluatingJavascriptFromString (iOS method, what is Android equivalent?)
So I have a JavaScriptInterface:
class MyJavaScriptInterface {
@android.webkit.JavascriptInterface
public void jsCallback(String jsResult) {
// your code...
Log.d("Debug", jsResult);
}
}
ant then here is the JS I use to get the string I need:
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webView.loadUrl("javascript:( "
+ "function () { "
+ "var htmltag = document.getElementsByTagName(\"html\");"
+ "var dataStr = htmltag[0].getAttribute(\"some-data-property-i-need\"); "
+ "console.log(window.HTMLOUT);"
+ "window.HTMLOUT.jsCallback(dataStr); } ) ()");
now in the console I see this traces:
06-02 01:14:06.409: V/CallNative fired:(26789): objc://domReady
06-02 01:14:06.464: I/Web Console(26789): undefined at null:1
06-02 01:14:06.480: E/Web Console(26789): Uncaught TypeError: Cannot call method 'jsCallback' of undefined at null:1
and I have no idea why "window.HTMLOUT" is undefined. Please note that it was working at a point and then I modified a bit the JS part but I still cannot figure it out what could be the problem.