Search code examples
flutterwebviewflutter-inappwebviewsafari-content-blocker

Problem with contentblocker in appwebview


hi i try block ads using ContentBlocker but but it doesn't work , i try with androidShouldInterceptRequest and it working fine but not available on ios for test i just try block all url contains "voiranime" and i use that regex ".*voiranime.*" but not working

contentBlockers:[
           ContentBlocker(
               trigger: ContentBlockerTrigger(urlFilter: ".*voiranime.*",),
               action: ContentBlockerAction(
                   type: ContentBlockerActionType.BLOCK
               )
           ),
         ],

Solution

  • Android

    InAppWebView(
    initialOptions: InAppWebViewGroupOptions(
        android: AndroidInAppWebViewOptions(
          useShouldInterceptRequest: true,
        ),
    ),
    androidShouldInterceptRequest: (controller, request) async {
      var url = request.url.toString();
      var i = 0;
      while(i<adblock.length){
        if(url.contains(adblock.elementAt(i))){
          return WebResourceResponse();
        }
        i++;
      }
    });
    

    my variable adblock is just list for filter like that

    List<String> ads(){
    List<String> list = [];
    list.add("imasdk.googleapis.com");
    list.add("rtmark.net");
    list.add("offerimage.com");
    list.add("xadsmart.com");
    list.add("cloudfront.net");
    list.add("in-page-push.com");
    return list;}
    

    you can add others if you want

    Ios

    you must use ContentBlocker with regex