在Android中的WebView禁用链接 [英] Disabling links in android WebView

查看:263
本文介绍了在Android中的WebView禁用链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我很绿色编程的世界,所以原谅我,如果我的问题没有间隙的任何部分。 我目前试图使一个应用程序需要连接不能在web视图被执行 - 或在另一个浏览器为那个缘故。我已成功地把下面两个环节,<一个href="http://stackoverflow.com/questions/2219074/in-android-webview-am-i-able-to-modify-a-webpages-dom">In Android的web视图,我能够修改网页的DOM?和<一href="http://blog.clearlyinnovative.com/post/2847924695/titanium-appcelerator-quickie-disable-links-in-webview"相对=nofollow>钛Appcelerator的晨间:在web视图禁用链接在一起,创建以下工作code:

First, I am quite green in the world of programming, so forgive me if my question lacks clearance in any part. I am currently trying to make an app which requires links cannot be executed in the WebView - or in another browser for that sake. I have managed to put the following two links, In Android Webview, am I able to modify a webpage's DOM? and Titanium Appcelerator Quickie: Disable links in Webview, together and create the following working code:

private class WebClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) 
    {       
        view.loadUrl("javascript:document.body.innerHTML = document.body.innerHTML.replace(/<a.*href=/gi,'<a href=\"#\" _url=');");       
    }
}

上面放置了的onCreate-方法之前和被引用在OnCreate法是这样的片段:

The snippet above is placed before the onCreate-method and is referenced in the onCreate-method like this:

    viewer = (WebView) findViewById(R.id.wv_engine);
    viewer.setWebViewClient(new WebViewClient());
    viewer.loadUrl(content);

现在,在code工作......但只有在请求的网页已被加载第二次。我第一次执行的WebView,example.com的所有环节完整,可点击和executedable。使用后退按钮,退出的WebView,并再次输入它呈现全部通过改变禁用链接

Now, the code works... But only after the requested webpage has been loaded a second time. The first time I execute the WebView, example.com has all its links intact, clickable and executedable. Using the backbutton, exiting the WebView and entering it again renders all the links disabled by changing

<a href="http://www.example.com">link</a>

<a href="#" _url="http://www.example.com">link</a>

现在,我一个理论,我不敢肯定有关(我不是程序员)。相信的JavaScript执行减缓,这导致链路操纵从未发生。这就是为什么它的第二次合作,而不是与缓存。我想到那些从阅读<一个href="http://stackoverflow.com/questions/4949963/disabling-caching-cookies-and-everything-else-in-a-webview">this讨论。不过,我不知道如何来纠正它。

Now, I a theory which I am not at all sure about (I am no programmer). I believe the JavaScript is executed to slow, which results in the links-manipulation never happening. That is why it works the second time working instead with with cache. I got those thought from reading this discussion. However I have no idea how to correct it.

所以我的问题有云:我如何才能确保可以被使用没有显示的网站的链接?有没有一种方法,使显示的网站之前,操作发生的呢?

So my question goes: How can I make sure that none of the links of the displayed website can ever be used? Is there a way to make manipulation happen before the site is displayed?

感谢您的时间和精力。这确实是一个了不起的社区;)

Thanks for your time and effort. This is truly an amazing community ;)

  • Krede

推荐答案

我认为这是错误的:

viewer.setWebViewClient(new WebViewClient());

我猜你的意思是这样:

I guess you mean this instead:

viewer.setWebViewClient(new WebClient());

这也解释了为什么在网址第一负载不会被禁用。但我不明白为什么他们被禁止第二次。

This explains why urls are not disabled at 1st load. But I don't understand why they are disabled 2nd time.

此外,我想你应该删除这样的:

Additionally, I think you should remove this:

view.loadUrl(url);

这篇关于在Android中的WebView禁用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆