Make Webview's auto links visible

南楼画角 提交于 2019-12-08 22:35:41
icyrock.com

Given this:

it still doesn't seem to be a way to do this from Java directly. One thing that might work is to write some JavaScript code and run it after page is loaded, e.g. as given here:

Here's an example of a similar thing:

where the idea is to disable links. You may be able to use a similar approach to add some CSS, including underlining. A couple of other SOqs / links that might help:

Hope this helps.

Here is some JS code which can be injected to linkify phone numbers, emails and urls:

            function linkify() {
                linkifyTexts(linkifyPhoneNumbers);
                linkifyTexts(linkifyEmails);
                linkifyTexts(linkifyWebAddresses1);
                linkifyTexts(linkifyWebAddresses2);
            }
            function linkifyPhoneNumbers(text) {
                text = text.replace(/\b\+?[0-9\-]+\*?\b/g, '<a href="tel:$&">$&</a>');
                return text;
            }
            function linkifyEmails(text) {
                text = text.replace(/(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim, '<a href="mailto:$1">$1</a>');
                return text;
            }
            function linkifyWebAddresses1(text) {
                text = text.replace(/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim, '<a href="$1" target="_blank">$1</a>');
                return text;
            }
            function linkifyWebAddresses2(text) {
                text = text.replace(/(^|[^\/])(www\.[\S]+(\b|$))/gim, '$1<a href="http://$2" target="_blank">$2</a>');
                return text;
            }

            var linkifyTexts = function(replaceFunc)
            {
                var tNodes = [];
                getTextNodes(document.body,false,tNodes,false);                              
                var l = tNodes.length;
                while(l--)
                {
                    wrapNode(tNodes[l], replaceFunc);
                }
            }
            function getTextNodes(node, includeWhitespaceNodes,textNodes,match) {
                if (node.nodeType == 3) {
                    if (includeWhitespaceNodes || !/^\s*$/.test(node.nodeValue)) {
                        if(match){
                            if(match.test(node.nodeValue))
                                textNodes.push(node);
                        }
                        else {
                            textNodes.push(node);
                        }
                    }
                } else {
                    for (var i = 0, len = node.childNodes.length; i < len; ++i) {
                        var subnode = node.childNodes[i];
                        if (subnode.nodeName != "A") {
                            getTextNodes(subnode,includeWhitespaceNodes,textNodes,match);
                        }
                    }
                }

            }
            function wrapNode(n, replaceFunc) {
                var temp = document.createElement('div');
                if(n.data)
                    temp.innerHTML = replaceFunc(n.data);
                else{
                    //whatever
                }
                while (temp.firstChild) {
                    n.parentNode.insertBefore(temp.firstChild,n);

                }
                n.parentNode.removeChild(n);

            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!