generated from dellevin/template
适配深色模式
This commit is contained in:
@@ -5,9 +5,9 @@ import 'package:webview_flutter/webview_flutter.dart';
|
||||
/// 豆瓣影视WebView页面 - 用于抓取影视信息
|
||||
class DoubanWebViewPage extends StatefulWidget {
|
||||
final String url;
|
||||
|
||||
|
||||
const DoubanWebViewPage({super.key, required this.url});
|
||||
|
||||
|
||||
@override
|
||||
State<DoubanWebViewPage> createState() => _DoubanWebViewPageState();
|
||||
}
|
||||
@@ -16,21 +16,21 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
late WebViewController _controller;
|
||||
bool _isLoading = true;
|
||||
bool _canExtract = false;
|
||||
bool _isExtracting = false; // 防止重复提取
|
||||
|
||||
bool _isExtracting = false; // 防止重复提取
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initWebView();
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// 清理 WebView 资源
|
||||
_controller.loadRequest(Uri.parse('about:blank'));
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
|
||||
void _initWebView() {
|
||||
_controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
@@ -55,23 +55,26 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
)
|
||||
..loadRequest(Uri.parse(widget.url));
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
backgroundColor: colors.surface,
|
||||
appBar: AppBar(
|
||||
title: const Text('豆瓣影视'),
|
||||
leading: _buildBackButton(),
|
||||
actions: [
|
||||
// 提取按钮 - 始终显示
|
||||
_buildActionButton(
|
||||
colors: colors,
|
||||
icon: Icons.auto_fix_high_outlined,
|
||||
onPressed: _showExtractedInfo,
|
||||
tooltip: '提取信息',
|
||||
),
|
||||
// 刷新按钮
|
||||
_buildActionButton(
|
||||
colors: colors,
|
||||
icon: Icons.refresh,
|
||||
onPressed: () => _controller.reload(),
|
||||
tooltip: '刷新',
|
||||
@@ -83,16 +86,14 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
children: [
|
||||
WebViewWidget(controller: _controller),
|
||||
// 加载指示器
|
||||
if (_isLoading)
|
||||
const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
if (_isLoading) const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/// 构建返回按钮
|
||||
Widget _buildBackButton() {
|
||||
return Container(
|
||||
@@ -121,6 +122,7 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
|
||||
/// 构建右上角操作按钮
|
||||
Widget _buildActionButton({
|
||||
required ColorScheme colors,
|
||||
required IconData icon,
|
||||
required VoidCallback onPressed,
|
||||
required String tooltip,
|
||||
@@ -138,75 +140,86 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: Icon(icon, color: const Color(0xFF1A1A1A), size: 22),
|
||||
child: Icon(icon, color: colors.onSurface, size: 22),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/// 显示提取的信息对话框
|
||||
Future<void> _showExtractedInfo() async {
|
||||
// 先提取信息
|
||||
final movieInfo = await _extractMovieInfo();
|
||||
if (movieInfo == null) return;
|
||||
|
||||
|
||||
// 显示提取的信息
|
||||
if (mounted) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
title: const Text(
|
||||
'提取的影视信息',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildInfoRow('标题', movieInfo['title']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow('导演', movieInfo['director']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow('类型', movieInfo['genres']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow('上映日期', movieInfo['releaseDate']?.toString() ?? '未提取到'),
|
||||
if (movieInfo['summary'] != null)
|
||||
_buildInfoRow('简介', movieInfo['summary'].toString().substring(0,
|
||||
movieInfo['summary'].toString().length > 100 ? 100 : movieInfo['summary'].toString().length) + '...'),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text(
|
||||
'取消',
|
||||
style: TextStyle(color: Color(0xFF999999)),
|
||||
builder: (ctx) {
|
||||
final colors = Theme.of(ctx).colorScheme;
|
||||
return AlertDialog(
|
||||
backgroundColor: colors.surface,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
title: Text(
|
||||
'提取的影视信息',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onSurface,
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(context, movieInfo);
|
||||
},
|
||||
child: const Text(
|
||||
'使用此信息',
|
||||
style: TextStyle(color: Color(0xFF1A1A1A), fontWeight: FontWeight.w600),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildInfoRow(colors, '标题', movieInfo['title']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '导演', movieInfo['director']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '类型', movieInfo['genres']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '上映日期', movieInfo['releaseDate']?.toString() ?? '未提取到'),
|
||||
if (movieInfo['summary'] != null)
|
||||
_buildInfoRow(
|
||||
colors,
|
||||
'简介',
|
||||
movieInfo['summary'].toString().substring(
|
||||
0,
|
||||
movieInfo['summary'].toString().length > 100
|
||||
? 100
|
||||
: movieInfo['summary'].toString().length) +
|
||||
'...'),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: Text(
|
||||
'取消',
|
||||
style: TextStyle(color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(ctx);
|
||||
Navigator.pop(context, movieInfo);
|
||||
},
|
||||
child: Text(
|
||||
'使用此信息',
|
||||
style: TextStyle(
|
||||
color: colors.onSurface, fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 构建信息行
|
||||
Widget _buildInfoRow(String label, String value) {
|
||||
Widget _buildInfoRow(ColorScheme colors, String label, String value) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Row(
|
||||
@@ -216,18 +229,18 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
width: 64,
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF999999),
|
||||
color: colors.onSurface.withValues(alpha: 0.4),
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
value,
|
||||
style: const TextStyle(
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF1A1A1A),
|
||||
color: colors.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -240,10 +253,10 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
Future<Map<String, dynamic>?> _extractMovieInfo() async {
|
||||
// 检查是否已提取过,避免重复点击
|
||||
if (_isExtracting) return null;
|
||||
|
||||
|
||||
try {
|
||||
_isExtracting = true;
|
||||
|
||||
|
||||
// 显示加载提示
|
||||
showDialog(
|
||||
context: context,
|
||||
@@ -252,16 +265,16 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
// 执行JavaScript代码提取页面信息
|
||||
final result = await _controller.runJavaScriptReturningResult(r'''
|
||||
(function() {
|
||||
const info = {};
|
||||
|
||||
|
||||
// 获取标题 - 移动版页面
|
||||
const titleEl = document.querySelector('.sub-title');
|
||||
info.title = titleEl ? titleEl.textContent.trim() : '';
|
||||
|
||||
|
||||
// 获取年份 - 从 original-title 中提取
|
||||
const originalTitleEl = document.querySelector('.sub-original-title');
|
||||
if (originalTitleEl) {
|
||||
@@ -270,7 +283,7 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
} else {
|
||||
info.year = '';
|
||||
}
|
||||
|
||||
|
||||
// 获取封面图 - 从 sub-cover 中的 img 标签获取
|
||||
const coverEl = document.querySelector('.sub-cover img');
|
||||
if (coverEl) {
|
||||
@@ -283,11 +296,11 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
} else {
|
||||
info.coverUrl = '';
|
||||
}
|
||||
|
||||
|
||||
// 获取评分 - 移动版可能在 mark-item 中
|
||||
const ratingEl = document.querySelector('.rating-num') || document.querySelector('.score');
|
||||
info.rating = ratingEl ? ratingEl.textContent.trim() : '';
|
||||
|
||||
|
||||
// 获取导演 - 从演职员列表中找
|
||||
const directorEl = document.querySelector('.movie-celebrities .item__celebrity .role');
|
||||
if (directorEl && directorEl.textContent.includes('导演')) {
|
||||
@@ -296,7 +309,7 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
} else {
|
||||
info.director = '';
|
||||
}
|
||||
|
||||
|
||||
// 获取编剧 - 从演职员列表中找(匹配"编剧"或"剧本")
|
||||
const writerEls = document.querySelectorAll('.movie-celebrities .item__celebrity');
|
||||
const writers = [];
|
||||
@@ -308,7 +321,7 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
}
|
||||
});
|
||||
info.writers = writers;
|
||||
|
||||
|
||||
// 获取主演- 从演职员列表中找前5个
|
||||
const actorEls = document.querySelectorAll('.movie-celebrities .item__celebrity');
|
||||
const actors = [];
|
||||
@@ -316,10 +329,10 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
const roleEl = el
|
||||
.querySelector('.role');
|
||||
if (roleEl && (
|
||||
roleEl.textContent.includes('配音') ||
|
||||
roleEl.textContent.includes('主演') ||
|
||||
roleEl.textContent.includes('配音') ||
|
||||
roleEl.textContent.includes('主演') ||
|
||||
roleEl.textContent.includes('演员') ||
|
||||
roleEl.textContent.includes('参演') ||
|
||||
roleEl.textContent.includes('参演') ||
|
||||
roleEl.textContent.includes('饰')
|
||||
)) {
|
||||
const nameEl = el.querySelector('.name');
|
||||
@@ -327,20 +340,20 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
}
|
||||
});
|
||||
info.actors = actors;
|
||||
|
||||
|
||||
// 获取类型 - 从 sub-meta 或标签中提取
|
||||
const metaEl = document.querySelector('.sub-meta');
|
||||
if (metaEl) {
|
||||
const metaText = metaEl.textContent;
|
||||
const parts = metaText.split('/').map(s => s.trim());
|
||||
// 过滤出类型(通常是中文,不是日期,不是时长)
|
||||
info.genres = parts.filter(p =>
|
||||
info.genres = parts.filter(p =>
|
||||
p && !p.match(/^\d{4}/) && !p.includes('分钟') && !p.includes('上映')
|
||||
).join(',');
|
||||
} else {
|
||||
info.genres = '';
|
||||
}
|
||||
|
||||
|
||||
// 获取上映日期
|
||||
if (metaEl) {
|
||||
const dateMatch = metaEl.textContent.match(/(\d{4}-\d{2}-\d{2})/);
|
||||
@@ -348,7 +361,7 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
} else {
|
||||
info.releaseDate = '';
|
||||
}
|
||||
|
||||
|
||||
// 获取简介
|
||||
const summaryEl = document.querySelector('.subject-intro p');
|
||||
if (summaryEl) {
|
||||
@@ -356,7 +369,7 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
} else {
|
||||
info.summary = '';
|
||||
}
|
||||
|
||||
|
||||
// 获取别名 - 从 original-title 中提取(去掉年份)
|
||||
if (originalTitleEl) {
|
||||
const fullText = originalTitleEl.textContent.trim();
|
||||
@@ -364,14 +377,14 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
} else {
|
||||
info.alternateTitles = [];
|
||||
}
|
||||
|
||||
|
||||
return JSON.stringify(info);
|
||||
})()
|
||||
''');
|
||||
|
||||
|
||||
// 关闭加载提示
|
||||
if (mounted) Navigator.pop(context);
|
||||
|
||||
|
||||
// 解析提取的信息
|
||||
// result 是 JavaScript 执行结果,已经是 JSON 字符串(带引号的)
|
||||
final String jsonStr = result.toString();
|
||||
@@ -380,12 +393,12 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
? jsonDecode(jsonStr) as String
|
||||
: jsonStr;
|
||||
final Map<String, dynamic> movieInfo = jsonDecode(cleanJson) as Map<String, dynamic>;
|
||||
|
||||
|
||||
return movieInfo;
|
||||
} catch (e) {
|
||||
// 关闭加载提示
|
||||
if (mounted) Navigator.pop(context);
|
||||
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('提取信息失败: $e')),
|
||||
|
||||
Reference in New Issue
Block a user