【问题标题】:JavaScript/Jsoup - Get background-image url from "i class"JavaScript/Jsoup - 从“i class”获取背景图片 url
【发布时间】:2017-01-26 20:24:57
【问题描述】:

所以我在我的应用程序的 webview 中加载了一个网页,并且有一个 HTML i class="img profimage",它有一个背景图片 url,这就是我的目标......

我想使用 JavaScript 或使用 Jsoup 来获得该目标...

到目前为止,我对 Jsoup 的尝试是这样的:

public class GetImage extends AsyncTask<String, String, String> {

protected String doInBackground(String... strings) {

Document document = null;

try {

document = Jsoup.connect(url).get();

} catch (IOException e) {

e.printStackTrace();

}

String temp = document.select("i.img.profpic").first().getElementsByAttribute("style").toString();

return temp.substring(temp.indexOf("(") + 1, temp.indexOf(")"));

}

 

protected void onPostExecute(String result) {

Log.d("ChatScreen", result);

Toast.makeText(ChatScreen.this, result, LENGTH_SHORT).show();

}

}

但是我得到了 NPE... 我不知道如何使用 JavaScript 获取 i 类 的背景图像 url...我可以在网上找到大量示例,了解如何使用 idsdiv 获取它

页面的HTML结构类似于:

<div ....>
<div ....>
<i class="img profimage" style="background-image: url("url here");"/>
</div>
</div>

【问题讨论】:

  • 你需要图片网址作为字符串吗?
  • @HarishKamboj 是的,因为我想使用 Glide 将它加载到 ImageView 中,所以我需要将 url 作为字符串传递给 Glide 方法..

标签: javascript android jsoup


【解决方案1】:

检查this Answer 可能会起作用,但我没有尝试过。 JS 函数在 evaluateJavascript() 中编写如下,它将读取 style 属性,然后您可以使用 substring 方法获取 URL。

webview.evaluateJavascript(
        "(function() { return (document.getElementsByTagName('i')[index].getAttribute('style')); })();",
         new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String html) {
                Log.d("HTML", html); 
                // now use substring to get url.....
            }
    });

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    参考this 答案,您可以尝试类似下面的方法(假设它是类profimage 的唯一/第一个元素,否则更改索引​​,即[n],其中n 是索引)。或者,只需给元素一个唯一的id 并使用document.getElementById('imgid') 并删除[0]。将它包装在一个函数中,以防你想在特定事件上调用它。

    function getUrl() {
        var img = document.getElementsByClass('profimage'),
        style = img[0].currentStyle || window.getComputedStyle(img[0], false),
        bi = style.backgroundImage.slice(5, -2);
        return bi;
     }
    

    切片将删除 URL 本身之前的 url(" 和之后的 ")。请注意,这会返回完整的 url。

    如果这不起作用,您可以尝试style = img.style, 来代替上面代码中较长的表达式,因为背景图像是在元素本身中添加了内联css (&lt;i ... style="background-image: url("url here");"/&gt;)。这将返回内联样式的 url,在本例中为 url here

    【讨论】:

    • 嘿,现在我检查了网页的 HTML,我对“background-image”标签有误,但它很简单“背景:#222222 url(“url here”)”...我是对不起...你能提供代码吗?
    • @Asama 应该大体相同,只需将backgroundImage 改为background,并使用不同的参数:slice(13, -2)
    • 并且背景标签在页面的 style.css 文件中,所以它没有嵌入到行中......还是一样吗?我尝试了不同的方法,包括你的方法,它们在 jsfiddle 中运行良好,但在 webview 中返回 null ......我不知道是什么问题......
    • @Asama 是的,我的答案中的代码(与先前评论的更改)应该可以工作(即currentStyle),但您可以尝试使用style,正如我在代码下方建议的那样。我看到WebView reference 说 JavaScript 默认是禁用的,也许你必须启用它(假设你有,只是检查)?
    • 您也可以尝试删除|| window.getComputedStyle()img[0].currentStyle || 看看是否有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多