stringByEvaluatingJavascriptFromString(iOS的方法,什么是Android的相同呢?)方法、stringByEvaluatingJavascriptFromStr

由网友(The bone [刺骨])分享简介:在iOS应用程序,我用In an iOS app, I used stringFromJavaScript = [webView stringByEvaluatingJavascriptFromString:@"document.getElementById(\"image\").getAttribute(\"sr...

在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:

定义在code JavaScript回调接口:

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); } ) ()");

希望这有助于!

阅读全文

相关推荐

最新文章