Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat: better captcha webview abstraction (#1882)

ErBW_s9b46238e86b6
diff --git a/lib/webview/captcha/captcha_webview_controller.dart b/lib/webview/captcha/captcha_webview_controller.dart
index 3cb4091a..803b23d8 100644
--- a/lib/webview/captcha/captcha_webview_controller.dart
+++ b/lib/webview/captcha/captcha_webview_controller.dart
@@ -5,7 +5,19 @@ import 'package:kazumi/webview/captcha/impl/captcha_webview_inappwebview_impl.da
import 'package:kazumi/webview/captcha/impl/captcha_webview_windows_impl.dart';
import 'package:kazumi/webview/captcha/impl/captcha_webview_linux_impl.dart';
-abstract class CaptchaWebviewController {
+abstract class CaptchaWebviewController<T> {
+ /// Webview controller
+ T? webviewController;
+
+ /// For type-1 (captcha image), whether a captcha image has been detected,
+ /// used to determine if verification is successful after page navigation
+ bool captchaWasFound = false;
+
+ /// For type-2 (auto-click button), we set this flag when the button click is triggered.
+ /// Then on page navigation or DOM change, if this flag is set,
+ /// we can confirm verification success without relying solely on captcha disappearance.
+ bool buttonWasClicked = false;
+
final StreamController<String> captchaImageFoundController =
StreamController<String>.broadcast();
final StreamController<void> captchaDisappearedController =
diff --git a/lib/webview/captcha/impl/captcha_webview_inappwebview_impl.dart b/lib/webview/captcha/impl/captcha_webview_inappwebview_impl.dart
index d5f65403..66526881 100644
--- a/lib/webview/captcha/impl/captcha_webview_inappwebview_impl.dart
+++ b/lib/webview/captcha/impl/captcha_webview_inappwebview_impl.dart
@@ -5,16 +5,12 @@ import 'package:kazumi/utils/logger.dart';
import 'package:kazumi/webview/captcha/captcha_webview_controller.dart';
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
-class CaptchaWebviewInAppWebviewImpl extends CaptchaWebviewController {
+class CaptchaWebviewInAppWebviewImpl
+ extends CaptchaWebviewController<PlatformInAppWebViewController> {
PlatformHeadlessInAppWebView? _headlessWebView;
- PlatformInAppWebViewController? _webviewController;
bool _handlersRegistered = false;
String _currentCaptchaImageXpath = '';
String _currentInputXpath = '';
- /// For type-1 (captcha image), whether a captcha image has been detected, used to determine if verification is successful after page navigation
- bool _captchaWasFound = false;
- /// For type-2 (auto-click button), we set this flag when the button click is triggered. Then on page navigation or DOM change, if this flag is set, we can confirm verification success without relying solely on captcha disappearance.
- bool _buttonWasClicked = false;
@override
Future<void> init() async {
@@ -31,7 +27,7 @@ class CaptchaWebviewInAppWebviewImpl extends CaptchaWebviewController {
),
onWebViewCreated: (controller) {
logEventController.add('[Captcha WebView] Created');
- _webviewController = controller;
+ webviewController = controller;
initEventController.add(true);
},
onLoadStart: (controller, url) {
@@ -39,9 +35,9 @@ class CaptchaWebviewInAppWebviewImpl extends CaptchaWebviewController {
},
onLoadStop: (controller, url) {
logEventController.add('[Captcha WebView] Load stop: $url');
- if (_buttonWasClicked && !captchaDisappearedController.isClosed) {
+ if (buttonWasClicked && !captchaDisappearedController.isClosed) {
KazumiLogger().i('[Captcha WebView] Button click → page navigated, verification done');
- _buttonWasClicked = false;
+ buttonWasClicked = false;
captchaDisappearedController.add(null);
}
},
@@ -57,52 +53,52 @@ class CaptchaWebviewInAppWebviewImpl extends CaptchaWebviewController {
void _registerHandlers() {
if (_handlersRegistered) return;
- _webviewController?.addJavaScriptHandler(
+ webviewController?.addJavaScriptHandler(
handlerName: 'CaptchaImageBridge',
callback: (args) {
final src = args.isNotEmpty ? args[0].toString() : '';
logEventController.add('[Captcha WebView] Captcha image found: $src');
if (src.isNotEmpty && !captchaImageFoundController.isClosed) {
- _captchaWasFound = true;
+ captchaWasFound = true;
captchaImageFoundController.add(src);
}
},
);
- _webviewController?.addJavaScriptHandler(
+ webviewController?.addJavaScriptHandler(
handlerName: 'CaptchaStatusBridge',
callback: (args) {
final status = args.isNotEmpty ? args[0].toString() : '';
logEventController.add('[Captcha WebView JS] Page captcha status: $status');
- if (status == 'absent' && _captchaWasFound &&
+ if (status == 'absent' && captchaWasFound &&
!captchaDisappearedController.isClosed) {
KazumiLogger().i('[Captcha WebView] Captcha gone after navigation (StatusBridge)');
- _captchaWasFound = false;
+ captchaWasFound = false;
captchaDisappearedController.add(null);
}
},
);
- _webviewController?.addJavaScriptHandler(
+ webviewController?.addJavaScriptHandler(
handlerName: 'CaptchaGoneBridge',
callback: (args) {
logEventController.add('[Captcha WebView] Captcha image disappeared');
- _buttonWasClicked = false;
+ buttonWasClicked = false;
if (!captchaDisappearedController.isClosed) {
captchaDisappearedController.add(null);
}
},
);
- _webviewController?.addJavaScriptHandler(
+ webviewController?.addJavaScriptHandler(
handlerName: 'ButtonClickedBridge',
callback: (args) {
logEventController.add('[Captcha WebView] Button clicked flag set');
- _buttonWasClicked = true;
+ buttonWasClicked = true;
},
);
- _webviewController?.addJavaScriptHandler(
+ webviewController?.addJavaScriptHandler(
handlerName: 'CaptchaLogBridge',
callback: (args) {
if (args.isNotEmpty) {
@@ -124,7 +120,7 @@ class CaptchaWebviewInAppWebviewImpl extends CaptchaWebviewController {
_currentInputXpath.replaceAll('\\', '\\\\').replaceAll("'", "\\'");
// Remove any previously injected captcha script before adding a fresh one.
- await _webviewController?.removeAllUserScripts();
+ await webviewController?.removeAllUserScripts();
const String scriptTemplate = """
window.flutter_inappwebview.callHandler('CaptchaLogBridge',
@@ -244,7 +240,7 @@ if (!_checkForCaptcha()) {
final script = scriptTemplate
.replaceAll('{XPATH}', escapedXpath)
.replaceAll('{INPUT_XPATH}', escapedInputXpath);
- await _webviewController?.addUserScripts(
+ await webviewController?.addUserScripts(
userScripts: [
UserScript(
source: script,
@@ -258,8 +254,8 @@ if (!_checkForCaptcha()) {
Future<void> loadPage(String url, String captchaXpath, {String? inputXpath}) async {
_currentCaptchaImageXpath = captchaXpath;
_currentInputXpath = inputXpath ?? '';
- _captchaWasFound = false;
- _buttonWasClicked = false;
+ captchaWasFound = false;
+ buttonWasClicked = false;
_registerHandlers();
await _addCaptchaUserScript();
try {
@@ -267,15 +263,15 @@ if (!_checkForCaptcha()) {
.deleteAllCookies();
logEventController.add('[Captcha WebView] Cookies cleared before load');
} catch (_) {}
- await _webviewController
+ await webviewController
?.loadUrl(urlRequest: URLRequest(url: WebUri(url)));
}
@override
Future<void> loadPageForButtonClick(String url, String buttonXpath) async {
_currentCaptchaImageXpath = ''; // disable captcha-image script on navigation
- _captchaWasFound = false;
- _buttonWasClicked = false;
+ captchaWasFound = false;
+ buttonWasClicked = false;
_registerHandlers();
await _addButtonClickUserScript(buttonXpath);
try {
@@ -283,14 +279,14 @@ if (!_checkForCaptcha()) {
.deleteAllCookies();
logEventController.add('[Captcha WebView] Cookies cleared before load');
} catch (_) {}
- await _webviewController
+ await webviewController
?.loadUrl(urlRequest: URLRequest(url: WebUri(url)));
}
Future<void> _addButtonClickUserScript(String buttonXpath) async {
final escapedXpath =
buttonXpath.replaceAll('\\', '\\\\').replaceAll("'", "\\'");
- await _webviewController?.removeAllUserScripts();
+ await webviewController?.removeAllUserScripts();
const String scriptTemplate = """
try { window.flutter_inappwebview.callHandler('CaptchaLogBridge',
@@ -343,7 +339,7 @@ if (!_checkAndClick()) {
""";
final script = scriptTemplate.replaceAll('{XPATH}', escapedXpath);
- await _webviewController?.addUserScripts(
+ await webviewController?.addUserScripts(
userScripts: [
UserScript(
source: script,
@@ -394,7 +390,7 @@ if (!_checkAndClick()) {
}
})();
''';
- await _webviewController?.evaluateJavascript(source: script);
+ await webviewController?.evaluateJavascript(source: script);
}
@override
@@ -414,7 +410,7 @@ if (!_checkAndClick()) {
@override
Future<void> unloadPage() async {
try {
- await _webviewController
+ await webviewController
?.loadUrl(urlRequest: URLRequest(url: WebUri('about:blank')));
} catch (_) {}
}
@@ -423,8 +419,8 @@ if (!_checkAndClick()) {
void dispose() {
_currentCaptchaImageXpath = '';
_currentInputXpath = '';
- _captchaWasFound = false;
- _buttonWasClicked = false;
+ captchaWasFound = false;
+ buttonWasClicked = false;
_handlersRegistered = false;
try {
PlatformCookieManager(const PlatformCookieManagerCreationParams())
@@ -438,6 +434,6 @@ if (!_checkAndClick()) {
} catch (_) {}
_headlessWebView?.dispose();
_headlessWebView = null;
- _webviewController = null;
+ webviewController = null;
}
}
diff --git a/lib/webview/captcha/impl/captcha_webview_linux_impl.dart b/lib/webview/captcha/impl/captcha_webview_linux_impl.dart
index 7ce11f29..0d0cfdf0 100644
--- a/lib/webview/captcha/impl/captcha_webview_linux_impl.dart
+++ b/lib/webview/captcha/impl/captcha_webview_linux_impl.dart
@@ -7,20 +7,16 @@ import 'package:kazumi/utils/storage.dart';
import 'package:kazumi/utils/proxy_utils.dart';
import 'package:kazumi/webview/captcha/captcha_webview_controller.dart';
-class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
- Webview? _webviewController;
+class CaptchaWebviewLinuxImpl
+ extends CaptchaWebviewController<Webview> {
VoidCallback? _navigationListener;
String _currentCaptchaImageXpath = '';
- /// For type-1 (captcha image), whether a captcha image has been detected, used to determine if verification is successful after page navigation
- bool _captchaWasFound = false;
String _buttonXpath = '';
- /// For type-2 (auto-click button), we set this flag when the button click is triggered. Then on page navigation or DOM change, if this flag is set, we can confirm verification success without relying solely on captcha disappearance.
- bool _buttonWasClicked = false;
@override
Future<void> init() async {
final proxyConfig = _getProxyConfiguration();
- _webviewController ??= await WebviewWindow.create(
+ webviewController ??= await WebviewWindow.create(
configuration: CreateConfiguration(
headless: true,
proxy: proxyConfig,
@@ -48,20 +44,20 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
}
void _initMessageBridge() {
- _webviewController?.addOnWebMessageReceivedCallback((message) async {
+ webviewController?.addOnWebMessageReceivedCallback((message) async {
final msg = message.toString();
logEventController.add('[Captcha WebView] WM: $msg');
if (msg.startsWith('captchaImage:')) {
final src = msg.replaceFirst('captchaImage:', '');
if (src.isNotEmpty && !captchaImageFoundController.isClosed) {
- _captchaWasFound = true;
+ captchaWasFound = true;
captchaImageFoundController.add(src);
}
} else if (msg.startsWith('buttonClicked:')) {
- _buttonWasClicked = true;
+ buttonWasClicked = true;
logEventController.add('[Captcha WebView] Button clicked flag set');
} else if (msg.startsWith('captchaGone:')) {
- _buttonWasClicked = false;
+ buttonWasClicked = false;
if (!captchaDisappearedController.isClosed) {
captchaDisappearedController.add(null);
}
@@ -77,11 +73,11 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
_onNavigationInject();
_onNavigationCompletion();
};
- _webviewController?.isNavigating.addListener(_navigationListener!);
+ webviewController?.isNavigating.addListener(_navigationListener!);
}
Future<void> _onNavigationInject() async {
- if (_webviewController?.isNavigating.value == false) {
+ if (webviewController?.isNavigating.value == false) {
logEventController.add('[Captcha WebView] Navigation completed');
if (_currentCaptchaImageXpath.isNotEmpty) {
await _injectCaptchaScript();
@@ -92,33 +88,33 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
}
Future<void> _onNavigationCompletion() async {
- if (_webviewController?.isNavigating.value == false) {
+ if (webviewController?.isNavigating.value == false) {
// Type-1: captcha image was seen; check if it has disappeared.
- if (_captchaWasFound) {
+ if (captchaWasFound) {
final present = await _isCaptchaPresent();
if (!present && !captchaDisappearedController.isClosed) {
logEventController
.add('[Captcha WebView] Captcha gone after navigation');
- _captchaWasFound = false;
+ captchaWasFound = false;
captchaDisappearedController.add(null);
}
}
// Type-2: button was clicked; page navigation confirms verification.
- if (_buttonWasClicked && !captchaDisappearedController.isClosed) {
+ if (buttonWasClicked && !captchaDisappearedController.isClosed) {
logEventController.add(
'[Captcha WebView] Button click and page navigated, verification done');
- _buttonWasClicked = false;
+ buttonWasClicked = false;
captchaDisappearedController.add(null);
}
}
}
Future<bool> _isCaptchaPresent() async {
- if (_currentCaptchaImageXpath.isEmpty || _webviewController == null) return false;
+ if (_currentCaptchaImageXpath.isEmpty || webviewController == null) return false;
final escaped =
_currentCaptchaImageXpath.replaceAll('\\', '\\\\').replaceAll("'", "\\'");
try {
- final result = await _webviewController!.evaluateJavaScript('''
+ final result = await webviewController!.evaluateJavaScript('''
(function() {
try {
var r = document.evaluate('$escaped', document, null,
@@ -220,7 +216,7 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
''';
try {
- await _webviewController?.evaluateJavaScript(script);
+ await webviewController?.evaluateJavaScript(script);
} catch (e) {
KazumiLogger().e('[Captcha WebView] inject script error: $e');
}
@@ -230,18 +226,18 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
Future<void> loadPage(String url, String captchaXpath, {String? inputXpath}) async {
_currentCaptchaImageXpath = captchaXpath;
_buttonXpath = '';
- _buttonWasClicked = false;
- _captchaWasFound = false;
- _webviewController?.launch(url);
+ buttonWasClicked = false;
+ captchaWasFound = false;
+ webviewController?.launch(url);
}
@override
Future<void> loadPageForButtonClick(String url, String buttonXpath) async {
_currentCaptchaImageXpath = '';
_buttonXpath = buttonXpath;
- _buttonWasClicked = false;
- _captchaWasFound = false;
- _webviewController?.launch(url);
+ buttonWasClicked = false;
+ captchaWasFound = false;
+ webviewController?.launch(url);
}
Future<void> _injectButtonClickScript(String buttonXpath) async {
@@ -298,7 +294,7 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
})();
''';
try {
- await _webviewController?.evaluateJavaScript(script);
+ await webviewController?.evaluateJavaScript(script);
} catch (e) {
KazumiLogger().e('[Captcha WebView] injectButtonClickScript error: $e');
}
@@ -346,7 +342,7 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
})();
''';
try {
- await _webviewController?.evaluateJavaScript(script);
+ await webviewController?.evaluateJavaScript(script);
} catch (e) {
KazumiLogger().e('[Captcha WebView] submitCaptchaInteract error: $e');
}
@@ -355,7 +351,7 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
@override
Future<String> getCookieString(String pageUrl) async {
try {
- final cookies = await _webviewController?.getAllCookies() ?? [];
+ final cookies = await webviewController?.getAllCookies() ?? [];
final cookieString =
cookies.map((c) => '${c.name}=${c.value}').join('; ');
logEventController
@@ -369,18 +365,18 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
@override
Future<void> unloadPage() async {
- _webviewController?.launch('about:blank');
+ webviewController?.launch('about:blank');
}
@override
void dispose() {
_currentCaptchaImageXpath = '';
_buttonXpath = '';
- _buttonWasClicked = false;
- _captchaWasFound = false;
+ buttonWasClicked = false;
+ captchaWasFound = false;
if (_navigationListener != null) {
try {
- _webviewController?.isNavigating.removeListener(_navigationListener!);
+ webviewController?.isNavigating.removeListener(_navigationListener!);
} catch (_) {}
_navigationListener = null;
}
@@ -390,7 +386,7 @@ class CaptchaWebviewLinuxImpl extends CaptchaWebviewController {
initEventController.close();
logEventController.close();
} catch (_) {}
- _webviewController?.close();
- _webviewController = null;
+ webviewController?.close();
+ webviewController = null;
}
}
diff --git a/lib/webview/captcha/impl/captcha_webview_windows_impl.dart b/lib/webview/captcha/impl/captcha_webview_windows_impl.dart
index 45c24d73..de0ad013 100644
--- a/lib/webview/captcha/impl/captcha_webview_windows_impl.dart
+++ b/lib/webview/captcha/impl/captcha_webview_windows_impl.dart
@@ -6,17 +6,14 @@ import 'package:kazumi/utils/storage.dart';
import 'package:kazumi/utils/proxy_utils.dart';
import 'package:kazumi/webview/captcha/captcha_webview_controller.dart';
-class CaptchaWebviewWindowsImpl extends CaptchaWebviewController {
+class CaptchaWebviewWindowsImpl
+ extends CaptchaWebviewController<WebviewController> {
HeadlessWebview? _headlessWebview;
final List<StreamSubscription> _subscriptions = [];
String _currentCaptchaImageXpath = '';
String _currentInputXpath = '';
String _currentPageUrl = '';
- /// For type-1 (captcha image), whether a captcha image has been detected, used to determine if verification is successful after page navigation
- bool _captchaWasFound = false;
String _buttonXpath = '';
- /// For type-2 (auto-click button), we set this flag when the button click is triggered. Then on page navigation or DOM change, if this flag is set, we can confirm verification success without relying solely on captcha disappearance.
- bool _buttonWasClicked = false;
@override
Future<void> init() async {
@@ -50,19 +47,19 @@ class CaptchaWebviewWindowsImpl extends CaptchaWebviewController {
_subscriptions.add(
_headlessWebview!.loadingState.listen((state) async {
if (state == LoadingState.navigationCompleted) {
- if (_captchaWasFound) {
+ if (captchaWasFound) {
final present = await _isCaptchaPresent();
if (!present && !captchaDisappearedController.isClosed) {
logEventController
.add('[Captcha WebView] Captcha gone after navigation');
- _captchaWasFound = false;
+ captchaWasFound = false;
captchaDisappearedController.add(null);
}
}
- if (_buttonWasClicked && !captchaDisappearedController.isClosed) {
+ if (buttonWasClicked && !captchaDisappearedController.isClosed) {
logEventController
.add('[Captcha WebView] Button click → page navigated, verification done');
- _buttonWasClicked = false;
+ buttonWasClicked = false;
captchaDisappearedController.add(null);
}
}
@@ -78,14 +75,14 @@ class CaptchaWebviewWindowsImpl extends CaptchaWebviewController {
if (msg.startsWith('captchaImage:')) {
final src = msg.replaceFirst('captchaImage:', '');
if (src.isNotEmpty && !captchaImageFoundController.isClosed) {
- _captchaWasFound = true;
+ captchaWasFound = true;
captchaImageFoundController.add(src);
}
} else if (msg.startsWith('buttonClicked:')) {
- _buttonWasClicked = true;
+ buttonWasClicked = true;
logEventController.add('[Captcha WebView] Button clicked flag set');
} else if (msg.startsWith('captchaGone:')) {
- _buttonWasClicked = false;
+ buttonWasClicked = false;
if (!captchaDisappearedController.isClosed) {
captchaDisappearedController.add(null);
}
@@ -245,9 +242,9 @@ class CaptchaWebviewWindowsImpl extends CaptchaWebviewController {
_currentCaptchaImageXpath = captchaXpath;
_currentInputXpath = inputXpath ?? '';
_buttonXpath = '';
- _buttonWasClicked = false;
+ buttonWasClicked = false;
_currentPageUrl = url;
- _captchaWasFound = false;
+ captchaWasFound = false;
await _headlessWebview?.loadUrl(url);
}
@@ -255,9 +252,9 @@ class CaptchaWebviewWindowsImpl extends CaptchaWebviewController {
Future<void> loadPageForButtonClick(String url, String buttonXpath) async {
_currentCaptchaImageXpath = '';
_buttonXpath = buttonXpath;
- _buttonWasClicked = false;
+ buttonWasClicked = false;
_currentPageUrl = url;
- _captchaWasFound = false;
+ captchaWasFound = false;
await _headlessWebview?.loadUrl(url);
}
@@ -394,7 +391,7 @@ class CaptchaWebviewWindowsImpl extends CaptchaWebviewController {
_currentCaptchaImageXpath = '';
_currentInputXpath = '';
_buttonXpath = '';
- _buttonWasClicked = false;
+ buttonWasClicked = false;
_currentPageUrl = '';
for (final s in _subscriptions) {
try {