This commit is contained in:
Schnitzel5 2025-05-05 20:12:58 +02:00
parent 821366cb06
commit d6d674c270
2 changed files with 57 additions and 5 deletions

View file

@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter_qjs/flutter_qjs.dart';
import 'package:http_interceptor/http_interceptor.dart';
import 'package:mangayomi/services/http/m_client.dart';
@ -16,6 +17,12 @@ class JsHttpClient {
);
}
runtime.onMessage('bytes_get', (dynamic args) async {
return await _toBytesResponse(client(args[1]), "GET", args);
});
runtime.onMessage('http_head', (dynamic args) async {
return await _toHttpResponse(client(args[1]), "HEAD", args);
});
runtime.onMessage('http_get', (dynamic args) async {
return await _toHttpResponse(client(args[1]), "GET", args);
});
@ -36,6 +43,22 @@ class Client {
constructor(reqcopyWith) {
this.reqcopyWith = reqcopyWith;
}
async getBytes(url, headers) {
headers = headers;
const result = await sendMessage(
"bytes_get",
JSON.stringify([null, this.reqcopyWith, url, headers])
);
return result;
}
async head(url, headers) {
headers = headers;
const result = await sendMessage(
"http_head",
JSON.stringify([null, this.reqcopyWith, url, headers])
);
return JSON.parse(result);
}
async get(url, headers) {
headers = headers;
const result = await sendMessage(
@ -115,6 +138,7 @@ Future<String> _toHttpResponse(Client client, String method, List args) async {
return jsonEncode(resMap);
}
final future = switch (method) {
"HEAD" => client.head(Uri.parse(url), headers: headers),
"GET" => client.get(Uri.parse(url), headers: headers),
"POST" => client.post(Uri.parse(url), headers: headers, body: body),
"PUT" => client.put(Uri.parse(url), headers: headers, body: body),
@ -124,6 +148,29 @@ Future<String> _toHttpResponse(Client client, String method, List args) async {
return jsonEncode((await future).toJson());
}
Future<Uint8List> _toBytesResponse(Client client, String method, List args) async {
final url = args[2] as String;
final headers = (args[3] as Map?)?.toMapStringString;
final body =
args.length >= 5
? args[4] is List
? args[4] as List
: args[4] is String
? args[4] as String
: (args[4] as Map?)?.toMapStringDynamic
: null;
var request = http.Request(method, Uri.parse(url));
request.headers.addAll(headers ?? {});
final future = switch (method) {
"GET" => client.get(Uri.parse(url), headers: headers),
"POST" => client.post(Uri.parse(url), headers: headers, body: body),
"PUT" => client.put(Uri.parse(url), headers: headers, body: body),
"DELETE" => client.delete(Uri.parse(url), headers: headers, body: body),
_ => client.patch(Uri.parse(url), headers: headers, body: body),
};
return (await future).bodyBytes;
}
extension ResponseExtexsion on Response {
Map<String, dynamic> toJson() => {
'body': body,

View file

@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:epubx/epubx.dart';
import 'package:flutter_qjs/flutter_qjs.dart';
@ -43,7 +44,7 @@ class JsUtils {
);
});
runtime.onMessage('parseEpub', (dynamic args) async {
final book = await EpubReader.readBook(args[0]);
final book = await EpubReader.readBook(decodeBytes(args[0]));
final List<String> chapters = [];
for (var chapter in book.Chapters ?? []) {
String chapterTitle = chapter.Title;
@ -52,12 +53,11 @@ class JsUtils {
return jsonEncode({
"title": book.Title,
"author": book.Author,
"cover": book.CoverImage?.data?.buffer.asUint8List(),
"chapters": chapters,
});
});
runtime.onMessage('parseEpubChapter', (dynamic args) async {
final book = await EpubReader.readBook(args[0]);
final book = await EpubReader.readBook(decodeBytes(args[0]));
final chapter =
book.Chapters?.where(
(element) => element.Title == args[1],
@ -169,10 +169,10 @@ async function evaluateJavascriptViaWebview(url, headers, scripts) {
);
}
async function parseEpub(bytes) {
return await sendMessage(
return JSON.parse(await sendMessage(
"parseEpub",
JSON.stringify([bytes])
);
));
}
async function parseEpubChapter(bytes, chapterTitle) {
return await sendMessage(
@ -182,4 +182,9 @@ async function parseEpubChapter(bytes, chapterTitle) {
}
''');
}
Uint8List decodeBytes(String value) {
var bytes = Uint8List.fromList(value.codeUnits);
return bytes;
}
}