stringByEvaluatingJavascriptFromString(iOS的方法,什么是Android的相同呢?) [英] stringByEvaluatingJavascriptFromString (iOS method, what is Android equivalent?)

查看:253
本文介绍了stringByEvaluatingJavascriptFromString(iOS的方法,什么是Android的相同呢?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS应用程序,我用

In an iOS app, I used

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

要获得被显示在web视图图像的src目录。我想要做同样为Android。我有什么选择?

To get the src directory of the image that was being displayed on the webView. I want to do the same for Android. What are my options?

基本上的目的是为了获取路径,这样我可以通过电子邮件发送同样的图片...

Basically the intent is to capture the path so that I can email this same picture...

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

这样一来,同样的图像会在用户点击链接,或者其发布至Facebook等。

This way, that same image would be loaded when the user clicks the link, or posts it to facebook etc.

推荐答案

是啊,我在Android中非常怀念这种方法)

Yeah, I miss this method greatly in Android ;)

要执行JavaScript,并得到回应,你可以做如下:

To execute JavaScript and get response you can do as follows:

  1. 定义在code JavaScript回调接口:

  1. Define JavaScript callback interface in your code:

class MyJavaScriptInterface {
    @JavascriptInterface
    public void someCallback(String jsResult) {
         // your code...
    }
}

  • 附加此回调到您的WebView

  • Attach this callback to your WebView

    MyJavaScriptInterface javaInterface = new MyJavaScriptInterface();
    yourWebView.addJavascriptInterface(javaInterface, "HTMLOUT");
    

  • 运行你的JavaScript调用 window.HTMLOUT.someCallback 从脚本:

  • Run your JavaScript calling window.HTMLOUT.someCallback from the script:

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

  • 希望这有助于!

    这篇关于stringByEvaluatingJavascriptFromString(iOS的方法,什么是Android的相同呢?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆