From c9abdbe12d10f7bc753ae8a3426e42b981910fc3 Mon Sep 17 00:00:00 2001
From: ucmao <294323976@qq.com>
Date: Fri, 11 Sep 2026 07:26:56 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A7=A3=E6=9E=90=E5=A4=B1?=
=?UTF-8?q?=E8=B4=A5=E6=8F=90=E7=A4=BA=E4=B8=8E=E7=94=A8=E6=88=B7=E5=BC=95?=
=?UTF-8?q?=E5=AF=BC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
templates/landing.html | 256 ++++++++++++++++++++++++++-
tests/test_landing_error_friendly.py | 46 +++++
2 files changed, 299 insertions(+), 3 deletions(-)
create mode 100644 tests/test_landing_error_friendly.py
diff --git a/templates/landing.html b/templates/landing.html
index 97d02cf..fb027a6 100644
--- a/templates/landing.html
+++ b/templates/landing.html
@@ -66,7 +66,33 @@
LIVE PARSER
粘贴链接,立即解析
支持分享短链和包含链接的完整分享文案。
正在识别平台并获取媒体信息…
-
+
+
+
+
+ {{ icon('alert', 'w-5 h-5') }}
+
+
+
+
暂时无法解析该内容
+
+
+
+
+ 排查建议:
+
+
+
+
+
+
+
+
+
@@ -377,7 +403,18 @@
}, duration);
}
- function clearParser(){input.value='';input.focus()}
+ function clearParser(){
+ input.value = '';
+ document.getElementById('errorState').classList.add('hidden');
+ input.focus();
+ }
+
+ function fillSampleUrl(){
+ input.value = 'https://www.bilibili.com/video/BV1xx411c7mD';
+ document.getElementById('errorState').classList.add('hidden');
+ input.focus();
+ showToast('已填入示例链接,可直接点击“开始解析”', 'success', 2500);
+ }
async function pasteFromClipboard(){
try {
input.value = await navigator.clipboard.readText();
@@ -923,7 +960,220 @@
document.getElementById('resultState').classList.remove('hidden');
}
- async function handleParse(){const text=input.value.trim(),button=document.getElementById('parseBtn'),loading=document.getElementById('loadingState'),error=document.getElementById('errorState'),result=document.getElementById('resultState');if(!text||button.disabled){if(!text)input.focus();return}button.disabled=true;loading.classList.remove('hidden');error.classList.add('hidden');result.classList.add('hidden');const hpWebsite=document.getElementById('hpWebsite')?.value||'';try{const response=await fetch('/api/parse',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({text,website:hpWebsite})});const payload=await response.json();if(response.ok&&payload.succ){renderResult(payload.data);result.scrollIntoView({behavior:'smooth',block:'nearest'})}else{document.getElementById('errorMsg').textContent=payload.retdesc||'请检查链接后重试';error.classList.remove('hidden')}}catch{document.getElementById('errorMsg').textContent='网络异常,请稍后重试';error.classList.remove('hidden')}finally{loading.classList.add('hidden');let countdown=3;const originalText='开始解析';button.textContent=`请稍候 (${countdown}s)`;const timer=setInterval(()=>{countdown--;if(countdown>0){button.textContent=`请稍候 (${countdown}s)`}else{clearInterval(timer);button.disabled=false;button.textContent=originalText}},1000)}}
+ function formatUserErrorMessage(payload) {
+ if (!payload || typeof payload !== 'object') {
+ return {
+ title: '解析未完成',
+ message: '未能成功获取媒体信息,请检查链接后重试。',
+ hint: '建议确认原作品在对应 App 中是否公开可见。',
+ tag: '未知状态'
+ };
+ }
+ const code = payload.error_code || '';
+ const rawDesc = typeof payload.retdesc === 'string' ? payload.retdesc.trim() : '';
+
+ // 1. 凭据/Cookie 要求(敏感信息脱敏,屏蔽环境变量名与服务端配置)
+ if (code.endsWith('_COOKIE_REQUIRED') || /cookie|凭证|yuanbao_cookie/i.test(rawDesc)) {
+ return {
+ title: '目标平台在线体验受限',
+ message: '该平台当前启用了较严格的访问安全策略或需要环境授权,首页在线体验暂无法直接提取。',
+ hint: '建议尝试解析抖音、B站、快手等平台公开作品体验。',
+ tag: '平台受限'
+ };
+ }
+
+ // 2. 链接与文本格式
+ if (code === 'INVALID_TEXT' || code === 'URL_NOT_FOUND') {
+ return {
+ title: '未检测到有效链接',
+ message: '请粘贴包含完整 HTTP/HTTPS 分享链接的文案后再试。',
+ hint: '支持直接粘贴客户端复制的完整分享口令(如“7.12 xxx 复制此链接打开…”)。',
+ tag: '链接格式'
+ };
+ }
+ if (code === 'TEXT_TOO_LONG') {
+ return {
+ title: '分享内容过长',
+ message: '粘贴的文本长度超出了单次解析限制(2000 字符以内)。',
+ hint: '建议仅保留核心分享链接或精简前后的多余文字。',
+ tag: '超长限制'
+ };
+ }
+ if (code === 'REDIRECT_FAILED') {
+ return {
+ title: '分享链接无法访问',
+ message: '无法连通或识别该分享链接,可能是短链已失效或目标平台拒绝了访问。',
+ hint: '请在手机浏览器或原 App 中确认该链接是否能正常打开。',
+ tag: '访问失败'
+ };
+ }
+ if (code === 'PLATFORM_NOT_SUPPORTED') {
+ return {
+ title: '暂未支持该平台',
+ message: '当前尚未支持提取该平台的内容,平台能力会持续更新完善。',
+ hint: '支持的平台列表可查阅下方“平台能力”板块。',
+ tag: '平台支持'
+ };
+ }
+ if (code === 'PLATFORM_DISABLED') {
+ return {
+ title: '该平台解析暂不可用',
+ message: '该平台的解析服务目前处于维护或临时下线状态。',
+ hint: '请尝试体验其他平台的作品,或稍后再试。',
+ tag: '维护中'
+ };
+ }
+
+ // 3. 作品自身状态(删除/私密/无媒体)
+ if (code === 'MEDIA_DELETED_OR_PRIVATE') {
+ return {
+ title: '作品不可查看',
+ message: '该内容可能已被原作者删除,或者属于仅自己/好友可见的私密作品。',
+ hint: '请确保该作品在对应平台为全网公开可见状态。',
+ tag: '作品状态'
+ };
+ }
+ if (code === 'NO_MEDIA_IN_CONTENT') {
+ return {
+ title: '未包含多媒体资源',
+ message: '该分享内容仅包含纯文字对话,未检测到可提取的视频、音频或图片。',
+ hint: '请尝试分享包含图片或视频的作品。',
+ tag: '无多媒体'
+ };
+ }
+
+ // 4. 频率与服务状态
+ if (code === 'RATE_LIMITED') {
+ return {
+ title: '体验频率受限',
+ message: '为保障公共演示体验稳定,首页在线体验已达到短时频控上限。',
+ hint: '请稍候 1 分钟后再试;或注册账号获取专属 API Key 进行调用。',
+ tag: '频控限制'
+ };
+ }
+ if (code === 'API_DISABLED' || code === 'DEMO_DISABLED') {
+ return {
+ title: '在线体验维护中',
+ message: '在线解析体验当前正在系统维护中,暂时无法处理体验请求。',
+ hint: '请稍后再试或联系系统管理员。',
+ tag: '服务维护'
+ };
+ }
+ if (code === 'INVALID_REQUEST') {
+ return {
+ title: '请求格式异常',
+ message: '体验请求参数异常,请刷新页面后重新输入。',
+ hint: '建议直接粘贴分享链接重试。',
+ tag: '参数异常'
+ };
+ }
+ if (code === 'MEDIA_NOT_FOUND' || code === 'INTERNAL_ERROR') {
+ return {
+ title: '提取媒体内容失败',
+ message: '暂时未能成功提取到媒体直链,可能是目标平台临时风控或网络波动。',
+ hint: '建议稍后重试,或尝试其他平台/作者的公开作品。',
+ tag: '提取失败'
+ };
+ }
+
+ // 5. 兜底敏感词与技术黑话过滤
+ if (/cookie|json|sql|traceback|500|exception|bearer|token|auth/i.test(rawDesc)) {
+ return {
+ title: '解析暂时遇到问题',
+ message: '未能成功获取媒体信息,请检查链接后重试。',
+ hint: '建议确认原作品在对应 App 中是否公开可见。',
+ tag: '解析异常'
+ };
+ }
+
+ return {
+ title: '解析未完成',
+ message: rawDesc || '未能成功获取媒体信息,请检查链接后重试。',
+ hint: '建议确认原作品在对应 App 中是否公开可见。',
+ tag: '提示'
+ };
+ }
+
+ function displayError(info) {
+ const error = document.getElementById('errorState');
+ const errorTitle = document.getElementById('errorTitle');
+ const errorMsg = document.getElementById('errorMsg');
+ const errorHint = document.getElementById('errorHint');
+ const errorTag = document.getElementById('errorTag');
+
+ if (errorTitle) errorTitle.textContent = info.title || '解析失败';
+ if (errorMsg) errorMsg.textContent = info.message || '请检查链接后重试';
+ if (errorHint) errorHint.textContent = info.hint || '建议确认原作品在对应 App 中是否公开可见。';
+ if (errorTag) {
+ if (info.tag) {
+ errorTag.textContent = info.tag;
+ errorTag.classList.remove('hidden');
+ } else {
+ errorTag.classList.add('hidden');
+ }
+ }
+ error.classList.remove('hidden');
+ error.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
+ }
+
+ async function handleParse(){
+ const text = input.value.trim();
+ const button = document.getElementById('parseBtn');
+ const loading = document.getElementById('loadingState');
+ const error = document.getElementById('errorState');
+ const result = document.getElementById('resultState');
+
+ if (!text || button.disabled) {
+ if (!text) {
+ displayError(formatUserErrorMessage({ error_code: 'INVALID_TEXT' }));
+ input.focus();
+ }
+ return;
+ }
+
+ button.disabled = true;
+ loading.classList.remove('hidden');
+ error.classList.add('hidden');
+ result.classList.add('hidden');
+ const hpWebsite = document.getElementById('hpWebsite')?.value || '';
+
+ try {
+ const response = await fetch('/api/parse', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ text, website: hpWebsite })
+ });
+ const payload = await response.json();
+ if (response.ok && payload.succ) {
+ renderResult(payload.data);
+ result.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
+ } else {
+ displayError(formatUserErrorMessage(payload));
+ }
+ } catch {
+ displayError({
+ title: '网络连接异常',
+ message: '无法连接到解析服务,请检查网络后重试。',
+ hint: '建议检查网络状况或刷新页面后再试。',
+ tag: '网络异常'
+ });
+ } finally {
+ loading.classList.add('hidden');
+ let countdown = 3;
+ const originalText = '开始解析';
+ button.textContent = `请稍候 (${countdown}s)`;
+ const timer = setInterval(() => {
+ countdown--;
+ if (countdown > 0) {
+ button.textContent = `请稍候 (${countdown}s)`;
+ } else {
+ clearInterval(timer);
+ button.disabled = false;
+ button.textContent = originalText;
+ }
+ }, 1000);
+ }
+ }
input.addEventListener('keydown',event=>{if((event.metaKey||event.ctrlKey)&&event.key==='Enter')handleParse()});
fetch('https://api.github.com/repos/ucmao/media-parser',{headers:{Accept:'application/vnd.github+json'}}).then(res=>res.ok?res.json():Promise.reject()).then(repo=>{if(repo&&repo.stargazers_count!==undefined){const countStr=new Intl.NumberFormat('en',{notation:'compact',maximumFractionDigits:1}).format(repo.stargazers_count);const el=document.getElementById('githubStars'),badge=document.getElementById('starCountBadge');if(el)el.textContent=countStr;if(badge){badge.classList.remove('hidden');badge.classList.add('inline-flex')}const navEl=document.getElementById('navGithubStars'),navBadge=document.getElementById('navStarBadge');if(navEl)navEl.textContent=countStr;if(navBadge){navBadge.classList.remove('hidden');navBadge.classList.add('inline-flex')}}}).catch(()=>{});
diff --git a/tests/test_landing_error_friendly.py b/tests/test_landing_error_friendly.py
new file mode 100644
index 0000000..79d3e54
--- /dev/null
+++ b/tests/test_landing_error_friendly.py
@@ -0,0 +1,46 @@
+import unittest
+from app import app
+
+
+class LandingErrorFriendlyTest(unittest.TestCase):
+ def setUp(self):
+ app.testing = True
+ self.client = app.test_client()
+
+ def test_landing_page_has_friendly_error_elements(self):
+ """验证首页渲染包含结构化的友好错误卡片及操作组件"""
+ response = self.client.get("/")
+ self.assertEqual(response.status_code, 200)
+ html = response.get_data(as_text=True)
+
+ # 检查错误卡片核心 DOM 结构
+ self.assertIn('id="errorState"', html)
+ self.assertIn('id="errorTitle"', html)
+ self.assertIn('id="errorMsg"', html)
+ self.assertIn('id="errorHintBox"', html)
+ self.assertIn('id="errorHint"', html)
+ self.assertIn('id="errorTag"', html)
+
+ # 检查快捷操作与示例填充按钮
+ self.assertIn("fillSampleUrl()", html)
+ self.assertIn("填入示例链接试一试", html)
+ self.assertIn("清空重试", html)
+
+ def test_landing_page_script_contains_error_formatting_and_masking(self):
+ """验证首页脚本包含错误码友好转译与 Cookie 等运维黑话脱敏逻辑"""
+ response = self.client.get("/")
+ self.assertEqual(response.status_code, 200)
+ html = response.get_data(as_text=True)
+
+ # 检查转译函数与敏感信息脱敏逻辑
+ self.assertIn("function formatUserErrorMessage(payload)", html)
+ self.assertIn("function displayError(info)", html)
+ self.assertIn("function fillSampleUrl()", html)
+ self.assertIn("_COOKIE_REQUIRED", html)
+ self.assertIn("目标平台在线体验受限", html)
+ self.assertIn("未检测到有效链接", html)
+ self.assertIn("建议尝试解析抖音、B站、快手等平台公开作品体验。", html)
+
+
+if __name__ == "__main__":
+ unittest.main()