// frontend/script.js // 當 DOM (文件物件模型) 完全載入後才執行裡面的程式碼 document.addEventListener('DOMContentLoaded', () => { // 取得 HTML 元素:輸入、按鈕、顯示區塊。document.getElementById('對應的html元素id') const inputTextArea = document.getElementById('predict_info'); // 輸入訊息的文字區域 const inputButton = document.getElementById('detect_button'); // 檢測按鈕 const clearButton = document.getElementById('clear_button'); // 清除按鈕 // 取得圖片上傳欄位與圖片按鈕 const imageInput = document.getElementById('imageInput'); const imageButton = document.getElementById('image_button'); // 取得顯示結果的 HTML 元素 const normalOrScam = document.getElementById('is_scam'); // 顯示正常或詐騙 const confidenceScoreSpan = document.getElementById('confidence_score'); // 顯示模型預測可信度 const suspiciousPhrasesDiv = document.getElementById('suspicious_phrases'); // 顯示可疑詞句列表 /* 後端 FastAPI API 的 URL 在開發階段,通常是 http://127.0.0.1:8000 或 http://localhost:8000 請根據你實際運行 FastAPI 的位址和 Port 進行設定 */ const API_URL = "http://127.0.0.1:8000/predict"; const API_IMAGE_URL = "http://127.0.0.1:8000/predict-image" // --- 檢測按鈕點擊事件監聽器 --- // 當檢測按鈕被點擊時,執行非同步函數 //addEventListener('click', async () => {...}) // click 是一種 DOM 事件,代表使用者點擊按鈕。 // async 是「非同步函數」的關鍵字,允許你在函數中使用 await。它讓你可以像同步一樣撰寫非同步程式(例如網路請求)。 inputButton.addEventListener('click', async () => { const message = inputTextArea.value.trim(); //.value取得