feat: WebView optimization

This commit is contained in:
xkeyC
2025-12-22 20:55:34 +08:00
parent eb42c7101d
commit dd762d53b2
6 changed files with 241 additions and 51 deletions

View File

@@ -27,9 +27,6 @@ class RustWebViewController {
/// 本地化脚本(从 assets 加载)
String _localizationScript = "";
/// 请求拦截器脚本
String _requestInterceptorScript = "";
/// 当前 URL
String _currentUrl = "";
String get currentUrl => _currentUrl;
@@ -76,7 +73,6 @@ class RustWebViewController {
Future<void> _loadScripts() async {
try {
_localizationScript = await rootBundle.loadString('assets/web_script.js');
_requestInterceptorScript = await rootBundle.loadString('assets/request_interceptor.js');
} catch (e) {
dPrint("Failed to load scripts: $e");
}
@@ -120,10 +116,6 @@ class RustWebViewController {
case rust_webview.WebViewEvent_NavigationCompleted(:final url):
dPrint("Navigation completed: $url");
_currentUrl = url;
// 注入请求拦截器
if (_requestInterceptorScript.isNotEmpty) {
executeScript(_requestInterceptorScript);
}
// 导航完成回调(用于注入脚本)
for (final callback in _navigationCompletedCallbacks) {
callback(url);

View File

@@ -36,7 +36,6 @@ class WebViewModel {
final localizationResource = <String, dynamic>{};
var localizationScript = "";
var requestInterceptorScript = "";
bool enableCapture = false;
@@ -75,7 +74,6 @@ class WebViewModel {
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0",
);
webview.addOnNavigationCallback(_onNavigation);
// 添加导航完成回调(用于注入脚本)
webview.addOnNavigationCompletedCallback(_onNavigationCompleted);
@@ -208,7 +206,6 @@ class WebViewModel {
Future<void> initLocalization(AppWebLocalizationVersionsData v) async {
localizationScript = await rootBundle.loadString('assets/web_script.js');
requestInterceptorScript = await rootBundle.loadString('assets/request_interceptor.js');
/// https://github.com/CxJuice/Uex_Chinese_Translate
// get versions
@@ -282,19 +279,10 @@ class WebViewModel {
FutureOr<void> dispose() {
webview.removeOnNavigationCompletedCallback(_onNavigationCompleted);
webview.removeOnNavigationCallback(_onNavigation);
if (loginMode && !_loginModeSuccess) {
loginCallback?.call(null, false);
}
_isClosed = true;
webview.dispose();
}
void _onNavigation(String url) {
// 在页面加载时注入拦截器
if (requestInterceptorScript.isNotEmpty) {
dPrint("Injecting request interceptor for: $url");
webview.executeScript(requestInterceptorScript);
}
}
}