Skip to content

Commit

Permalink
[webview] refactor the content blockers
Browse files Browse the repository at this point in the history
to be much more efficient and maybe block permission requests
  • Loading branch information
BrightDV committed Dec 1, 2023
1 parent c30adbc commit b618617
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 123 deletions.
138 changes: 55 additions & 83 deletions lib/Screens/MixedNews/rss_feed_article.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,97 +36,69 @@ class RssFeedArticleScreen extends StatefulWidget {
}

class _RssFeedArticleScreenState extends State<RssFeedArticleScreen> {
final List adUrlFilters = [
".*.doubleclick.net/.*",
".*.crashlytics.com/.*",
".*.scorecardresearch.com/.*",
".*.pubmatic.com/.*",
".*.supersonicads.com/.*",
".*.outbrain.com/.*",
".*.googlesyndication.com/.*",
".*.googletagservices.com/.*",
".*.google-analytics.com/.*",
".*.amazon-adsystem.com/.*",
".*.id5-sync.com/.*",
".*.quantcast.com/.*",
".*.adsafeprotected.com/.*",
".*.crwdcntrl.net/.*",
".*.chartbeat.net/.*",
".*.omnitagjs.com/.*",
".*.justpremium.com/.*",
".*.stickyadstv.com/.*",
".*.teads.tv/.*",
".*.taboola.com/.*",
".*.aaxads.com/.*",
".*.googleapis.com/.*",
".*.btloader.com/.*",
".*.avantisvideo.com/.*",
".*.cookiepro.com/.*",
".*.wknd.ai/.*",
".*.webspectator.com/.*",
".*.googletagmanager.com/.*",
".*.id5-sync.com/.*",
".*.revcontent.com/.*",
".*.omnitagjs.com/.*",
];
final List<ContentBlocker> contentBlockers = [];

@override
void initState() {
super.initState();
if (!kIsWeb) {
for (final adUrlFilter in adUrlFilters) {
contentBlockers.add(
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: adUrlFilter,
),
action: ContentBlockerAction(
type: ContentBlockerActionType.BLOCK,
),
contentBlockers.add(
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: ".*",
unlessDomain: [
widget.articleUrl.split('/')[2],
],
resourceType: [
ContentBlockerTriggerResourceType.SCRIPT,
ContentBlockerTriggerResourceType.RAW,
],
),
);
List<String> selectors = [
".banner",
".banners",
".ads",
".ad",
".advert",
".w7e-platform-101",
".adgrid-ad-container",
".ms-apb-inarticle-after-preview-without-sidebar",
".ms-apb-super",
".ms-content_sidebar",
".ms-apb",
".ms-footer_piano-footer",
".advert-banner-container",
".ad-top-margin",
".mv-ad-box",
".onetrust-pc-dark-filter",
".ms-ap",
".ms-hapb",
".ci-ad",
".ot-sdk-container",
".GoogleActiveViewElement",
".widget_text",
".topbanmobile",
".primisslate",
".snackStickyParent",
".region-banner",
".inarticle-wrapper",
];
contentBlockers.add(
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: ".*",
),
action: ContentBlockerAction(
type: ContentBlockerActionType.CSS_DISPLAY_NONE,
selector: selectors.join(', '),
),
action: ContentBlockerAction(
type: ContentBlockerActionType.BLOCK,
),
);
}
),
);
List<String> selectors = [
".banner",
".banners",
".ads",
".ad",
".advert",
".w7e-platform-101",
".adgrid-ad-container",
".ms-apb-inarticle-after-preview-without-sidebar",
".ms-apb-super",
".ms-content_sidebar",
".ms-apb",
".ms-footer_piano-footer",
".advert-banner-container",
".ad-top-margin",
".mv-ad-box",
".onetrust-pc-dark-filter",
".ms-ap",
".ms-hapb",
".ci-ad",
".ot-sdk-container",
".GoogleActiveViewElement",
".widget_text",
".topbanmobile",
".primisslate",
".snackStickyParent",
".region-banner",
".inarticle-wrapper",
];
contentBlockers.add(
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: ".*",
),
action: ContentBlockerAction(
type: ContentBlockerActionType.CSS_DISPLAY_NONE,
selector: selectors.join(', '),
),
),
);
}
}

Expand Down
60 changes: 27 additions & 33 deletions lib/Screens/session_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,47 +46,41 @@ class SessionScreen extends StatefulWidget {
}

class _SessionScreenState extends State<SessionScreen> {
final List adUrlFilters = [
".*.pubtm.com/.*",
".*.pubperf.com/.*",
".*.uniconsent.com/.*",
".*.advertserve.com/.*",
".*.zqtk.net/.*",
];
final List<ContentBlocker> contentBlockers = [];

@override
void initState() {
super.initState();
if (!kIsWeb) {
for (final adUrlFilter in adUrlFilters) {
contentBlockers.add(
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: adUrlFilter,
),
action: ContentBlockerAction(
type: ContentBlockerActionType.BLOCK,
),
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: ".*",
unlessDomain: ["live.planetf1.com"],
resourceType: [
ContentBlockerTriggerResourceType.SCRIPT,
ContentBlockerTriggerResourceType.RAW,
],
),
action: ContentBlockerAction(
type: ContentBlockerActionType.BLOCK,
),
);
List<String> selectors = [
".bs-sticky",
".bs-block",
".unic",
];
contentBlockers.add(
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: ".*",
),
);
List<String> selectors = [
".bs-sticky",
".bs-block",
".unic",
];
contentBlockers.add(
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: ".*",
),
action: ContentBlockerAction(
type: ContentBlockerActionType.CSS_DISPLAY_NONE,
selector: selectors.join(', '),
),
action: ContentBlockerAction(
type: ContentBlockerActionType.CSS_DISPLAY_NONE,
selector: selectors.join(', '),
),
);
}
),
);
}
}

Expand Down
86 changes: 79 additions & 7 deletions lib/Screens/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,90 @@
* Copyright (c) 2022-2023, BrightDV
*/

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:boxbox/Screens/article.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class TestScreen extends StatelessWidget {
const TestScreen({Key? key}) : super(key: key);
class TestScreen extends StatefulWidget {
const TestScreen({super.key});

@override
State<TestScreen> createState() => _TestScreenState();
}

class _TestScreenState extends State<TestScreen> {
final List<ContentBlocker> contentBlockers = [];

@override
void initState() {
super.initState();
contentBlockers.add(
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: ".*",
unlessDomain: ["live.planetf1.com"],
resourceType: [
ContentBlockerTriggerResourceType.SCRIPT,
ContentBlockerTriggerResourceType.RAW,
],
),
action: ContentBlockerAction(
type: ContentBlockerActionType.BLOCK,
),
),
);

List<String> selectors = [
".bs-sticky",
".bs-block",
".unic",
];
contentBlockers.add(
ContentBlocker(
trigger: ContentBlockerTrigger(
urlFilter: ".*",
),
action: ContentBlockerAction(
type: ContentBlockerActionType.CSS_DISPLAY_NONE,
selector: selectors.join(', '),
),
),
);
}

@override
Widget build(BuildContext context) {
return const ArticleScreen(
'gNNm6u0VT260LtqZzgeY6',
'quote',
true,
return Scaffold(
appBar: AppBar(
title: const Text('Test'),
),
body: InAppWebView(
initialUrlRequest: URLRequest(
url: WebUri(
"https://live.planetf1.com/",
),
),
initialSettings: InAppWebViewSettings(
contentBlockers: contentBlockers,
),
gestureRecognizers: {
Factory<VerticalDragGestureRecognizer>(
() => VerticalDragGestureRecognizer(),
),
Factory<HorizontalDragGestureRecognizer>(
() => HorizontalDragGestureRecognizer(),
),
Factory<ScaleGestureRecognizer>(
() => ScaleGestureRecognizer(),
),
},
onPermissionRequest: (controller, permissionRequest) async {
PermissionResponse(
action: PermissionResponseAction.DENY,
);
},
),
);
}
}

0 comments on commit b618617

Please sign in to comment.