generateMediaItemFromWebView method
- {required AppModel appModel,
- required InAppWebViewController controller}
Generate a media item given a WebView.
Implementation
Future<MediaItem?> generateMediaItemFromWebView({
required AppModel appModel,
required InAppWebViewController controller,
}) async {
String title = await controller.getTitle() ?? '';
Uri url = (await controller.getUrl())!..removeFragment();
MediaItem? item = appModel
.getMediaTypeHistory(mediaType: mediaType)
.firstWhereOrNull((item) => item.mediaIdentifier == url.toString());
if (item != null) {
return item;
}
if (title.endsWith('| mokuro')) {
title = title.replaceAll('| mokuro', '');
}
bool isMokuroPage = await controller.evaluateJavascript(
source:
'document.body.getElementsByClassName("pageContainer").length != 0 && document.getElementById("popupAbout") != null;');
if (!isMokuroPage) {
return null;
}
int pageCount = await controller.evaluateJavascript(source: '''
document.body.getElementsByClassName('pageContainer').length
''');
String relativeUrl = await controller.evaluateJavascript(source: '''
var bgImage = document.body.getElementsByClassName('pageContainer')[0].style.backgroundImage;
bgImage.substring(5, bgImage.length - 2);
''');
String imageUrl = getImageUrl(
relativeUrl: relativeUrl,
mediaIdentifier: url.toString(),
);
return MediaItem(
canDelete: true,
canEdit: true,
title: title,
mediaIdentifier: url.toString(),
position: 0,
duration: pageCount,
imageUrl: imageUrl,
mediaTypeIdentifier: mediaType.uniqueKey,
mediaSourceIdentifier: uniqueKey,
);
}