zoukankan      html  css  js  c++  java
  • 当WebView加载的是非HTML网页内容时,如何获取它的内容?

    在iOS平台中,我们直接使用webview加载一段JS代码即可:

    NSString *myText = [webView stringByEvaluatingJavaScriptFromString:@";document.documentElement.textContent"];
    

     Swift:

    webView.stringByEvaluatingJavaScriptFromString("document.documentElement.textContent")
    

    注:在webViewDidFinishLoad中调用这段代码。

    在Android中,调用JS代码的方式在KitKat之前是通过loadUrl()方法来调用,之后是通过evaluateJavascript(String script, ValueCallback<String> resultCallback),这两个方法的调用都是没有返回值的,因此不能像iOS一样直接获取,但是有一个小技巧:

    1.在onPageFinished中调用:

    webView.loadUrl("javascript:alert(document.documentElement.textContent)");
    

    2.接着在onJsAlert中处理即可,参数message就是你要的数据了。

  • 相关阅读:
    ant design pro梳理
    JSON.stringify()
    数组小细节
    js this细节
    策略模式解决if-else过多
    使用useState的赋值函数异步更新问题
    Hook
    React Api
    Intent
    树的非递归遍历
  • 原文地址:https://www.cnblogs.com/emmet7life/p/5355960.html
Copyright © 2011-2022 走看看