improve html api;

fix thumbnails loading api;
add favoriteId api
This commit is contained in:
nyne
2024-10-20 11:11:47 +08:00
parent 0b13950a9e
commit b682d7d87b
9 changed files with 219 additions and 74 deletions

View File

@@ -1,6 +1,6 @@
part of 'comic_source.dart';
typedef AddOrDelFavFunc = Future<Res<bool>> Function(String comicId, String folderId, bool isAdding);
typedef AddOrDelFavFunc = Future<Res<bool>> Function(String comicId, String folderId, bool isAdding, String? favId);
class FavoriteData{
final String key;

View File

@@ -58,6 +58,8 @@ class Comic {
final String? language;
final String? favoriteId;
const Comic(
this.title,
this.cover,
@@ -68,7 +70,7 @@ class Comic {
this.sourceKey,
this.maxPage,
this.language,
);
): favoriteId = null;
Map<String, dynamic> toJson() {
return {
@@ -81,6 +83,7 @@ class Comic {
"sourceKey": sourceKey,
"maxPage": maxPage,
"language": language,
"favoriteId": favoriteId,
};
}
@@ -92,7 +95,8 @@ class Comic {
tags = List<String>.from(json["tags"] ?? []),
description = json["description"] ?? "",
maxPage = json["maxPage"],
language = json["language"];
language = json["language"],
favoriteId = json["favoriteId"];
@override
bool operator ==(Object other) {

View File

@@ -202,7 +202,7 @@ class ComicSourceParser {
JsEngine().runCode("ComicSource.sources.$_key.account.logout()");
}
if(!_checkExists('account.loginWithWebview')) {
if (!_checkExists('account.loginWithWebview')) {
return AccountConfig(
login,
null,
@@ -378,7 +378,7 @@ class ComicSourceParser {
CategoryComicsData? _loadCategoryComicsData() {
if (!_checkExists("categoryComics")) return null;
var options = <CategoryComicsOptions>[];
for (var element in _getValue("categoryComics.optionList")) {
for (var element in _getValue("categoryComics.optionList") ?? []) {
LinkedHashMap<String, String> map = LinkedHashMap<String, String>();
for (var option in element["options"]) {
if (option.isEmpty || !option.contains("-")) {
@@ -528,7 +528,12 @@ class ComicSourceParser {
return res;
}
Future<Res<bool>> addOrDelFavFunc(comicId, folderId, isAdding) async {
Future<Res<bool>> addOrDelFavFunc(
String comicId,
String folderId,
bool isAdding,
String? favId,
) async {
func() async {
try {
await JsEngine().runCode("""
@@ -703,13 +708,13 @@ class ComicSourceParser {
}
ComicThumbnailLoader? _parseThumbnailLoader() {
if (!_checkExists("comic.loadThumbnail")) {
if (!_checkExists("comic.loadThumbnails")) {
return null;
}
return (id, next) async {
try {
var res = await JsEngine().runCode("""
ComicSource.sources.$_key.comic.loadThumbnail(${jsonEncode(id)}, ${jsonEncode(next)})
ComicSource.sources.$_key.comic.loadThumbnails(${jsonEncode(id)}, ${jsonEncode(next)})
""");
return Res(List<String>.from(res['thumbnails']), subData: res['next']);
} catch (e, s) {
@@ -818,6 +823,7 @@ class ComicSourceParser {
""");
return res as String?;
}
return LinkHandler(domains, linkToId);
}
}

View File

@@ -141,7 +141,11 @@ class JsEngine with _JSEngineApi {
}
case "random":
{
return _randomInt(message["min"], message["max"]);
return _random(
message["min"] ?? 0,
message["max"] ?? 1,
message["type"],
);
}
case "cookie":
{
@@ -232,7 +236,7 @@ mixin class _JSEngineApi {
Object? handleHtmlCallback(Map<String, dynamic> data) {
switch (data["function"]) {
case "parse":
if(_documents.length > 2) {
if (_documents.length > 2) {
_documents.remove(_documents.keys.first);
}
_documents[data["key"]] = DocumentWrapper.parse(data["data"]);
@@ -276,6 +280,12 @@ mixin class _JSEngineApi {
var docKey = data["key"];
_documents.remove(docKey);
return null;
case "getClassNames":
return _documents[data["doc"]]!.getClassNames(data["key"]);
case "getId":
return _documents[data["doc"]]!.getId(data["key"]);
case "getLocalName":
return _documents[data["doc"]]!.getLocalName(data["key"]);
}
return null;
}
@@ -455,7 +465,10 @@ mixin class _JSEngineApi {
: output.sublist(0, outputOffset);
}
int _randomInt(int min, int max) {
num _random(num min, num max, String type) {
if (type == "double") {
return min + (max - min) * math.Random().nextDouble();
}
return (min + (max - min) * math.Random().nextDouble()).toInt();
}
}
@@ -568,4 +581,16 @@ class DocumentWrapper {
}
return null;
}
List<String> getClassNames(int key) {
return (elements[key]).classes.toList();
}
String? getId(int key) {
return (elements[key]).id;
}
String? getLocalName(int key) {
return (elements[key]).localName;
}
}

View File

@@ -124,6 +124,9 @@ class LocalComic with HistoryMixin implements Comic {
@override
String? get language => null;
@override
String? get favoriteId => null;
}
class LocalManager with ChangeNotifier {