Skip to content

Commit

Permalink
feat(html): Init netease iframe player parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
realth000 committed Sep 24, 2024
1 parent 35ab824 commit abe4563
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/utils/html/html_muncher.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:collection/collection.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:tsdm_client/constants/layout.dart';
import 'package:tsdm_client/extensions/build_context.dart';
import 'package:tsdm_client/extensions/universal_html.dart';
import 'package:tsdm_client/shared/models/models.dart';
Expand All @@ -18,6 +19,7 @@ import 'package:tsdm_client/widgets/card/spoiler_card.dart';
import 'package:tsdm_client/widgets/network_indicator_image.dart';
import 'package:tsdm_client/widgets/quoted_text.dart';
import 'package:universal_html/html.dart' as uh;
import 'package:url_launcher/url_launcher_string.dart';

/// Use the same span to append line break.
const emptySpan = TextSpan(text: '\n');
Expand Down Expand Up @@ -187,6 +189,10 @@ final class _Muncher with LoggerMixin {
/// Map to store div classes and corresponding munch functions.
Map<String, List<InlineSpan>? Function(uh.Element)>? _divMap;

/// Regex to match netease player iframe.
final _neteasePlayerRe =
RegExp(r'//music\.163\.com/outchain/player\?.+id=(?<id>\d+).*');

List<InlineSpan>? _munch(uh.Element rootElement) {
final spanList = <InlineSpan>[];

Expand Down Expand Up @@ -311,6 +317,7 @@ final class _Muncher with LoggerMixin {
'hr' => _buildHr(node),
'pre' => _buildPre(node),
'details' => _buildDetails(node),
'iframe' => _buildIframe(node),
'ignore_js_op' ||
'table' ||
'tbody' ||
Expand Down Expand Up @@ -992,6 +999,35 @@ final class _Muncher with LoggerMixin {
];
}
List<InlineSpan>? _buildIframe(uh.Element element) {
final neteasePlayerId = _neteasePlayerRe
.firstMatch(element.attributes['src'] ?? '')
?.namedGroup('id');
if (neteasePlayerId != null) {
// Recognized netease player iframe.
// But only a song id here, nothing with song title/artist or other info.
// TODO: Check and get song info.
final url = 'https://music.163.com/#/song?id=$neteasePlayerId';
return [
WidgetSpan(
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: () async => launchUrlString(url),
child: Card(
child: Padding(
padding: edgeInsetsL12T12R12B12,
child: Text('netease music here (id=$neteasePlayerId)'),
),
),
),
),
),
];
}
return null;
}
/* Setup Functions */
/// Try parse color from [element].
Expand Down

0 comments on commit abe4563

Please sign in to comment.