Determine element of url from webview shouldOverRideUrlLoading

后端 未结 2 1950
醉酒成梦
醉酒成梦 2021-01-23 19:07

I am displaying a html in a custom activity with a webview. I am overriding

public boolean shouldOverrideUrlLoading(WebView view, String url) {

相关标签:
2条回答
  • 2021-01-23 19:47

    There is a dirty hack:

    1. Bind some Java object so that it can be called from Javascript with WebView:

      addJavascriptInterface(javaObjectExposed, "JSname")
      
    2. Force execute javascript within an existing page by

      WebView.loadUrl("javascript:window.JSname.passData("some data");"); 
      

    Described here: http://lexandera.com/2009/01/extracting-html-from-a-webview/

    Note: this is a security risk - any JS code in this web page could access/call your binded Java object. Best to pass some one-time cookies to loadUrl() and pass them back your Java object to check that it's your code making the call.

    0 讨论(0)
  • 2021-01-23 19:57

    In addition to @Peter Knego's approach, there is the inverse:

    1. Use addJavascriptInterface(), per his step #1

    2. For your link, use an onClick attribute on your <a> tag to call out some method on the Java object from step #1, where the Java object turns around and loads the URL into the WebView, and supplies whatever sort of identifying information you want.

    Suffice it to say, there's no way to get the information you want from shouldOverrideUrlLoading().

    0 讨论(0)
提交回复
热议问题