AlauStone commited on
Commit
73983f1
·
verified ·
1 Parent(s): 17fa58a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -9
app.py CHANGED
@@ -393,17 +393,48 @@ def _inject_action_js(content, msg_id):
393
  (function() {{
394
  const msgContent = `{escaped}`;
395
  let speaking = false;
 
396
 
397
- const copyBtn = window.parent.document.getElementById('copy_btn_{msg_id}');
398
- const shareBtn = window.parent.document.getElementById('share_btn_{msg_id}');
399
- const ttsBtn = window.parent.document.getElementById('tts_btn_{msg_id}');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
400
 
401
  if (copyBtn) {{
402
  copyBtn.onclick = function() {{
403
- navigator.clipboard.writeText(msgContent).then(() => {{
404
  copyBtn.textContent = '✅ 已复制';
405
  setTimeout(() => {{ copyBtn.textContent = '📋 复制'; }}, 1500);
406
- }}).catch(() => {{ alert('复制失败'); }});
 
 
 
407
  }};
408
  copyBtn.onmouseover = function() {{ this.style.background='#f0f0f0'; }};
409
  copyBtn.onmouseout = function() {{ this.style.background='none'; }};
@@ -411,12 +442,15 @@ def _inject_action_js(content, msg_id):
411
 
412
  if (shareBtn) {{
413
  shareBtn.onclick = function() {{
414
- if (navigator.share) {{
 
415
  navigator.share({{ title: '智答AI助手', text: msgContent }}).catch(() => {{}});
416
  }} else {{
417
- navigator.clipboard.writeText(msgContent).then(() => {{
418
  shareBtn.textContent = '✅ 已复制';
419
  setTimeout(() => {{ shareBtn.textContent = '🔗 分享'; }}, 1500);
 
 
420
  }});
421
  }}
422
  }};
@@ -426,8 +460,10 @@ def _inject_action_js(content, msg_id):
426
 
427
  if (ttsBtn) {{
428
  ttsBtn.onclick = function() {{
 
 
429
  if (speaking) {{
430
- window.speechSynthesis.cancel();
431
  speaking = false;
432
  ttsBtn.textContent = '🔊 播报';
433
  }} else {{
@@ -436,7 +472,7 @@ def _inject_action_js(content, msg_id):
436
  utterance.rate = 1.0;
437
  utterance.onend = () => {{ speaking = false; ttsBtn.textContent = '🔊 播报'; }};
438
  utterance.onerror = () => {{ speaking = false; ttsBtn.textContent = '🔊 播报'; }};
439
- window.speechSynthesis.speak(utterance);
440
  speaking = true;
441
  ttsBtn.textContent = '⏹️ 停止';
442
  }}
 
393
  (function() {{
394
  const msgContent = `{escaped}`;
395
  let speaking = false;
396
+ const parentDoc = window.parent.document;
397
 
398
+ // 兼容性复制函数(支持 iframe 环境)
399
+ function copyToClipboard(text) {{
400
+ // 方法1: 现代 API
401
+ if (navigator.clipboard && window.isSecureContext) {{
402
+ return navigator.clipboard.writeText(text);
403
+ }}
404
+ // 方法2: 传统方法(textarea)
405
+ return new Promise((resolve, reject) => {{
406
+ const textarea = parentDoc.createElement('textarea');
407
+ textarea.value = text;
408
+ textarea.style.position = 'fixed';
409
+ textarea.style.left = '-9999px';
410
+ textarea.style.top = '-9999px';
411
+ parentDoc.body.appendChild(textarea);
412
+ textarea.focus();
413
+ textarea.select();
414
+ try {{
415
+ const ok = parentDoc.execCommand('copy');
416
+ parentDoc.body.removeChild(textarea);
417
+ ok ? resolve() : reject();
418
+ }} catch (e) {{
419
+ parentDoc.body.removeChild(textarea);
420
+ reject(e);
421
+ }}
422
+ }});
423
+ }}
424
+
425
+ const copyBtn = parentDoc.getElementById('copy_btn_{msg_id}');
426
+ const shareBtn = parentDoc.getElementById('share_btn_{msg_id}');
427
+ const ttsBtn = parentDoc.getElementById('tts_btn_{msg_id}');
428
 
429
  if (copyBtn) {{
430
  copyBtn.onclick = function() {{
431
+ copyToClipboard(msgContent).then(() => {{
432
  copyBtn.textContent = '✅ 已复制';
433
  setTimeout(() => {{ copyBtn.textContent = '📋 复制'; }}, 1500);
434
+ }}).catch(() => {{
435
+ // 最后方案:提示用户手动复制
436
+ prompt('请手动复制以下内容:', msgContent.substring(0, 500));
437
+ }});
438
  }};
439
  copyBtn.onmouseover = function() {{ this.style.background='#f0f0f0'; }};
440
  copyBtn.onmouseout = function() {{ this.style.background='none'; }};
 
442
 
443
  if (shareBtn) {{
444
  shareBtn.onclick = function() {{
445
+ // 分享功能:优先用 Web Share API,否则复制
446
+ if (navigator.share && !window.parent.frames.length) {{
447
  navigator.share({{ title: '智答AI助手', text: msgContent }}).catch(() => {{}});
448
  }} else {{
449
+ copyToClipboard(msgContent).then(() => {{
450
  shareBtn.textContent = '✅ 已复制';
451
  setTimeout(() => {{ shareBtn.textContent = '🔗 分享'; }}, 1500);
452
+ }}).catch(() => {{
453
+ prompt('请手动复制分享:', msgContent.substring(0, 500));
454
  }});
455
  }}
456
  }};
 
460
 
461
  if (ttsBtn) {{
462
  ttsBtn.onclick = function() {{
463
+ const synth = window.speechSynthesis || window.parent.speechSynthesis;
464
+ if (!synth) {{ alert('浏览器不支持语音播报'); return; }}
465
  if (speaking) {{
466
+ synth.cancel();
467
  speaking = false;
468
  ttsBtn.textContent = '🔊 播报';
469
  }} else {{
 
472
  utterance.rate = 1.0;
473
  utterance.onend = () => {{ speaking = false; ttsBtn.textContent = '🔊 播报'; }};
474
  utterance.onerror = () => {{ speaking = false; ttsBtn.textContent = '🔊 播报'; }};
475
+ synth.speak(utterance);
476
  speaking = true;
477
  ttsBtn.textContent = '⏹️ 停止';
478
  }}