简体   繁体   English

stringByEvaluatingJavascriptFromString(iOS方法,相当于Android?)

[英]stringByEvaluatingJavascriptFromString (iOS method, what is Android equivalent?)

In an iOS app, I used 在iOS应用中,我使用了

stringFromJavaScript = [webView stringByEvaluatingJavascriptFromString:@"document.getElementById(\"image\").getAttribute(\"src")"];

To get the src directory of the image that was being displayed on the webView. 获取正在webView上显示的图像的src目录。 I want to do the same for Android. 我想对Android执行相同操作。 What are my options? 我有什么选择?

Basically the intent is to capture the path so that I can email this same picture... 基本上,目的是捕获路径,以便我可以通过电子邮件发送此图片...

ie. 即。

"picture.php?image=%@",stringFromJavascript

This way, that same image would be loaded when the user clicks the link, or posts it to facebook etc. 这样,当用户单击链接或将其发布到facebook等时,将加载相同的图像。

Yeah, I miss this method greatly in Android ;) 是的,我在Android中非常想念这种方法;)

To execute JavaScript and get response you can do as follows: 要执行JavaScript并获得响应,您可以执行以下操作:

  1. Define JavaScript callback interface in your code: 在代码中定义JavaScript回调接口:

     class MyJavaScriptInterface { @JavascriptInterface public void someCallback(String jsResult) { // your code... } } 
  2. Attach this callback to your WebView 将此回调附加到您的WebView

     MyJavaScriptInterface javaInterface = new MyJavaScriptInterface(); yourWebView.addJavascriptInterface(javaInterface, "HTMLOUT"); 
  3. Run your JavaScript calling window.HTMLOUT.someCallback from the script: 运行脚本中的JavaScript调用window.HTMLOUT.someCallback

     yourWebView.loadUrl("javascript:( function () { var resultSrc = document.getElementById(\\"image\\").getAttribute(\\"src\\"); window.HTMLOUT.someCallback(resultSrc); } ) ()"); 

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM