From 9ea749a84a3b0631860d4080b471938dfe1230e0 Mon Sep 17 00:00:00 2001 From: nyne Date: Fri, 31 Jan 2025 11:53:06 +0800 Subject: [PATCH] login with webview on windows and linux. fix #162, fix #141 --- lib/pages/accounts_page.dart | 66 ++++++++++++++++++++++++++++++++++-- lib/pages/webview.dart | 5 ++- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/lib/pages/accounts_page.dart b/lib/pages/accounts_page.dart index 121ad37..b569b19 100644 --- a/lib/pages/accounts_page.dart +++ b/lib/pages/accounts_page.dart @@ -9,6 +9,7 @@ import 'package:venera/foundation/state_controller.dart'; import 'package:venera/network/cookie_jar.dart'; import 'package:venera/pages/webview.dart'; import 'package:venera/utils/translations.dart'; +import 'dart:io' as io; class AccountsPageLogic extends StateController { final _reLogin = {}; @@ -236,7 +237,13 @@ class _LoginPageState extends State<_LoginPage> { const SizedBox(height: 24), if (widget.config.loginWebsite != null) TextButton( - onPressed: loginWithWebview, + onPressed: () { + if (App.isWindows || App.isLinux) { + loginWithWebview2(); + } else { + loginWithWebview(); + } + }, child: Text("Login with webview".tl), ), const SizedBox(height: 8), @@ -313,8 +320,8 @@ class _LoginPageState extends State<_LoginPage> { bool success = false; void validate(InAppWebViewController c) async { - if (widget.config.checkLoginStatus != null - && widget.config.checkLoginStatus!(url, title)) { + if (widget.config.checkLoginStatus != null && + widget.config.checkLoginStatus!(url, title)) { var cookies = (await c.getCookies(url)) ?? []; SingleInstanceCookieJar.instance?.saveFromResponse( Uri.parse(url), @@ -346,4 +353,57 @@ class _LoginPageState extends State<_LoginPage> { 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 = []; + 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(); + } } diff --git a/lib/pages/webview.dart b/lib/pages/webview.dart index e6a4320..70655f6 100644 --- a/lib/pages/webview.dart +++ b/lib/pages/webview.dart @@ -303,7 +303,10 @@ class DesktopWebview { proxy: AppDio.proxy, )); _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); _runTimer(); _webview!.onClose.then((value) {