简体   繁体   English

Android WebView:确定<a>target =“_ blank”</a>

[英]Android WebView: Determine <a> target= “_ blank”

is it possible to check if the user has clicked on a html link with the target="_blank". 是否可以检查用户是否已点击带有target =“_ blank”的html链接。

What I want to do is to display htlm in my App in a WebView, but start "external" links in the android default browser. 我想要做的是在我的应用程序中在WebView中显示htlm,但在android默认浏览器中启动“外部”链接。 A "external" link is for me a link with target="_blank". “外部”链接对我来说是一个target =“_ blank”的链接。 All other links should be handled in the webview. 所有其他链接应在webview中处理。

So for example: the user clicks on a link like this in my WebView: 例如:用户在我的WebView中点击这样的链接:

<a href="http://www.google.com" target="_blank">new window</a>

and then I want to open the given url in the android browser. 然后我想在Android浏览器中打开给定的URL。

I tried it with shouldOverrideUrlLoading(), but at this point I can't determine, if the target was "_blank" or a normal link (without target). 我用shouldOverrideUrlLoading()尝试了它,但此时我无法确定目标是“_blank”还是普通链接(没有目标)。

I tried also setSupportMultipleWindows(true); 我也试过setSupportMultipleWindows(true); in combination with onCreateWindow(), but in this callback I can't get the url. 与onCreateWindow()结合使用,但在此回调中我无法获取url。

I cant change the HTML that is displayed, so I can't use a JavaScript Bridge with addJavascriptInterface() 我无法更改显示的HTML,因此我无法使用带有addJavascriptInterface()的JavaScript Bridge

What else can I do? 我还可以做些什么? Any other idea? 还有其他想法吗?

I just solved this issue myself. 我自己刚刚解决了这个问题。 Here is how I fixed it. 这是我修复它的方法。

mWebView.setWebChromeClient(new WebChromeListener() {
    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
        WebView newWebView = new WebView(view.getContext());
        newWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(browserIntent);
                return true;
            }
        });
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(newWebView);
        resultMsg.sendToTarget();
        return true;
    }
});

You can do the following: ( ugly but will work) 您可以执行以下操作:( 丑陋但可以工作)

inside onPageFinished(), inject a javascript code fragment into the page which does something like: 在onPageFinished()内部,将一个javascript代码片段注入页面,其中包含:

  1. iterates on all elements with a target=_blank attribute 使用target = _blank属性迭代所有元素
  2. change the href for those elements to external://[original href] 将这些元素的href更改为external:// [original href]

If the site uses jquery it should be easy. 如果该网站使用jquery,它应该很容易。 If not, you can still do it using standard DOM Javascript. 如果没有,您仍然可以使用标准DOM Javascript来完成。

on your shouldOverrideUrlLoading(), look for those external://* links and open them externally. 在你的shouldOverrideUrlLoading()上,查找那些external:// *链接并在外部打开它们。

In order to inject the javascript , do the following: 要注入javascript,请执行以下操作:

webView.loadUrl("javascript:(function() { PLACE YOUR JS CODE HERE })()");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM