pichillilorenzo / flutter_inappwebview

A Flutter plugin that allows you to add an inline webview, to use a headless webview, and to open an in-app browser window.
https://inappwebview.dev
Apache License 2.0
3.29k stars 1.62k forks source link

How to use ContentBlocker to block ads? #616

Closed CarmenLexa closed 1 month ago

CarmenLexa commented 3 years ago

I'm trying to use ContentBlocker to block ads but it doesn't work, I try to use onLoadResource but I don't know how to lock the resource before it loads, any ideas?

arcanebrownie commented 3 years ago

I could use some help with this as well, if someone could give an example on how to use contentBlocker, it would be appreciated!

Vanilo commented 3 years ago

Try this:

contentBlockers: [ ContentBlocker( trigger: ContentBlockerTrigger( urlFilter: '.*', resourceType: [ContentBlockerTriggerResourceType.IMAGE], ), action: ContentBlockerAction( type: ContentBlockerActionType.BLOCK, ), ), ],

Change the urlFilter and resoureType as you need.

arcanebrownie commented 3 years ago

Try this:

contentBlockers: [ ContentBlocker( trigger: ContentBlockerTrigger( urlFilter: '.*', resourceType: [ContentBlockerTriggerResourceType.IMAGE], ), action: ContentBlockerAction( type: ContentBlockerActionType.BLOCK, ), ), ],

Change the urlFilter and resoureType as you need.

This doesn't really work since I am trying to load normal images. Also, there are certain ads that are "overlays"? I'm not sure how else to explain it. I was thinking what if I inject a javascript adblocker into the webview? Do you have any thoughts on that @Vanilo?

oiolong commented 3 years ago

Try this: contentBlockers: [ ContentBlocker( trigger: ContentBlockerTrigger( urlFilter: '.*', resourceType: [ContentBlockerTriggerResourceType.IMAGE], ), action: ContentBlockerAction( type: ContentBlockerActionType.BLOCK, ), ), ], Change the urlFilter and resoureType as you need.

This doesn't really work since I am trying to load normal images. Also, there are certain ads that are "overlays"? I'm not sure how else to explain it. I was thinking what if I inject a javascript adblocker into the webview? Do you have any thoughts on that @Vanilo?

It's working .

Try this:

   crossPlatform: InAppWebViewOptions(
    useShouldOverrideUrlLoading: true,
    mediaPlaybackRequiresUserGesture: false,

    contentBlockers: [
        ContentBlocker(
                        trigger: ContentBlockerTrigger( urlFilter: "http://.*aaa.*", resourceType: [ContentBlockerTriggerResourceType.SCRIPT], ),
                        action: ContentBlockerAction( type: ContentBlockerActionType.BLOCK, ), ),
    ],

  ),
  android: AndroidInAppWebViewOptions(
    useHybridComposition: true,
  ),
  ios: IOSInAppWebViewOptions(
    allowsInlineMediaPlayback: true,
  ));

Change the urlFilter and resoureType as you need.

urlFilter ↔ String A regular expression pattern to match the URL against. read / write

If you wanna filter some js named aaa.js

You can try this :

http://.*aaa.*

SamoilovEA commented 3 years ago

@oiolong Hello. How to put LIST from web sites into urlFilter method? "http://.*aaa.", "http://.bbb.", "http://.ccc.*" ?

github-actions[bot] commented 1 month ago

This issue is stale and has been automatically closed because it has been open for more than 365 days with no activity. Please reopen a new issue if you still have it.

github-actions[bot] commented 3 weeks ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.