获取WebView中显示的链接的“链接文本”

时间:2014-06-13 10:32:41

标签: android google-chrome android-webview android-menu

我有Android WebView,它显示一些链接:
<a href="http://link1.html">Link1Text</a>
<a href="http://link2.html">Link2Text</a>
现在我想检索 Link1Text 和当我长按这些链接时, Link2Text 。我在代码中实现了 contextMenu ,我可以使用 HitTestResult getExtra()成功获取链接网址http://link1.htmlhttp://link2.html方法,但是我如何获得那些链接文本?周五,我需要这些链接文本来实现&#34;复制链接文本&#34; contextMenu中的选项。

2 个答案:

答案 0 :(得分:1)

要获取achor链接的文本,请执行以下操作:

I。通过evaluateJavascript在WebViewClient的onPageFinished()回调中将一个touchstart侦听器连接到每个网页。像:

        //Javascripts to evaluate in onPageFinished

        const w=window;
        w.addEventListener('touchstart',wrappedOnDownFunc);
        function wrappedOnDownFunc(e){
            if(e.touches.length==1){
                w._touchtarget = e.touches[0].target;
            }
            console.log('hey touched something ' +w._touchtarget);
        }

请注意,我们已经保存了触摸目标。

II。然后为网络视图实现OnLongClicklisenter。长按链接对象时,请再次使用evaluateJavascript

            @Override
            public boolean onLongClick(View v) {
                WebView.HitTestResult result = ((WebView)v).getHitTestResult();
                if (null == result) return false;
                int type = result.getType();
                switch (type) {
                    case WebView.HitTestResult.SRC_ANCHOR_TYPE:
                        if(result.getExtra()!=null){
                            ((WebView)v).evaluateJavascript("window._touchtarget?window._touchtarget.innerText:''", new ValueCallback<String>() {
                                @Override
                                public void onReceiveValue(String value) {
                                    System.out.println("hey received link text : "+value);
                                }
                            });
                        }
                    return true;
                }
                return false;
            }

此外,我们甚至可以选择选择anchor元素的文本!实际上,这是当您长按标签时三星浏览器提供的选项之一。

要实现这一目标,我们仍然需要记录的touch target。此外,我们需要2种新的javascript方法:

        function selectTouchtarget(){
            var tt = w._touchtarget;
            if(tt){
                w._touchtarget_href = tt.getAttribute("href");
                tt.removeAttribute("href");
                var sel = w.getSelection();
                var range = document.createRange();
                range.selectNodeContents(tt);
                sel.removeAllRanges();
                sel.addRange(range);
            }
        }
        function restoreTouchtarget(){
            var tt = w._touchtarget;
            if(tt){
                tt.setAttribute("href", w._touchtarget_href);
            }
        }

最后在onLongClick侦听器中,我们以编程方式设置选择,触发操作菜单栏并恢复innerText的已移除href属性,而不仅仅是获取touch target

    case WebViewmy.HitTestResult.SRC_ANCHOR_TYPE:
        if(result.getExtra()!=null){
            WebViewmy mWebView = ((WebViewmy)v);
            mWebView.evaluateJavascript("selectTouchtarget()", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    /* bring in action mode by a fake click on the programmatically  selected text. */
                    MotionEvent te = MotionEvent.obtain(0,0,KeyEvent.ACTION_DOWN,mWebView.lastX,mWebView.lastY,0);
                    mWebView.dispatchTouchEvent(te);
                    te.setAction(KeyEvent.ACTION_UP);
                    mWebView.dispatchTouchEvent(te);
                    te.recycle();
                    //if it's not delayed for a while or the href attribute is not removed, then the above code would click into
                    //  the anchor element instead of select it's text.
                    /* restore href attribute */
                    mWebView.postDelayed(() -> mWebView.evaluateJavascript("restoreTouchtarget()", null), 100);
                }
            });
        }
    return true;

就我而言,我将WebView扩展为WebViewmy,以在onTouchEvent方法中记录最后触摸的位置,lastX和lastY。

答案 1 :(得分:0)

不幸的是,没有明确的官方方法可以做到这一点。虽然有两个API(selectText和copySelection)正在等待API审批,但这可能有助于实现这一目标,但目前尚不可用。

相关问题