基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。
feat: headless webview for darwin platform (#1315)
ErBW_s38c0db51a2e5
diff --git a/lib/pages/webview/webview_controller_impel/webview_apple_controller_impel.dart b/lib/pages/webview/webview_controller_impel/webview_apple_controller_impel.dartindex 57c1ae96..1e61cde0 100644--- a/lib/pages/webview/webview_controller_impel/webview_apple_controller_impel.dart+++ b/lib/pages/webview/webview_controller_impel/webview_apple_controller_impel.dart@@ -1,16 +1,126 @@import 'dart:async';+import 'dart:collection';+import 'package:flutter/material.dart';import 'package:kazumi/utils/utils.dart';import 'package:kazumi/pages/webview/webview_controller.dart';import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';class WebviewAppleItemControllerImpelextends WebviewItemController<PlatformInAppWebViewController> {+ PlatformHeadlessInAppWebView? headlessWebView;Timer? loadingMonitorTimer;bool hasInjectedScripts = false;@overrideFuture<void> init() async {- initEventController.add(true);+ headlessWebView ??= PlatformHeadlessInAppWebView(+ PlatformHeadlessInAppWebViewCreationParams(+ initialUserScripts: UnmodifiableListView<UserScript>([+ UserScript(+ source: '''+ function removeLazyLoading() {+ document.querySelectorAll('iframe[loading="lazy"]').forEach(iframe => {+ console.log('Removing lazy loading from:', iframe.src);+ iframe.removeAttribute('loading');+ });+ }+ if (document.readyState === 'loading') {+ document.addEventListener('DOMContentLoaded', removeLazyLoading);+ } else {+ removeLazyLoading();+ }+ ''',+ injectionTime: UserScriptInjectionTime.AT_DOCUMENT_END,+ ),+ ]),+ initialSettings: InAppWebViewSettings(+ userAgent: Utils.getRandomUA(),+ mediaPlaybackRequiresUserGesture: true,+ useOnLoadResource: false,+ cacheEnabled: false,+ isInspectable: false,+ contentBlockers: [+ ContentBlocker(+ trigger: ContentBlockerTrigger(+ urlFilter: r"^https?://.+?devtools-detector\.js",+ resourceType: [+ ContentBlockerTriggerResourceType.SCRIPT,+ ]),+ action:+ ContentBlockerAction(type: ContentBlockerActionType.BLOCK),+ ),+ ContentBlocker(+ trigger: ContentBlockerTrigger(urlFilter: '.*', resourceType: [+ ContentBlockerTriggerResourceType.IMAGE,+ ]),+ action:+ ContentBlockerAction(type: ContentBlockerActionType.BLOCK),+ ),+ ContentBlocker(+ trigger: ContentBlockerTrigger(+ urlFilter: r"^https?://.+?googleads",+ resourceType: [+ ContentBlockerTriggerResourceType.DOCUMENT,+ ]),+ action:+ ContentBlockerAction(type: ContentBlockerActionType.BLOCK),+ ),+ ContentBlocker(+ trigger: ContentBlockerTrigger(+ urlFilter: r"^https?://.+?googlesyndication\.com",+ resourceType: [+ ContentBlockerTriggerResourceType.DOCUMENT,+ ]),+ action:+ ContentBlockerAction(type: ContentBlockerActionType.BLOCK),+ ),+ ContentBlocker(+ trigger: ContentBlockerTrigger(+ urlFilter: r"^https?://.+?prestrain\.html",+ resourceType: [+ ContentBlockerTriggerResourceType.DOCUMENT,+ ]),+ action:+ ContentBlockerAction(type: ContentBlockerActionType.BLOCK),+ ),+ ContentBlocker(+ trigger: ContentBlockerTrigger(+ urlFilter: r"^https?://.+?prestrain%2Ehtml",+ resourceType: [+ ContentBlockerTriggerResourceType.DOCUMENT,+ ]),+ action:+ ContentBlockerAction(type: ContentBlockerActionType.BLOCK),+ ),+ ContentBlocker(+ trigger: ContentBlockerTrigger(+ urlFilter: r"^https?://.+?adtrafficquality",+ resourceType: [+ ContentBlockerTriggerResourceType.DOCUMENT,+ ]),+ action:+ ContentBlockerAction(type: ContentBlockerActionType.BLOCK),+ ),+ ],+ ),+ onWebViewCreated: (controller) {+ debugPrint('[WebView] Created');+ webviewController = controller;+ initEventController.add(true);+ },+ onLoadStart: (controller, url) {+ logEventController.add('started loading: $url');+ },+ onLoadStop: (controller, url) {+ logEventController.add('loading completed: $url');+ },+ onReceivedError: (controller, request, error) {+ debugPrint(+ '[WebView] Error: ${error.toString()} - Request: ${request.url}');+ },+ ),+ );+ await headlessWebView?.run();}@override@@ -259,7 +369,6 @@ class WebviewAppleItemControllerImpel@overrideFuture<void> unloadPage() async {loadingMonitorTimer?.cancel();- await webviewController!.clearAllCache();await webviewController!.loadUrl(urlRequest: URLRequest(url: WebUri("about:blank")));}@@ -267,5 +376,8 @@ class WebviewAppleItemControllerImpel@overridevoid dispose() {loadingMonitorTimer?.cancel();+ headlessWebView?.dispose();+ headlessWebView = null;+ webviewController = null;}}diff --git a/lib/pages/webview/webview_item_impel/webview_apple_item_impel.dart b/lib/pages/webview/webview_item_impel/webview_apple_item_impel.dartindex e5267453..0de9c7ce 100644--- a/lib/pages/webview/webview_item_impel/webview_apple_item_impel.dart+++ b/lib/pages/webview/webview_item_impel/webview_apple_item_impel.dart@@ -1,9 +1,6 @@-import 'dart:collection';import 'package:flutter/material.dart';import 'package:kazumi/pages/webview/webview_controller.dart';import 'package:flutter_modular/flutter_modular.dart';-import 'package:kazumi/utils/utils.dart';-import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';class WebviewAppleItemImpel extends StatefulWidget {const WebviewAppleItemImpel({super.key});@@ -18,6 +15,7 @@ class _WebviewAppleItemImpelState extends State<WebviewAppleItemImpel> {@overridevoid initState() {super.initState();+ webviewAppleItemController.init();}@override@@ -28,113 +26,16 @@ class _WebviewAppleItemImpelState extends State<WebviewAppleItemImpel> {@overrideWidget build(BuildContext context) {- return PlatformInAppWebViewWidget(PlatformInAppWebViewWidgetCreationParams(- initialUserScripts: UnmodifiableListView<UserScript>([- UserScript(- source: '''- function removeLazyLoading() {- document.querySelectorAll('iframe[loading="lazy"]').forEach(iframe => {- console.log('Removing lazy loading from:', iframe.src);- iframe.removeAttribute('loading');- });- }- if (document.readyState === 'loading') {- document.addEventListener('DOMContentLoaded', removeLazyLoading);- } else {- removeLazyLoading();- }- ''',- injectionTime: UserScriptInjectionTime.AT_DOCUMENT_END,+ return Container(+ height: MediaQuery.of(context).size.width * 9.0 / (16.0),+ width: MediaQuery.of(context).size.width,+ color: Colors.black,+ child: const Center(+ child: Text(+ '此平台不支持Webview规则',+ style: TextStyle(color: Colors.white),),- ]),- initialSettings: InAppWebViewSettings(- userAgent: Utils.getRandomUA(),- mediaPlaybackRequiresUserGesture: true,- useOnLoadResource: false,- cacheEnabled: false,- isInspectable: false,- contentBlockers: [- ContentBlocker(- trigger: ContentBlockerTrigger(- urlFilter: r"^https?://.+?devtools-detector\.js",- resourceType: [- ContentBlockerTriggerResourceType.SCRIPT,- ]),- action: ContentBlockerAction(type: ContentBlockerActionType.BLOCK),- ),- ContentBlocker(- trigger: ContentBlockerTrigger(urlFilter: '.*', resourceType: [- ContentBlockerTriggerResourceType.IMAGE,- ]),- action: ContentBlockerAction(type: ContentBlockerActionType.BLOCK),- ),- ContentBlocker(- trigger: ContentBlockerTrigger(- urlFilter: r"^https?://.+?googleads",- resourceType: [- ContentBlockerTriggerResourceType.DOCUMENT,- ]),- action: ContentBlockerAction(type: ContentBlockerActionType.BLOCK),- ),- ContentBlocker(- trigger: ContentBlockerTrigger(- urlFilter: r"^https?://.+?googlesyndication\.com",- resourceType: [- ContentBlockerTriggerResourceType.DOCUMENT,- ]),- action: ContentBlockerAction(type: ContentBlockerActionType.BLOCK),- ),- ContentBlocker(- trigger: ContentBlockerTrigger(- urlFilter: r"^https?://.+?prestrain\.html",- resourceType: [- ContentBlockerTriggerResourceType.DOCUMENT,- ]),- action: ContentBlockerAction(type: ContentBlockerActionType.BLOCK),- ),- ContentBlocker(- trigger: ContentBlockerTrigger(- urlFilter: r"^https?://.+?prestrain%2Ehtml",- resourceType: [- ContentBlockerTriggerResourceType.DOCUMENT,- ]),- action: ContentBlockerAction(type: ContentBlockerActionType.BLOCK),- ),- ContentBlocker(- trigger: ContentBlockerTrigger(- urlFilter: r"^https?://.+?adtrafficquality",- resourceType: [- ContentBlockerTriggerResourceType.DOCUMENT,- ]),- action: ContentBlockerAction(type: ContentBlockerActionType.BLOCK),- ),- ],),- onWebViewCreated: (controller) {- debugPrint('[WebView] Created');- webviewAppleItemController.webviewController = controller;- webviewAppleItemController.init();- },- onLoadStart: (controller, url) {- webviewAppleItemController.logEventController- .add('started loading: $url');- },- onLoadStop: (controller, url) {- webviewAppleItemController.logEventController- .add('loading completed: $url');- },- onConsoleMessage: (controller, consoleMessage) {- debugPrint(- '[WebView] Console.${consoleMessage.messageLevel}: ${consoleMessage.message}');- },- onLoadResource: (controller, resource) {- debugPrint(- '[WebView] Resource: ${resource.url} - ${resource.initiatorType}');- },- onReceivedError: (controller, request, error) {- debugPrint(- '[WebView] Error: ${error.toString()} - Request: ${request.url}');- },- )).build(context);+ );}}