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

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

问题描述

首先,我在编程的世界很绿,所以原谅我,如果我的问题在任何部分缺乏通关。
我目前正试图制作一个需要链接的应用程序无法在WebView中执行,或者在另一个浏览器中执行链接。我设法放了以下两个链接:在Android Webview中,我是否可以修改网页的DOM? Titanium Appcelerator Quickie:禁用Webview中的链接,并创建以下工作代码:

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

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

上面的代码片段放在onCreate方法之前,在onCreate方法中被引用,如下所示:

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

现在,代码工作...但只有在请求的网页被加载了第二次之后。我第一次执行WebView,example.com的所有链接都是完整的,可点击的和可执行的。使用反向按钮,退出WebView并重新输入它将通过更改

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

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

现在,我是一个不能确定的理论(我不是程序员)。我相信JavaScript被执行缓慢,这导致链接操纵永远不会发生。这就是为什么它第二次工作,而不是使用缓存。我从阅读这个讨论中得到了这些想法。但是我不知道如何纠正它。



所以我的问题是:如何确保显示的网站的任何链接都不能被使用?有没有办法在网站显示之前进行操作?



感谢您的时间和精力。这是真正的一个惊人的社区;)




  • Krede


解决方案

我认为这是错误的:

  viewer.setWebViewClient(new WebViewClient ()); 

我想你的意思是:

  viewer.setWebViewClient(new WebClient()); 

这解释了为什么在第一次加载时不禁用网址。但是我不明白为什么他们第二次被禁用。



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

  view.loadUrl(URL); 


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=');");       
    }
}

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);

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>

to

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

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

解决方案

I think this is wrong:

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天全站免登陆