从webview中删除特定元素

时间:2015-11-14 17:36:53

标签: android webview

我正在制作一个供个人使用的网络视图应用,但广告太多了,所以我决定以这种方式将它们搞砸。标题现在是不可见的,但广告仍然可见!禁用javascript删除了所有广告,但我需要验证验证码图片,这只有在启用了javascript时才有效。

public class MainActivity extends Activity {
    private WebView mWebView;

    // fit on screen
    private int getScale() {
        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        int width = display.getWidth();
        Double val = new Double(width) / new Double(width);
        val = val * 100d;
        return val.intValue();
    }

    // fit on screen
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //mWebView.getSettings().setLoadsImagesAutomatically(false);
        mWebView = (WebView) findViewById(R.id.webView1);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new MyWebViewClient());
        // mWebView.loadUrl("http://www.afreesms.com.prx2.unblocksit.es/freesms/");
        mWebView.loadUrl("http://www.afreesms.com/freesms/");
        mWebView.setPadding(0, 0, 0, 0);// fit on screen
        mWebView.setInitialScale(getScale());// fit on screen

        // zoom support
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            // Use the API 11+ calls to disable the controls
            // Use a seperate class to obtain 1.6 compatibility
            new Runnable() {
                public void run() {
                    mWebView.getSettings().setDisplayZoomControls(false);
                }
            }.run();
        } else {
            ZoomButtonsController zoom_controll = null;
            try {
                zoom_controll = (ZoomButtonsController) mWebView.getClass()
                        .getMethod("getZoomButtonsController")
                        .invoke(mWebView, null);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            zoom_controll.getContainer().setVisibility(View.GONE);
        }
        // zoom support

    }

    private class MyWebViewClient extends WebViewClient {

        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
            // Removes element which id = 'mastHead'
            view.loadUrl("javascript:(function() { "
                    + "(elem = document.getElementById('header')).parentNode.removeChild(elem); "
                    + "})()");
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);      
            return true;
        }

        @Override
        // Loading Spinner and status
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            //
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //
        }
    };

1 个答案:

答案 0 :(得分:0)

广告是否已在http://www.afreesms.com/freesms/上启用? 如果是,那么您无法从应用程序中删除它们,因为它直接从网站加载数据。