Skip to content

Commit

Permalink
add function for extension
Browse files Browse the repository at this point in the history
  • Loading branch information
appdevelpo committed Nov 23, 2023
1 parent 287a924 commit 024599c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
42 changes: 41 additions & 1 deletion lib/controllers/watch/comic_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:miru_app/models/index.dart';
import 'package:miru_app/controllers/watch/reader_controller.dart';
import 'package:miru_app/data/services/database_service.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'dart:async';

class ComicController extends ReaderController<ExtensionMangaWatch> {
ComicController({
Expand All @@ -19,7 +20,7 @@ class ComicController extends ReaderController<ExtensionMangaWatch> {
final readType = MangaReadMode.standard.obs;
// 当前页码
final currentPage = 0.obs;

bool timerCancel = false;
final pageController = PageController().obs;
final itemPositionsListener = ItemPositionsListener.create();
final itemScrollController = ItemScrollController();
Expand All @@ -38,6 +39,7 @@ class ComicController extends ReaderController<ExtensionMangaWatch> {
final pos = itemPositionsListener.itemPositions.value.first;
currentPage.value = pos.index;
});
_pageUpdate();
ever(readType, (callback) {
_jumpPage(currentPage.value);
// 保存设置
Expand Down Expand Up @@ -70,6 +72,31 @@ class ComicController extends ReaderController<ExtensionMangaWatch> {
super.onInit();
}

_pageUpdate() async {
int curPage = currentPage.value;
Timer(const Duration(seconds: 2), () {
//set update timer
Timer.periodic(const Duration(milliseconds: 600), (timer) {
debugPrint('$curPage');
if (curPage >= super.watchData.value!.urls.length - 1 || timerCancel) {
timer.cancel();
}
curPage++;

if (super.watchData.value!.urls[curPage] == "") {
_getPage(curPage);
}
});
});
}

_getPage(int page) async {
await Future.delayed(const Duration(seconds: 1));

final updatePage = await runtime.updatePages(page) as ExtensionUpdatePages;
super.watchData.value!.urls[page] = updatePage.url;
}

_initSetting() async {
readType.value = await DatabaseService.getMnagaReaderType(super.detailUrl);
}
Expand All @@ -81,6 +108,18 @@ class ComicController extends ReaderController<ExtensionMangaWatch> {
index: page,
);
}
int curPage = currentPage.value;
Timer.periodic(const Duration(milliseconds: 600), (timer) {
debugPrint('$curPage');
if (curPage == -1 || timerCancel) {
timer.cancel();
}
curPage--;

if (super.watchData.value!.urls[curPage] == "") {
_getPage(curPage);
}
});
return;
}
if (pageController.value.hasClients) {
Expand Down Expand Up @@ -133,6 +172,7 @@ class ComicController extends ReaderController<ExtensionMangaWatch> {
);
}
pageController.value.dispose();
timerCancel = true;
super.onClose();
}
}
61 changes: 60 additions & 1 deletion lib/data/services/extension_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,26 @@ class ExtensionService {
))
.data;
});

// request with headers
runtime.onMessage('RequestWithHeader', (dynamic args) async {
ExtensionUtils.addLog(
extension,
ExtensionLogLevel.info,
"RequestWithHeader: ${args[0]} , ${args[1]}",
);
_cuurentRequestUrl = args[0];
Response response = await _dio.request<String>(
args[0],
data: args[1]['data'],
queryParameters: args[1]['queryParameters'] ?? {},
options: Options(
headers: args[1]['headers'] ?? {},
method: args[1]['method'] ?? 'get',
),
);
Map<String, dynamic> headers = response.headers.map;
return {'data': response.data, 'headers': headers};
});
// 设置
runtime.onMessage('registerSetting', (dynamic args) async {
args[0]['package'] = extension.package;
Expand Down Expand Up @@ -295,6 +314,22 @@ class ExtensionService {
return res;
}
}
async RequestWithHeader(url, options) {
options = options || {};
options.headers = options.headers || {};
const miruUrl = options.headers["Miru-Url"] || "${extension.webSite}";
options.method = options.method || "get";
const res = await sendMessage(
"RequestWithHeader",
JSON.stringify([miruUrl + url, options])
);
console.log(res)
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
querySelector(content, selector) {
return new Element(content, selector);
}
Expand Down Expand Up @@ -327,6 +362,9 @@ class ExtensionService {
}
createFilter(filter){
throw new Error("not implement createFilter");
}
updatePages(){
}
detail(url) {
throw new Error("not implement detail");
Expand Down Expand Up @@ -503,13 +541,34 @@ class ExtensionService {
result.headers ??= {
"Referer": _cuurentRequestUrl,
};
if (result.pages == null) {
return result;
}
result.urls.addAll(
List<String>.filled(result.pages! - result.urls.length, ""));
return result;
default:
return ExtensionFikushonWatch.fromJson(data);
}
});
}

Future<Object?> updatePages(int page) async {
return _runExtension(() async {
final jsResult = await runtime.handlePromise(
await runtime
.evaluateAsync('stringify(()=>extenstion.updatePages("$page"))'),
);
final data = jsonDecode(jsResult.stringResult);

final result = ExtensionUpdatePages.fromJson(data);
result.headers ??= {
"Referer": _cuurentRequestUrl,
};
return result;
});
}

Future<String> checkUpdate(url) async {
return _runExtension(() async {
final jsResult = await runtime.handlePromise(
Expand Down
18 changes: 17 additions & 1 deletion lib/models/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,12 @@ class ExtensionMangaWatch {
ExtensionMangaWatch({
required this.urls,
this.headers,
this.pages,
});

final List<String> urls;
late Map<String, String>? headers;

final int? pages;
factory ExtensionMangaWatch.fromJson(Map<String, dynamic> json) =>
_$ExtensionMangaWatchFromJson(json);

Expand All @@ -214,6 +215,21 @@ class ExtensionFikushonWatch {
Map<String, dynamic> toJson() => _$ExtensionFikushonWatchToJson(this);
}

@JsonSerializable()
class ExtensionUpdatePages {
late Map<String, String>? headers;
final String url;
ExtensionUpdatePages({
required this.url,
this.headers,
});

factory ExtensionUpdatePages.fromJson(Map<String, dynamic> json) =>
_$ExtensionUpdatePagesFromJson(json);

Map<String, dynamic> toJson() => _$ExtensionUpdatePagesToJson(this);
}

@JsonSerializable()
class ExtensionLog {
ExtensionLog({
Expand Down
18 changes: 18 additions & 0 deletions lib/models/extension.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 024599c

Please sign in to comment.