A Flutter WebView implementation that blocks ads and trackers using EasyList and AdGuard filter lists.
- 🚫 Basic ad and tracker blocking using EasyList and AdGuard filters
- 🌐 Supports both URL and HTML content loading
- 🔄 Navigation control (back, forward, refresh)
- 📱 User agent strings for Android and iOS
- ⚡ Early resource blocking for better performance
- 🎯 Domain-based filtering and element hiding
- 🔍 Detailed logging of blocked resources
- 💉 Custom JavaScript injection support
Add this to your pubspec.yaml
:
dependencies:
adblocker_webview: ^1.0.0
import 'package:adblocker_webview/adblocker_webview.dart';
// Initialize the controller (preferably in main())
void main() async {
await AdBlockerWebviewController.instance.initialize();
runApp(MyApp());
}
// Use in your widget
class MyWebView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdBlockerWebview(
url: Uri.parse('https://example.com'),
shouldBlockAds: true,
adBlockerWebviewController: AdBlockerWebviewController.instance,
onLoadStart: (url) => print('Started loading: $url'),
onLoadFinished: (url) => print('Finished loading: $url'),
onLoadError: (url, code) => print('Error: $code'),
onProgress: (progress) => print('Progress: $progress%'),
);
}
}
AdBlockerWebview(
initialHtmlData: '<html><body>Hello World!</body></html>',
shouldBlockAds: true,
adBlockerWebviewController: AdBlockerWebviewController.instance,
)
final controller = AdBlockerWebviewController.instance;
// Check if can go back
if (await controller.canGoBack()) {
controller.goBack();
}
// Reload page
controller.reload();
// Execute JavaScript
controller.runJavaScript('console.log("Hello from Flutter!")');
The WebView can be configured with various options:
AdBlockerWebview(
url: Uri.parse('https://example.com'),
shouldBlockAds: true, // Enable/disable ad blocking
adBlockerWebviewController: AdBlockerWebviewController.instance,
onLoadStart: (url) {
// Page started loading
},
onLoadFinished: (url) {
// Page finished loading
},
onProgress: (progress) {
// Loading progress (0-100)
},
onLoadError: (url, code) {
// Handle loading errors
},
onUrlChanged: (url) {
// URL changed
},
);
- Basic support for EasyList and AdGuard filter lists
- Blocks common ad resources before they load
- Hides ad elements using CSS rules
- Supports exception rules for whitelisting
- Blocks common trackers and unwanted resources
- Early blocking for better performance
- Basic domain-based filtering
- Exception handling for whitelisted domains
- Hides common ad containers and placeholders
- CSS-based element hiding
- Basic domain-specific rules support
- Batch processing for better performance
-
Controller Initialization
// Old (1.2.0) final controller = AdBlockerWebviewController(); await controller.initialize(); // New (2.0.0-beta) await AdBlockerWebviewController.instance.initialize( FilterConfig( filterTypes: [FilterType.easyList, FilterType.adGuard], ), );
-
URL Parameter Type
// Old (1.2.0) AdBlockerWebview( url: "https://example.com", // ... ) // New (2.0.0-beta) AdBlockerWebview( url: Uri.parse("https://example.com"), // ... )
-
Filter Configuration
// Old (1.2.0) AdBlockerWebview( //.. other params additionalHostsToBlock: ['ads.example.com'], ); // New (2.0.0-beta) // Use FilterConfig for configuration await AdBlockerWebviewController.instance.initialize( FilterConfig( filterTypes: [FilterType.easyList, FilterType.adGuard], ), );
-
Event Handlers
// Old (1.2.0) onTitleChanged: (title) { ... } // New (2.0.0-beta) // Use onUrlChanged instead onUrlChanged: (url) { ... }
additionalHostsToBlock
parameter is removed- Individual controller instances are replaced with singleton
onTitleChanged
callback is replaced withonUrlChanged
- Singleton controller pattern for better resource management
- Structured filter configuration using
FilterConfig
- Improved type safety with
Uri
for URLs - Enhanced filter list parsing and management
- Better performance through early resource blocking
-
Update the package version in
pubspec.yaml
:dependencies: adblocker_webview: ^2.0.0-beta
-
Replace controller initialization with singleton pattern
-
Update URL parameters to use
Uri
instead ofString
-
Replace deprecated callbacks with new ones
-
Update filter configuration to use
FilterConfig
-
Test the application thoroughly after migration
We welcome contributions to improve the ad-blocking capabilities! Here's how you can help:
- Fork the repository
- Create a new branch from
main
for your feature/fix- Use
feature/
prefix for new features - Use
fix/
prefix for bug fixes - Use
docs/
prefix for documentation changes
- Use
- Make your changes
- Write/update tests if needed
- Update documentation if needed
- Run tests and ensure they pass
- Submit a pull request
- Check that your code follows our style guide (see analysis badge)
- Write clear commit messages
- Include tests for new features
- Update documentation if needed
- Verify all tests pass
- Create an issue first to discuss major changes
- Update the README.md if needed
- Update the CHANGELOG.md following semantic versioning
- The PR will be reviewed by maintainers
- Once approved, it will be merged
- Follow Effective Dart guidelines
- Use the provided analysis options
- Run
dart format
before committing
This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.