""" 改進錯誤訊息顯示 - 使錯誤訊息可完整顯示和複製 """ with open('index.html', 'r', encoding='utf-8') as f: content = f.read() # 備份 with open('index.html.backup2', 'w', encoding='utf-8') as f: f.write(content) # 找到錯誤處理的 alert 並替換為更好的顯示方式 old_error_handling = ''' } catch (error) { console.error("Error calling LLM API:", error); alert(`AI 生成錯誤: ${error.message}\\n\\n請確保:\\n1. Flask 後端已啟動 (python app_updated.py)\\n2. 已在 .env 文件中配置 LLM API Key\\n3. 網路連線正常`); throw error; }''' new_error_handling = ''' } catch (error) { console.error("Error calling LLM API:", error); // 嘗試解析更詳細的錯誤訊息 let errorDetails = error.message; try { // 如果錯誤訊息是 JSON 格式,嘗試美化顯示 const errorJson = JSON.parse(error.message); errorDetails = JSON.stringify(errorJson, null, 2); } catch (e) { // 不是 JSON,使用原始訊息 } // 創建可複製的錯誤對話框 showCopyableError({ title: 'AI 生成錯誤', message: error.message, details: errorDetails, suggestions: [ 'Flask 後端已啟動 (python start_server.py)', '已在 .env 文件中配置有效的 LLM API Key', '網路連線正常', '嘗試使用不同的 LLM API (DeepSeek 或 OpenAI)' ] }); throw error; }''' # 替換 new_content = content.replace(old_error_handling, new_error_handling) if new_content == content: print("WARNING: Pattern not found, content not changed") else: print("SUCCESS: Error handling improved") # 添加 showCopyableError 函數(如果還沒有) if 'function showCopyableError' not in new_content: # 在 前添加新函數 error_display_function = ''' // 顯示可複製的錯誤訊息 function showCopyableError(options) { const { title, message, details, suggestions } = options; // 創建對話框 const modal = document.createElement('div'); modal.style.cssText = ` position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.7); display: flex; align-items: center; justify-content: center; z-index: 10000; animation: fadeIn 0.3s; `; modal.innerHTML = `
`; document.body.appendChild(modal); // 點擊背景關閉 modal.addEventListener('click', (e) => { if (e.target === modal) { modal.remove(); } }); } // 複製錯誤訊息到剪貼板 function copyErrorDetails() { const text = document.getElementById('errorDetailsText').textContent; navigator.clipboard.writeText(text).then(() => { alert('錯誤訊息已複製到剪貼板!'); }).catch(err => { // Fallback: 選取文字 const range = document.createRange(); range.selectNode(document.getElementById('errorDetailsText')); window.getSelection().removeAllRanges(); window.getSelection().addRange(range); try { document.execCommand('copy'); alert('錯誤訊息已複製到剪貼板!'); } catch (e) { alert('複製失敗,請手動選取並複製'); } }); } ''' new_content = new_content.replace(' ', error_display_function + '\n ') print("Added showCopyableError function") # 寫回 with open('index.html', 'w', encoding='utf-8') as f: f.write(new_content) print("\nDone! Improvements:") print("1. Error messages now show in a modal dialog") print("2. Full error details are expandable") print("3. Error details can be copied to clipboard") print("4. Better formatting and readability") print("\nPlease reload the page (Ctrl+F5) to see the changes")