This commit is contained in:
kodjomoustapha 2024-02-07 23:50:22 +01:00
parent 9217bc6a3b
commit 88fa089727
4 changed files with 104 additions and 7 deletions

View file

@ -44,20 +44,46 @@ class _MangaWebViewState extends ConsumerState<MangaWebView> {
super.initState();
}
bool _cancel = false;
final _windowsWebview = FlutterWindowsWebview();
Webview? _linuxWebview;
Webview? _desktopWebview;
void _runWebViewDesktop() async {
if (Platform.isLinux || Platform.isMacOS) {
_linuxWebview = await WebviewWindow.create(
_desktopWebview = await WebviewWindow.create(
configuration: CreateConfiguration(
userDataFolderWindows: await getWebViewPath(),
),
);
_linuxWebview!
_desktopWebview!
..launch(widget.url)
..onClose.whenComplete(() {
Navigator.pop(context);
..onClose.whenComplete(() async {
if (Platform.isMacOS && widget.hasCloudFlare) {
final cookieList = await _desktopWebview!.getCookies(widget.url);
for (var c in cookieList) {
final cookie =
c.entries.map((e) => "${e.key}=${e.value}").join(";");
await MInterceptor.setCookie(_url, "", cookie: cookie);
}
_cancel = true;
}
if (mounted) {
Navigator.pop(context);
}
});
if (Platform.isMacOS && widget.hasCloudFlare) {
await Future.doWhile(() async {
await Future.delayed(const Duration(seconds: 1));
if (_cancel) return false;
final ua =
await _desktopWebview?.evaluateJavaScript("navigator.userAgent");
if (ua != null) {
MInterceptor.setCookie(_url, ua);
return false;
}
return true;
});
}
}
//credit: https://github.com/wgh136/PicaComic/blob/master/lib/network/nhentai_network/cloudflare.dart
else if (Platform.isWindows && await FlutterWindowsWebview.isAvailable()) {
@ -104,7 +130,7 @@ class _MangaWebViewState extends ConsumerState<MangaWebView> {
leading: IconButton(
onPressed: () {
if (Platform.isLinux || Platform.isMacOS) {
_linuxWebview!.close();
_desktopWebview!.close();
}
Navigator.pop(context);
},

View file

@ -88,4 +88,6 @@ abstract class Webview {
/// post a web message as JSON to the top level document in this WebView
Future<void> postWebMessageAsJson(String webMessage);
Future<List<Map<String, dynamic>>> getCookies(String url);
}

View file

@ -219,7 +219,8 @@ class WebviewImpl extends Webview {
}
@override
void removeOnWebMessageReceivedCallback(OnWebMessageReceivedCallback callback) {
void removeOnWebMessageReceivedCallback(
OnWebMessageReceivedCallback callback) {
_onWebMessageReceivedCallbacks.remove(callback);
}
@ -258,4 +259,27 @@ class WebviewImpl extends Webview {
"webMessage": webMessage,
});
}
@override
Future<List<Map<String, dynamic>>> getCookies(String url) async {
final cookieListMap =
await channel.invokeMethod<List>('getCookies', {'url': url}) ?? [];
List<Map<String, dynamic>> cookies = [];
for (var cookieMap in cookieListMap) {
cookies.add({
if (cookieMap["name"] != null) "name": cookieMap["name"],
if (cookieMap["value"] != null) "value": cookieMap["value"],
if (cookieMap["expiresDate"] != null)
"expiresDate": cookieMap["expiresDate"],
if (cookieMap["isSessionOnly"] != null)
"isSessionOnly": cookieMap["isSessionOnly"],
if (cookieMap["domain"] != null) "domain": cookieMap["domain"],
if (cookieMap["isSecure"] != null) "isSecure": cookieMap["isSecure"],
if (cookieMap["isHttpOnly"] != null)
"isHttpOnly": cookieMap["isHttpOnly"],
if (cookieMap["path"] != null) "path": cookieMap["path"]
});
}
return cookies;
}
}

View file

@ -45,6 +45,51 @@ public class DesktopWebviewWindowPlugin: NSObject, FlutterPlugin {
result(viewId)
viewId += 1
break
case "getCookies":
guard let argument = call.arguments as? [String: Any?] else {
result(FlutterError(code: "0", message: "arg is not map", details: nil))
return
}
guard let url = argument["url"] as? String else {
result(FlutterError(code: "0", message: "param url not found", details: nil))
return
}
var cookieList: [[String: Any?]] = []
if let urlHost = URL(string: url)?.host {
WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in
for cookie in cookies {
if urlHost.hasSuffix(cookie.domain) || ".\(urlHost)".hasSuffix(cookie.domain) {
var sameSite: String? = nil
if #available(macOS 10.15, *) {
if let sameSiteValue = cookie.sameSitePolicy?.rawValue {
sameSite = sameSiteValue.prefix(1).capitalized + sameSiteValue.dropFirst()
}
}
var expiresDateTimestamp: Int64 = -1
if let expiresDate = cookie.expiresDate?.timeIntervalSince1970 {
// convert to milliseconds
expiresDateTimestamp = Int64(expiresDate * 1000)
}
cookieList.append([
"name": cookie.name,
"value": cookie.value,
"expiresDate": expiresDateTimestamp != -1 ? expiresDateTimestamp : nil,
"isSessionOnly": cookie.isSessionOnly,
"domain": cookie.domain,
"sameSite": sameSite,
"isSecure": cookie.isSecure,
"isHttpOnly": cookie.isHTTPOnly,
"path": cookie.path,
])
}
}
result(cookieList)
}
return
}
result(cookieList)
break
case "launch":
guard let argument = call.arguments as? [String: Any?] else {
result(FlutterError(code: "0", message: "arg is not map", details: nil))