Skip to content

Commit 59d5eb7

Browse files
committed
feat: 35-Make the user agent customizable
1 parent 6df9ba6 commit 59d5eb7

8 files changed

Lines changed: 34 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
## 2.3.0
2+
### New Features
3+
* **Custom User Agent**: Set a custom user agent for the webview
4+
* Configure via
5+
```dart
6+
AdBlockerWebview(
7+
adBlockerWebviewController: controller,
8+
shouldBlockAds: true,
9+
url: Uri.parse('https://example.com'),
10+
userAgent: 'MyCustomApp/1.0 (Custom User Agent)',
11+
// ... other parameters
12+
)```
13+
114
## 2.2.0
215
316
**Breaking Changes**

example/lib/browser.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class _BrowserState extends State<Browser> {
3030
child: AdBlockerWebview(
3131
url: Uri.parse(widget.url),
3232
adBlockerWebviewController: widget.controller,
33+
userAgent: 'MyCustomApp/1.0 (Custom User Agent)',
3334
onProgress: (progress) {
3435
setState(() {
3536
_progress = progress;

lib/src/adblocker_webview.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class AdBlockerWebview extends StatefulWidget {
2020
required this.shouldBlockAds,
2121
this.url,
2222
this.initialHtmlData,
23+
this.userAgent,
2324
this.onLoadStart,
2425
this.onLoadFinished,
2526
this.onProgress,
@@ -43,6 +44,10 @@ class AdBlockerWebview extends StatefulWidget {
4344
/// Either this or [url] must be provided, but not both.
4445
final String? initialHtmlData;
4546

47+
/// Custom user agent string for the webview.
48+
/// If not provided, a default user agent will be used based on the platform.
49+
final String? userAgent;
50+
4651
/// Required: The controller for [AdBlockerWebview].
4752
/// See more at [AdBlockerWebviewController].
4853
final AdBlockerWebviewController adBlockerWebviewController;
@@ -199,6 +204,11 @@ class _AdBlockerWebviewState extends State<AdBlockerWebview> {
199204
}
200205

201206
String _getUserAgent() {
207+
// Return custom user agent if provided
208+
if (widget.userAgent != null && widget.userAgent!.isNotEmpty) {
209+
return widget.userAgent!;
210+
}
211+
202212
final osVersion = Platform.operatingSystemVersion;
203213

204214
if (Platform.isAndroid) {

lib/src/adblocker_webview_controller.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ abstract interface class AdBlockerWebviewController
9797
/// Checks if a domain is in the whitelist
9898
bool isAllowedDomain(String urlOrDomain);
9999

100-
101100
/// Returns the set of blocked domains (custom block list)
102101
Set<String> get blockedDomains;
103102

@@ -114,7 +113,6 @@ abstract interface class AdBlockerWebviewController
114113
/// Checks if a domain is in the custom block list
115114
bool isBlockedDomain(String urlOrDomain);
116115

117-
118116
/// Returns the blocking statistics
119117
BlockingStatistics get statistics;
120118

lib/src/adblocker_webview_controller_impl.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ class AdBlockerWebviewControllerImpl implements AdBlockerWebviewController {
118118
return controller.runJavaScript(script);
119119
}
120120

121-
122121
@override
123122
Set<String> get allowedDomains => _adBlockManager.allowedDomains;
124123

@@ -134,7 +133,6 @@ class AdBlockerWebviewControllerImpl implements AdBlockerWebviewController {
134133
bool isAllowedDomain(String urlOrDomain) =>
135134
_adBlockManager.isAllowedDomain(urlOrDomain);
136135

137-
138136
@override
139137
Set<String> get blockedDomains => _adBlockManager.blockedDomains;
140138

lib/src/block_resource_loading.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: lines_longer_than_80_chars, use_raw_strings
2+
13
import 'dart:convert';
24
import 'package:adblocker_manager/adblocker_manager.dart';
35

@@ -23,6 +25,7 @@ String getResourceLoadingBlockerScript(List<ResourceRule> rules) {
2325
}
2426
// Complex rule
2527
// t: types, d: domains, i: important, tp: thirdParty
28+
// ignore: omit_local_variable_types
2629
final Map<String, dynamic> map = {'u': r.url};
2730
if (r.resourceTypes != null) map['t'] = r.resourceTypes;
2831
if (r.domains != null) map['d'] = r.domains;

packages/adblocker_core/lib/src/adblocker_filter_impl.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: lines_longer_than_80_chars
2+
13
import 'package:adblocker_core/src/adblocker_filter.dart';
24
import 'package:adblocker_core/src/lru_cache.dart';
35
import 'package:adblocker_core/src/parser/css_rules_parser.dart';

pubspec.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: adblocker_webview
22
description: A webview for flutter with adblocking capability. It's currently based on Official Flutter Webview Plugin.
33
repository: https://github.com/islamdidarmd/flutter_adblocker_webview
44
issue_tracker: https://github.com/islamdidarmd/flutter_adblocker_webview/issues
5-
version: 2.2.0
5+
version: 2.3.0
66
homepage: https://github.com/islamdidarmd/flutter_adblocker_webview
77

88
environment:
@@ -26,3 +26,7 @@ dev_dependencies:
2626
sdk: flutter
2727
test: ^1.21.0
2828
very_good_analysis: ^5.1.0
29+
30+
platforms:
31+
android:
32+
ios:

0 commit comments

Comments
 (0)