Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e0771ad
Add support for Flutter 3.27.0
didarul-codes Oct 7, 2024
eb4dea6
Replace InApp webview with webview flutter
didarul-codes Dec 14, 2024
fbc3643
Hide ad elements dynamically
didarul-codes Dec 15, 2024
07e5cfc
Intercept fetch and XHR requests to block ads loading
didarul-codes Dec 15, 2024
57166a0
Update blocking scripts
didarul-codes Dec 17, 2024
39a9b4d
Add advanced parsing of rules
didarul-codes Dec 21, 2024
9c2106f
Improve element hiding
didarul-codes Dec 21, 2024
80ea394
Update user agent to have os version
didarul-codes Dec 22, 2024
0f8f7a6
Update css rules parsing
didarul-codes Dec 22, 2024
4a0411f
Improve processing of css selectors
didarul-codes Dec 22, 2024
3988740
Replace where calls with raw for loop
didarul-codes Dec 23, 2024
5bf9569
Add url resource parser
didarul-codes Dec 29, 2024
0fa1446
Block Url resorces from navigation
didarul-codes Dec 29, 2024
eb83a67
Block resource requests using fetch and xhr
didarul-codes Dec 30, 2024
218b7b2
Add support for Flutter 3.29
didarul-codes Feb 15, 2025
9199c8e
Refactor core package
didarul-codes Feb 15, 2025
98b2411
Create Admob Filter Manager
didarul-codes Feb 16, 2025
c989850
Improve url input page in example app
didarul-codes Feb 17, 2025
6dfaefe
Improve url input page in example app
didarul-codes Feb 17, 2025
edea998
Add tests for core
didarul-codes Feb 21, 2025
11d6160
Move Ablock manager logic to controller
didarul-codes Feb 21, 2025
abad6f9
Update readme
didarul-codes Feb 21, 2025
9a5b351
Promote 2.0.0 beta to prod
didarul-codes Mar 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .fvm/fvm_config.json

This file was deleted.

4 changes: 4 additions & 0 deletions .fvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutter": "3.29.0",
"flavors": {}
}
6 changes: 4 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest

env:
FLUTTER_VERSION: '3.19.5' # Update with your desired default version
FLUTTER_VERSION: '3.29.0' # Update with your desired default version

steps:
- uses: actions/checkout@v4
Expand All @@ -23,4 +23,6 @@ jobs:
run: flutter pub get

- name: Run tests
run: flutter test
run: |
flutter test
cd packages/adblocker_core && flutter test
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ migrate_working_dir/
*.iws
.idea/

#vscode related
.vscode/

#fvm related
.fvm/flutter_sdk

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
Expand All @@ -33,3 +35,6 @@ migrate_working_dir/
build/
.flutter-plugins
.flutter-plugins-dependencies

# FVM Version Cache
.fvm/
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 1.2.0
* Added support for HTML string loading
## 2.0.0-beta
* Added support for easylist and adguard filters
* Added support for resource rules parsing
* Removed third party package dependency and using official webview_flutter package

**Breaking Changes**
* Minimum Supported flutter version is 3.27.1
* Minimum Supported dart version is 3.7.0

## 1.1.2
* Removed redundant isolate uses
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2023, Md Didarul Islam
Copyright (c) 2025, Md Didarul Islam

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
303 changes: 240 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,78 +1,255 @@
[![style: very good analysis](https://img.shields.io/badge/style-very_good_analysis-B22C89.svg)](https://pub.dev/packages/very_good_analysis)

- A webview implementation of in Flutter that blocks most of the ads that appear inside of the webpages
- Current implementation is based on official `flutter_inappwebview` packages. So, the features and limitation of that package
is included
# AdBlocker WebView Flutter

>On iOS the WebView widget is backed by a [WKWebView](https://developer.apple.com/documentation/webkit/wkwebview).
On Android the WebView widget is backed by a [WebView](https://developer.android.com/reference/android/webkit/WebView).
A Flutter WebView implementation that blocks ads and trackers using EasyList and AdGuard filter lists.

| | Android | iOS |
|-------------|----------------|-------|
| **Support** | SDK 19+ or 20+ | 11.0+ |
## Features

## Getting started
Add `adblocker_webview` as a [dependency](https://pub.dev/packages/adblocker_webview/install) in your pubspec.yaml file.
- 🚫 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

## Usage
1. Acquire an instance of [AdBlockerWebviewController](https://pub.dev/documentation/adblocker_webview/latest/adblocker_webview/AdBlockerWebviewController-class.html)
```dart
final _adBlockerWebviewController = AdBlockerWebviewController.instance;
## Getting Started

### Installation

Add this to your `pubspec.yaml`:

```yaml
dependencies:
adblocker_webview: ^1.0.0
```
It's better to warm up the controller before displaying the webview. It's possible to do that by

### Basic Usage

```dart
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
void initState() {
super.initState();
_adBlockerWebviewController.initialize();
/// ... Other code here.
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%'),
);
}
}
```

### Loading HTML Content

```dart
AdBlockerWebview(
initialHtmlData: '<html><body>Hello World!</body></html>',
shouldBlockAds: true,
adBlockerWebviewController: AdBlockerWebviewController.instance,
)
```

2. Add the [AdBlockerWebview](https://pub.dev/documentation/adblocker_webview/latest/adblocker_webview/AdBlockerWebview-class.html) in widget tree
### Navigation Control

```dart
AdBlockerWebview(
url: "Valid url Here",
adBlockerWebviewController: widget.controller,
onProgress: (progress) {
setState(() {
_progress = progress;
});
},
shouldBlockAds: true,
/// Other params if required
);
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!")');
```
Supported params of [AdBlockerWebview](https://pub.dev/documentation/adblocker_webview/latest/adblocker_webview/AdBlockerWebview-class.html]) are:
```dart
const AdBlockerWebview({
required this.adBlockerWebviewController,
required this.shouldBlockAds,
this.url,
this.initialHtmlData,
this.onLoadStart,
this.onLoadFinished,
this.onProgress,
this.onLoadError,
this.onTitleChanged,
this.options,
this.additionalHostsToBlock = const [],
super.key,
}) : assert(
(url == null && initialHtmlData != null) ||
(url != null && initialHtmlData == null),
'Both url and initialHtmlData can not be non null');

## Configuration

The WebView can be configured with various options:

```dart
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
},
);
```
#### Caching
- API response for Ad hosts is cached automatically and no config is required!

### Contribution
Contributions are welcome 😄. Please file an issue [here](https://github.com/islamdidarmd/flutter_adblocker_webview/issues) if you want to include additional feature or found a bug!
#### Guide
1. Create an issue first to make sure your request is not a duplicate one
2. Create a fork of the repository (If it's your first contribution)
3. Make a branch from `develop`
4. Branch name should indicate the contribution type
- `feature/**` for new feature
- `bugfix/**` for a bug fix
5. Raise a PR against the `develop` branch

## Features in Detail

### Ad Blocking
- 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

### Resource Blocking
- Blocks common trackers and unwanted resources
- Early blocking for better performance
- Basic domain-based filtering
- Exception handling for whitelisted domains

### Element Hiding
- Hides common ad containers and placeholders
- CSS-based element hiding
- Basic domain-specific rules support
- Batch processing for better performance

## Migration Guide

### Migrating from 1.2.0 to 2.0.0-beta

#### Breaking Changes

1. **Controller Initialization**
```dart
// 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],
),
);
```

2. **URL Parameter Type**
```dart
// Old (1.2.0)
AdBlockerWebview(
url: "https://example.com",
// ...
)

// New (2.0.0-beta)
AdBlockerWebview(
url: Uri.parse("https://example.com"),
// ...
)
```

3. **Filter Configuration**
```dart
// 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],
),
);
```

4. **Event Handlers**
```dart
// Old (1.2.0)
onTitleChanged: (title) { ... }

// New (2.0.0-beta)
// Use onUrlChanged instead
onUrlChanged: (url) { ... }
```

#### Deprecated Features
- `additionalHostsToBlock` parameter is removed
- Individual controller instances are replaced with singleton
- `onTitleChanged` callback is replaced with `onUrlChanged`

#### New Features
- 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

#### Steps to Migrate
1. Update the package version in `pubspec.yaml`:
```yaml
dependencies:
adblocker_webview: ^2.0.0-beta
```

2. Replace controller initialization with singleton pattern
3. Update URL parameters to use `Uri` instead of `String`
4. Replace deprecated callbacks with new ones
5. Update filter configuration to use `FilterConfig`
6. Test the application thoroughly after migration

## Contributing

We welcome contributions to improve the ad-blocking capabilities! Here's how you can help:

### Getting Started
1. Fork the repository
2. 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
3. Make your changes
4. Write/update tests if needed
5. Update documentation if needed
6. Run tests and ensure they pass
7. Submit a pull request

### Before Submitting
- 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

### Pull Request Process
1. Create an issue first to discuss major changes
2. Update the README.md if needed
3. Update the CHANGELOG.md following semantic versioning
4. The PR will be reviewed by maintainers
5. Once approved, it will be merged

### Code Style
- Follow [Effective Dart](https://dart.dev/guides/language/effective-dart) guidelines
- Use the provided analysis options
- Run `dart format` before committing

## License

This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.
Loading