Home > OS >  Add adblock in webview
Add adblock in webview

Time:05-06

I'm trying to add adblock in my fragment, and when I'm tryed inisialize webview I got underline this on new AdBlockerWebView.init(this).initializeWebView(webView); line

package com.example.bottomnavigationview;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.monstertechno.adblocker.AdBlockerWebView;
import com.monstertechno.adblocker.util.AdBlocker;

public class Fragment2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment2,container,false);
        View v = inflater.inflate(R.layout.fragment2,container,false);
        WebView webView = (WebView)v.findViewById(R.id.webView);
        new AdBlockerWebView.init(this).initializeWebView(webView);
        webView.setWebViewClient(new Browser_home());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://cyber.sports.ru/dota2/match/");
        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK){
                        if(webView != null){
                            if (webView.canGoBack()){
                                webView.goBack();
                            } else {
                                getActivity().onBackPressed();
                            }
                        }
                    }
                }
                return true;
            }
        });
        return v;
    }

where I need put line new AdBlockerWebView.init(this).initializeWebView(webView); to fix my error?

CodePudding user response:

what param takes init(...) method? I bet its Context, so you can use this in Activity, Service and other classes extending Context. but Fragment isn't extending Context, don't have own, is using parent Activitys context, so in there you may use e.g. requireContext() or webView.getContext() as param for init method

new AdBlockerWebView.init(webView.getContext()).initializeWebView(webView);

assuming you are using THIS library

  • Related