Skip to content

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

refactor: better webview log for Android and Apple platforms (#1188)

KotaHv87169860a78c
diff --git a/lib/pages/webview/webview_controller_impel/webview_android_controller_impel.dart b/lib/pages/webview/webview_controller_impel/webview_android_controller_impel.dart
index 4c267479..dbdf011c 100644
--- a/lib/pages/webview/webview_controller_impel/webview_android_controller_impel.dart
+++ b/lib/pages/webview/webview_controller_impel/webview_android_controller_impel.dart
@@ -1,5 +1,4 @@
import 'dart:async';
-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';
@@ -52,8 +51,19 @@ class WebviewAndroidItemControllerImpel
}
void addJavaScriptHandlers(bool useNativePlayer, bool useLegacyParser) {
+ logEventController.add('Adding LogBridge handler');
+ webviewController?.addJavaScriptHandler(
+ handlerName: 'LogBridge',
+ callback: (args) {
+ String message = args[0].toString();
+ if (message.contains('about:blank')) {
+ return;
+ }
+ logEventController.add(message);
+ });
+
if (!useNativePlayer) {
- debugPrint('[WebView] Adding IframeRedirectBridge handler');
+ logEventController.add('Adding IframeRedirectBridge handler');
webviewController?.addJavaScriptHandler(
handlerName: 'IframeRedirectBridge',
callback: (args) {
@@ -65,7 +75,7 @@ class WebviewAndroidItemControllerImpel
});
});
} else if (useLegacyParser) {
- debugPrint('[WebView] Adding JSBridgeDebug handler');
+ logEventController.add('Adding JSBridgeDebug handler');
webviewController?.addJavaScriptHandler(
handlerName: 'JSBridgeDebug',
callback: (args) {
@@ -94,7 +104,7 @@ class WebviewAndroidItemControllerImpel
}
});
} else {
- debugPrint('[WebView] Adding VideoBridgeDebug handler');
+ logEventController.add('Adding VideoBridgeDebug handler');
webviewController?.addJavaScriptHandler(
handlerName: 'VideoBridgeDebug',
callback: (args) {
@@ -120,10 +130,11 @@ class WebviewAndroidItemControllerImpel
final List<UserScript> scripts = [];
if (useLegacyParser) {
- debugPrint('[WebView] Adding JSBridgeDebug user script');
+ logEventController.add('Adding JSBridgeDebug UserScript');
const String jsBridgeDebugScript = """
+ window.flutter_inappwebview.callHandler('LogBridge', 'JSBridgeDebug script loaded: ' + window.location.href);
function processIframeElement(iframe) {
- window.flutter_inappwebview.callHandler('JSBridgeDebug', 'Processing iframe element');
+ window.flutter_inappwebview.callHandler('LogBridge', 'Processing iframe element');
let src = iframe.getAttribute('src');
if (src) {
window.flutter_inappwebview.callHandler('JSBridgeDebug', src);
@@ -131,7 +142,7 @@ class WebviewAndroidItemControllerImpel
}
const _observer = new MutationObserver((mutations) => {
- window.flutter_inappwebview.callHandler('JSBridgeDebug', 'Scanning for iframes');
+ window.flutter_inappwebview.callHandler('LogBridge', 'Scanning for iframes...');
mutations.forEach(mutation => {
if (mutation.type === 'attributes' && mutation.target.nodeName === 'IFRAME') {
processIframeElement(mutation.target);
@@ -158,15 +169,16 @@ class WebviewAndroidItemControllerImpel
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START,
));
} else {
- debugPrint('[WebView] Adding VideoBridgeDebug user script');
+ logEventController.add('Adding VideoBridgeDebug UserScripts');
const String blobParserScript = """
+ window.flutter_inappwebview.callHandler('LogBridge', 'BlobParser script loaded: ' + window.location.href);
const _r_text = window.Response.prototype.text;
window.Response.prototype.text = function () {
return new Promise((resolve, reject) => {
_r_text.call(this).then((text) => {
resolve(text);
if (text.trim().startsWith("#EXTM3U")) {
- console.log('M3U8 source found:', this.url);
+ window.flutter_inappwebview.callHandler('LogBridge', 'M3U8 source found: ' + this.url);
window.flutter_inappwebview.callHandler('VideoBridgeDebug', this.url);
}
}).catch(reject);
@@ -179,7 +191,7 @@ class WebviewAndroidItemControllerImpel
try {
let content = this.responseText;
if (content.trim().startsWith("#EXTM3U")) {
- console.log('M3U8 source found:', args[1]);
+ window.flutter_inappwebview.callHandler('LogBridge', 'M3U8 source found: ' + args[1]);
window.flutter_inappwebview.callHandler('VideoBridgeDebug', args[1]);
};
} catch {}
@@ -189,8 +201,9 @@ class WebviewAndroidItemControllerImpel
""";
const String videoTagParserScript = """
+ window.flutter_inappwebview.callHandler('LogBridge', 'VideoTagParser script loaded: ' + window.location.href);
const _observer = new MutationObserver((mutations) => {
- window.flutter_inappwebview.callHandler('VideoBridgeDebug', 'Scanning for video elements');
+ window.flutter_inappwebview.callHandler('LogBridge', 'Scanning for video elements...');
for (const mutation of mutations) {
if (mutation.type === "attributes" && mutation.target.nodeName === "VIDEO") {
if (processVideoElement(mutation.target)) return;
@@ -209,11 +222,11 @@ class WebviewAndroidItemControllerImpel
}
});
function processVideoElement(video) {
- window.flutter_inappwebview.callHandler('VideoBridgeDebug', 'Scanning video element for source URL');
+ window.flutter_inappwebview.callHandler('LogBridge', 'Scanning video element for source URL');
let src = video.getAttribute('src');
if (src && src.trim() !== '' && !src.startsWith('blob:') && !src.includes('googleads')) {
_observer.disconnect();
- console.log('VIDEO source found:', src);
+ window.flutter_inappwebview.callHandler('LogBridge', 'VIDEO source found: ' + src);
window.flutter_inappwebview.callHandler('VideoBridgeDebug', src);
return true;
}
@@ -222,7 +235,7 @@ class WebviewAndroidItemControllerImpel
src = source.getAttribute('src');
if (src && src.trim() !== '' && !src.startsWith('blob:') && !src.includes('googleads')) {
_observer.disconnect();
- console.log('VIDEO source found (source tag):', src);
+ window.flutter_inappwebview.callHandler('LogBridge', 'VIDEO source found (source tag): ' + src);
window.flutter_inappwebview.callHandler('VideoBridgeDebug', src);
return true;
}
@@ -263,10 +276,12 @@ class WebviewAndroidItemControllerImpel
Future<void> onLoadStart() async {
if (!useNativePlayer && shouldInjectIframeRedirect) {
- debugPrint('[WebView] Adding IframeRedirectBridge user script');
+ logEventController.add('Adding IframeRedirectBridge UserScript');
shouldInjectIframeRedirect = false;
await webviewController?.evaluateJavascript(source: """
+ window.flutter_inappwebview.callHandler('LogBridge', 'IframeRedirectBridge script loaded: ' + window.location.href);
const _observer = new MutationObserver((mutations) => {
+ window.flutter_inappwebview.callHandler('LogBridge', 'Scanning for iframes...');
for (const mutation of mutations) {
if (mutation.type === "attributes" && mutation.target.nodeName === "IFRAME") {
if (processIframeElement(mutation.target)) return;
@@ -286,6 +301,7 @@ class WebviewAndroidItemControllerImpel
});
function processIframeElement(iframe) {
+ window.flutter_inappwebview.callHandler('LogBridge', 'Processing iframe element');
let src = iframe.getAttribute("src");
if (src && src.trim() !== '' && (src.startsWith('http') || src.startsWith('//')) && !src.includes('googleads') && !src.includes('adtrafficquality') && !src.includes('googlesyndication.com') && !src.includes('google.com') && !src.includes('prestrain.html') && !src.includes('prestrain%2Ehtml')) {
_observer.disconnect();
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.dart
index 1ab9c5e9..57c1ae96 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,5 +1,4 @@
import 'dart:async';
-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';
@@ -49,8 +48,19 @@ class WebviewAppleItemControllerImpel
}
void addJavaScriptHandlers(bool useNativePlayer, bool useLegacyParser) {
+ logEventController.add('Adding LogBridge handler');
+ webviewController?.addJavaScriptHandler(
+ handlerName: 'LogBridge',
+ callback: (args) {
+ String message = args[0].toString();
+ if (message.contains('about:blank')) {
+ return;
+ }
+ logEventController.add(message);
+ });
+
if (!useNativePlayer) {
- debugPrint('[WebView] Adding IframeRedirectBridge handler');
+ logEventController.add('Adding IframeRedirectBridge handler');
webviewController?.addJavaScriptHandler(
handlerName: 'IframeRedirectBridge',
callback: (args) {
@@ -62,7 +72,7 @@ class WebviewAppleItemControllerImpel
});
});
} else if (useLegacyParser) {
- debugPrint('[WebView] Adding JSBridgeDebug handler');
+ logEventController.add('Adding JSBridgeDebug handler');
webviewController?.addJavaScriptHandler(
handlerName: 'JSBridgeDebug',
callback: (args) {
@@ -91,7 +101,7 @@ class WebviewAppleItemControllerImpel
}
});
} else {
- debugPrint('[WebView] Adding VideoBridgeDebug handler');
+ logEventController.add('Adding VideoBridgeDebug handler');
webviewController?.addJavaScriptHandler(
handlerName: 'VideoBridgeDebug',
callback: (args) {
@@ -114,8 +124,9 @@ class WebviewAppleItemControllerImpel
final List<UserScript> scripts = [];
if (!useNativePlayer) {
- debugPrint('[WebView] Adding IframeRedirectBridge user script');
+ logEventController.add('Adding IframeRedirectBridge UserScript');
const String iframeRedirectScript = """
+ window.flutter_inappwebview.callHandler('LogBridge', 'IframeRedirectBridge script loaded: ' + window.location.href);
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
var iframe = iframes[i];
@@ -133,10 +144,11 @@ class WebviewAppleItemControllerImpel
forMainFrameOnly: true,
));
} else if (useLegacyParser) {
- debugPrint('[WebView] Adding JSBridgeDebug user script');
+ logEventController.add('Adding JSBridgeDebug UserScript');
const String jsBridgeDebugScript = """
+ window.flutter_inappwebview.callHandler('LogBridge', 'JSBridgeDebug script loaded: ' + window.location.href);
var iframes = document.getElementsByTagName('iframe');
- window.flutter_inappwebview.callHandler('JSBridgeDebug', 'The number of iframe tags is' + iframes.length);
+ window.flutter_inappwebview.callHandler('LogBridge', 'The number of iframe tags is ' + iframes.length);
for (var i = 0; i < iframes.length; i++) {
var iframe = iframes[i];
var src = iframe.getAttribute('src');
@@ -151,15 +163,16 @@ class WebviewAppleItemControllerImpel
forMainFrameOnly: false,
));
} else {
- debugPrint('[WebView] Adding VideoBridgeDebug user script');
+ logEventController.add('Adding VideoBridgeDebug UserScripts');
const String blobParserScript = """
+ window.flutter_inappwebview.callHandler('LogBridge', 'BlobParser script loaded: ' + window.location.href);
const _r_text = window.Response.prototype.text;
window.Response.prototype.text = function () {
return new Promise((resolve, reject) => {
_r_text.call(this).then((text) => {
resolve(text);
if (text.trim().startsWith("#EXTM3U")) {
- console.log('M3U8 source found:', this.url);
+ window.flutter_inappwebview.callHandler('LogBridge', 'M3U8 source found: ' + this.url);
window.flutter_inappwebview.callHandler('VideoBridgeDebug', this.url);
}
}).catch(reject);
@@ -172,7 +185,7 @@ class WebviewAppleItemControllerImpel
try {
let content = this.responseText;
if (content.trim().startsWith("#EXTM3U")) {
- console.log('M3U8 source found:', args[1]);
+ window.flutter_inappwebview.callHandler('LogBridge', 'M3U8 source found: ' + args[1]);
window.flutter_inappwebview.callHandler('VideoBridgeDebug', args[1]);
};
} catch {}
@@ -182,11 +195,12 @@ class WebviewAppleItemControllerImpel
""";
const String videoTagParserScript = """
+ window.flutter_inappwebview.callHandler('LogBridge', 'VideoTagParser script loaded: ' + window.location.href);
function processVideoElement(video) {
- window.flutter_inappwebview.callHandler('VideoBridgeDebug', 'Scanning video tag for source URL');
+ window.flutter_inappwebview.callHandler('LogBridge', 'Scanning video element for source URL');
let src = video.getAttribute('src');
if (src && src.trim() !== '' && !src.startsWith('blob:') && !src.includes('googleads')) {
- console.log('VIDEO source found:', src);
+ window.flutter_inappwebview.callHandler('LogBridge', 'VIDEO source found: ' + src);
window.flutter_inappwebview.callHandler('VideoBridgeDebug', src);
return;
}
@@ -194,7 +208,7 @@ class WebviewAppleItemControllerImpel
for (let source of sources) {
src = source.getAttribute('src');
if (src && src.trim() !== '' && !src.startsWith('blob:') && !src.includes('googleads')) {
- console.log('VIDEO source found (source tag):', src);
+ window.flutter_inappwebview.callHandler('LogBridge', 'VIDEO source found (source tag): ' + src);
window.flutter_inappwebview.callHandler('VideoBridgeDebug', src);
return;
}
@@ -204,6 +218,7 @@ class WebviewAppleItemControllerImpel
document.querySelectorAll('video').forEach(processVideoElement);
const _observer = new MutationObserver((mutations) => {
+ window.flutter_inappwebview.callHandler('LogBridge', 'Scanning for video elements...');
mutations.forEach(mutation => {
if (mutation.type === 'attributes' && mutation.target.nodeName === 'VIDEO') {
processVideoElement(mutation.target);
diff --git a/lib/pages/webview/webview_item_impel/webview_android_item_impel.dart b/lib/pages/webview/webview_item_impel/webview_android_item_impel.dart
index 7c910862..2eed8d1a 100644
--- a/lib/pages/webview/webview_item_impel/webview_android_item_impel.dart
+++ b/lib/pages/webview/webview_item_impel/webview_android_item_impel.dart
@@ -48,13 +48,15 @@ class _WebviewAndroidItemImpelState extends State<WebviewAndroidItemImpel> {
webviewAndroidItemController.init();
},
onLoadStart: (controller, url) async {
- debugPrint('[WebView] Started loading: $url');
+ webviewAndroidItemController.logEventController
+ .add('started loading: $url');
if (url.toString() != 'about:blank') {
await webviewAndroidItemController.onLoadStart();
}
},
onLoadStop: (controller, url) {
- debugPrint('[WebView] Loading completed: $url');
+ webviewAndroidItemController.logEventController
+ .add('loading completed: $url');
},
)).build(context);
}
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.dart
index 1b0fde39..e5267453 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
@@ -116,10 +116,12 @@ class _WebviewAppleItemImpelState extends State<WebviewAppleItemImpel> {
webviewAppleItemController.init();
},
onLoadStart: (controller, url) {
- debugPrint('[WebView] Started loading: $url');
+ webviewAppleItemController.logEventController
+ .add('started loading: $url');
},
onLoadStop: (controller, url) {
- debugPrint('[WebView] Loading completed: $url');
+ webviewAppleItemController.logEventController
+ .add('loading completed: $url');
},
onConsoleMessage: (controller, consoleMessage) {
debugPrint(