Customization options for the Browser Ad-Filtering Solution.
Available APIs
You can control the ad-filtering behavior via any of these entry points:
The C++ class FilteringConfiguration,
The Java class org.chromium.components.adblock.FilteringConfiguration
The Browser Extension API defined and documented in chrome/common/extensions/api/eyeo_filtering_private.idl
API requirements
Android OS
Instance of a BrowserContextHandle required
Desktop OS (Windows, Linux, MacOS)
The SHA1 hash of your extension ID must be added to the allowlist in chrome/common/extensions/api/_permission_features.json
Works on any platform
Instance of a BrowserContext required
API changes more frequently than the Java and JavaScript APIs
Capabilities
Toggle ad filtering
To toggle ad filtering call setEnabled with the appropriate bool value:
To query if ad filtering is enabled call isEnabled.
The following example disables ad filtering:
importorg.chromium.components.adblock.FilteringConfiguration;// Creates "adblock" configuration if does not exist yet, returns a valid handle.FilteringConfiguration adblockConfiguration =FilteringConfiguration.createConfiguration("adblock", browserContextHandle);adblockConfiguration.isEnabled(); // trueadblockConfiguration.setEnabled(false);adblockConfiguration.isEnabled(); // false
You can toggle Acceptable Ads by adding or removing a filter list with the Acceptable Ads URL. The following example:
checks whether Acceptable Ads are enabled
disables them
verifies that Acceptable Ads are disabled
importorg.chromium.components.adblock.FilteringConfiguration;// Creates "adblock" configuration if does not exist yet, returns a valid handle.FilteringConfiguration adblockConfiguration =FilteringConfiguration.createConfiguration("adblock", browserContextHandle);adblockConfiguration.getFilterLists().contains(FilteringConfiguration.getAcceptableAdsUrl()); // trueadblockConfiguration.removeFilterList(FilteringConfiguration.getAcceptableAdsUrl());adblockConfiguration.getFilterLists().contains(FilteringConfiguration.getAcceptableAdsUrl()); // false
Use addAllowedDomain to stop filtering ads on a specific domain, and removeAllowedDomain to resume.
getAllowedDomains returns a list of allowed domains.
importorg.chromium.components.adblock.FilteringConfiguration;// Creates "adblock" configuration if does not exist yet, returns a valid handle.FilteringConfiguration adblockConfiguration =FilteringConfiguration.createConfiguration("adblock", browserContextHandle);adblockConfiguration.addAllowedDomain("example.com");adblockConfiguration.getAllowedDomains(); // ["example.com"]adblockConfiguration.removeAllowedDomain("example.com");adblockConfiguration.getAllowedDomains(); // []
Note: Pass a domain ('example.com') as an argument, not a URL ('http://www.example.com/page.html').
Add/Remove custom filters
Use addCustomFilter to add and removeCustomFilter to remove a single filter.
To get the list of custom filters added use getCustomFilters.
The following code snippet installs a new filter:
importorg.chromium.components.adblock.FilteringConfiguration;// Creates "adblock" configuration if does not exist yet, returns a valid handle.FilteringConfiguration adblockConfiguration =FilteringConfiguration.createConfiguration("adblock", browserContextHandle);adblockConfiguration.addCustomFilter("example_domain##.example_selector");adblockConfiguration.getCustomFilters(); // ["example_domain##.example_selector"]adblockConfiguration.removeCustomFilter("example_domain##.example_selector");adblockConfiguration.getCustomFilters(); // []
There is an option to receive events when some resource is blocked or allowed. Typically, this is needed to implement a counter in the UI. Please do not subscribe if you are not going to consume these notifications, as it has a small performance penalty.