login with webview on windows and linux.

fix #162, fix #141
This commit is contained in:
2025-01-31 11:53:06 +08:00
parent d675af3fb4
commit 9ea749a84a
2 changed files with 67 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ import 'package:venera/foundation/state_controller.dart';
import 'package:venera/network/cookie_jar.dart'; import 'package:venera/network/cookie_jar.dart';
import 'package:venera/pages/webview.dart'; import 'package:venera/pages/webview.dart';
import 'package:venera/utils/translations.dart'; import 'package:venera/utils/translations.dart';
import 'dart:io' as io;
class AccountsPageLogic extends StateController { class AccountsPageLogic extends StateController {
final _reLogin = <String, bool>{}; final _reLogin = <String, bool>{};
@@ -236,7 +237,13 @@ class _LoginPageState extends State<_LoginPage> {
const SizedBox(height: 24), const SizedBox(height: 24),
if (widget.config.loginWebsite != null) if (widget.config.loginWebsite != null)
TextButton( TextButton(
onPressed: loginWithWebview, onPressed: () {
if (App.isWindows || App.isLinux) {
loginWithWebview2();
} else {
loginWithWebview();
}
},
child: Text("Login with webview".tl), child: Text("Login with webview".tl),
), ),
const SizedBox(height: 8), const SizedBox(height: 8),
@@ -313,8 +320,8 @@ class _LoginPageState extends State<_LoginPage> {
bool success = false; bool success = false;
void validate(InAppWebViewController c) async { void validate(InAppWebViewController c) async {
if (widget.config.checkLoginStatus != null if (widget.config.checkLoginStatus != null &&
&& widget.config.checkLoginStatus!(url, title)) { widget.config.checkLoginStatus!(url, title)) {
var cookies = (await c.getCookies(url)) ?? []; var cookies = (await c.getCookies(url)) ?? [];
SingleInstanceCookieJar.instance?.saveFromResponse( SingleInstanceCookieJar.instance?.saveFromResponse(
Uri.parse(url), Uri.parse(url),
@@ -346,4 +353,57 @@ class _LoginPageState extends State<_LoginPage> {
context.pop(); context.pop();
} }
} }
// for windows and linux
void loginWithWebview2() async {
if (!await DesktopWebview.isAvailable()) {
context.showMessage(message: "Webview is not available".tl);
}
var url = widget.config.loginWebsite!;
var title = '';
bool success = false;
void onClose() {
if (success) {
widget.source.data['account'] = 'ok';
widget.source.saveData();
context.pop();
}
}
void validate(DesktopWebview webview) async {
if (widget.config.checkLoginStatus != null &&
widget.config.checkLoginStatus!(url, title)) {
var cookiesMap = await webview.getCookies(url);
var cookies = <io.Cookie>[];
cookiesMap.forEach((key, value) {
cookies.add(io.Cookie(key, value));
});
SingleInstanceCookieJar.instance?.saveFromResponse(
Uri.parse(url),
cookies,
);
success = true;
widget.config.onLoginWithWebviewSuccess?.call();
webview.close();
onClose();
}
}
var webview = DesktopWebview(
initialUrl: widget.config.loginWebsite!,
onTitleChange: (t, webview) {
title = t;
validate(webview);
},
onNavigation: (u, webview) {
url = u;
validate(webview);
},
onClose: onClose,
);
webview.open();
}
} }

View File

@@ -303,7 +303,10 @@ class DesktopWebview {
proxy: AppDio.proxy, proxy: AppDio.proxy,
)); ));
_webview!.addOnWebMessageReceivedCallback(onMessage); _webview!.addOnWebMessageReceivedCallback(onMessage);
_webview!.setOnNavigation((s) => onNavigation?.call(s, this)); _webview!.setOnNavigation((s) {
s = s.substring(1, s.length - 1);
return onNavigation?.call(s, this);
});
_webview!.launch(initialUrl, triggerOnUrlRequestEvent: false); _webview!.launch(initialUrl, triggerOnUrlRequestEvent: false);
_runTimer(); _runTimer();
_webview!.onClose.then((value) { _webview!.onClose.then((value) {