jerrynnms commited on
Commit
6ce4b4c
·
verified ·
1 Parent(s): 5fac806

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +140 -107
script.js CHANGED
@@ -1,107 +1,140 @@
1
- /// script.js
2
- document.addEventListener('DOMContentLoaded', () => {
3
- const inputTextArea = document.getElementById('predict_info');
4
- const inputButton = document.getElementById('detect_button');
5
- const clearButton = document.getElementById('clear_button');
6
- const normalOrScam = document.getElementById('is_scam');
7
- const confidenceScoreSpan = document.getElementById('confidence_score');
8
- const suspiciousPhrasesDiv = document.getElementById('suspicious_phrases');
9
- const feedbackArea = document.getElementById('feedback_area');
10
- const feedbackCorrectBtn = document.getElementById('feedback_correct');
11
- const feedbackWrongBtn = document.getElementById('feedback_wrong');
12
- const feedbackStatus = document.getElementById('feedback_status');
13
-
14
- let lastPrediction = null;
15
-
16
- // 使用相對路徑,Vercel 會自動解析
17
- const API_URL = '/predict';
18
- const FEEDBACK_API = '/feedback';
19
-
20
- inputButton.addEventListener('click', async () => {
21
- const message = inputTextArea.value.trim();
22
- if (!message) {
23
- alert('請輸入您想檢測的訊息內容。');
24
- return;
25
- }
26
-
27
- normalOrScam.textContent = '檢測中...';
28
- normalOrScam.style.color = 'gray';
29
- confidenceScoreSpan.textContent = '計算中...';
30
- suspiciousPhrasesDiv.innerHTML = '<p>正在分析訊息,請稍候...</p>';
31
- feedbackArea.style.display = 'none';
32
- feedbackStatus.textContent = '';
33
- feedbackCorrectBtn.style.display = 'inline-block';
34
- feedbackWrongBtn.style.display = 'inline-block';
35
-
36
- try {
37
- const response = await fetch(API_URL, {
38
- method: 'POST',
39
- headers: { 'Content-Type': 'application/json' },
40
- body: JSON.stringify({ text: message }),
41
- });
42
-
43
- if (!response.ok) throw new Error(`伺服器錯誤: ${response.status} ${response.statusText}`);
44
- const data = await response.json();
45
-
46
- updateResults(data.status, data.confidence, data.suspicious_keywords);
47
- feedbackArea.style.display = 'block';
48
- lastPrediction = { text: message, model_status: data.status };
49
- } catch (error) {
50
- console.error('訊息檢測失敗:', error);
51
- alert(`訊息檢測失敗,請檢查後端服務。\n錯誤詳情: ${error.message}`);
52
- resetResults();
53
- }
54
- });
55
-
56
- clearButton.addEventListener('click', () => {
57
- inputTextArea.value = '';
58
- resetResults();
59
- feedbackArea.style.display = 'none';
60
- feedbackStatus.textContent = '';
61
- });
62
-
63
- feedbackCorrectBtn.addEventListener('click', () => submitFeedback('正確'));
64
- feedbackWrongBtn.addEventListener('click', () => submitFeedback('錯誤'));
65
-
66
- async function submitFeedback(user_feedback) {
67
- if (!lastPrediction) return;
68
- const payload = { ...lastPrediction, user_feedback };
69
- try {
70
- const res = await fetch(FEEDBACK_API, {
71
- method: 'POST',
72
- headers: { 'Content-Type': 'application/json' },
73
- body: JSON.stringify(payload),
74
- });
75
- const msg = await res.json();
76
- feedbackStatus.textContent = '✅ 感謝你的回饋!';
77
- feedbackCorrectBtn.style.display = 'none';
78
- feedbackWrongBtn.style.display = 'none';
79
- } catch (e) {
80
- feedbackStatus.textContent = '❌ 回饋提交失敗';
81
- }
82
- }
83
-
84
- function updateResults(isScam, confidence, suspiciousParts) {
85
- normalOrScam.textContent = isScam;
86
- confidenceScoreSpan.textContent = confidence;
87
- suspiciousPhrasesDiv.innerHTML = '';
88
- if (suspiciousParts && suspiciousParts.length > 0) {
89
- const ul = document.createElement('ul');
90
- suspiciousParts.forEach(phrase => {
91
- const li = document.createElement('li');
92
- li.textContent = phrase;
93
- ul.appendChild(li);
94
- });
95
- suspiciousPhrasesDiv.appendChild(ul);
96
- } else {
97
- suspiciousPhrasesDiv.innerHTML = '<p>沒有偵測到特別可疑的詞句。</p>';
98
- }
99
- }
100
-
101
- function resetResults() {
102
- normalOrScam.textContent = '待檢測';
103
- normalOrScam.style.color = 'inherit';
104
- confidenceScoreSpan.textContent = '待檢測';
105
- suspiciousPhrasesDiv.innerHTML = '<p>請輸入訊息並點擊「檢測!」按鈕。</p>';
106
- }
107
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener('DOMContentLoaded', () => {
2
+ const inputTextArea = document.getElementById('predict_info');
3
+ const inputButton = document.getElementById('detect_button');
4
+ const clearButton = document.getElementById('clear_button');
5
+ const normalOrScam = document.getElementById('is_scam');
6
+ const confidenceScoreSpan = document.getElementById('confidence_score');
7
+ const suspiciousPhrasesDiv = document.getElementById('suspicious_phrases');
8
+ const feedbackArea = document.getElementById('feedback_area');
9
+ const feedbackCorrectBtn = document.getElementById('feedback_correct');
10
+ const feedbackWrongBtn = document.getElementById('feedback_wrong');
11
+ const feedbackStatus = document.getElementById('feedback_status');
12
+ const imageUpload = document.getElementById('image_upload');
13
+ const ocrResult = document.getElementById('ocr_result');
14
+
15
+ let lastPrediction = null;
16
+
17
+ const API_URL = '/predict';
18
+ const FEEDBACK_API = '/feedback';
19
+ const IMAGE_API = '/analyze-image';
20
+
21
+ // 文字檢測
22
+ inputButton.addEventListener('click', async () => {
23
+ const message = inputTextArea.value.trim();
24
+ if (!message) {
25
+ alert('請輸入您想檢測的訊息內容。');
26
+ return;
27
+ }
28
+
29
+ normalOrScam.textContent = '檢測中...';
30
+ normalOrScam.style.color = 'gray';
31
+ confidenceScoreSpan.textContent = '計算中...';
32
+ suspiciousPhrasesDiv.innerHTML = '<p>正在分析訊息,請稍候...</p>';
33
+ feedbackArea.style.display = 'none';
34
+ feedbackStatus.textContent = '';
35
+ feedbackCorrectBtn.style.display = 'inline-block';
36
+ feedbackWrongBtn.style.display = 'inline-block';
37
+
38
+ try {
39
+ const response = await fetch(API_URL, {
40
+ method: 'POST',
41
+ headers: { 'Content-Type': 'application/json' },
42
+ body: JSON.stringify({ text: message }),
43
+ });
44
+
45
+ if (!response.ok) throw new Error(`伺服器錯誤: ${response.status} ${response.statusText}`);
46
+ const data = await response.json();
47
+
48
+ updateResults(data.status, data.confidence, data.suspicious_keywords);
49
+ feedbackArea.style.display = 'block';
50
+ lastPrediction = { text: message, model_status: data.status };
51
+ } catch (error) {
52
+ console.error('訊息檢測失敗:', error);
53
+ alert(`訊息檢測失敗,請檢查後端服務。\n錯誤詳情: ${error.message}`);
54
+ resetResults();
55
+ }
56
+ });
57
+
58
+ clearButton.addEventListener('click', () => {
59
+ inputTextArea.value = '';
60
+ resetResults();
61
+ feedbackArea.style.display = 'none';
62
+ feedbackStatus.textContent = '';
63
+ });
64
+
65
+ feedbackCorrectBtn.addEventListener('click', () => submitFeedback('正確'));
66
+ feedbackWrongBtn.addEventListener('click', () => submitFeedback('錯誤'));
67
+
68
+ async function submitFeedback(user_feedback) {
69
+ if (!lastPrediction) return;
70
+ const payload = { ...lastPrediction, user_feedback };
71
+ try {
72
+ const res = await fetch(FEEDBACK_API, {
73
+ method: 'POST',
74
+ headers: { 'Content-Type': 'application/json' },
75
+ body: JSON.stringify(payload),
76
+ });
77
+ const msg = await res.json();
78
+ feedbackStatus.textContent = '✅ 感謝你的回饋!';
79
+ feedbackCorrectBtn.style.display = 'none';
80
+ feedbackWrongBtn.style.display = 'none';
81
+ } catch (e) {
82
+ feedbackStatus.textContent = '❌ 回饋提交失敗';
83
+ }
84
+ }
85
+
86
+ function updateResults(isScam, confidence, suspiciousParts) {
87
+ normalOrScam.textContent = isScam;
88
+ confidenceScoreSpan.textContent = confidence;
89
+ suspiciousPhrasesDiv.innerHTML = '';
90
+ if (suspiciousParts && suspiciousParts.length > 0) {
91
+ const ul = document.createElement('ul');
92
+ suspiciousParts.forEach(phrase => {
93
+ const li = document.createElement('li');
94
+ li.textContent = phrase;
95
+ ul.appendChild(li);
96
+ });
97
+ suspiciousPhrasesDiv.appendChild(ul);
98
+ } else {
99
+ suspiciousPhrasesDiv.innerHTML = '<p>沒有偵測到特別可疑的詞句。</p>';
100
+ }
101
+ }
102
+
103
+ function resetResults() {
104
+ normalOrScam.textContent = '待檢測';
105
+ normalOrScam.style.color = 'inherit';
106
+ confidenceScoreSpan.textContent = '待檢測';
107
+ suspiciousPhrasesDiv.innerHTML = '<p>請輸入訊息並點擊「檢測!」按鈕。</p>';
108
+ }
109
+
110
+ // ✅ 圖片上傳 + OCR 分析
111
+ window.analyzeImage = async function () {
112
+ if (!imageUpload.files.length) {
113
+ alert('請選擇圖片');
114
+ return;
115
+ }
116
+
117
+ const formData = new FormData();
118
+ formData.append('file', imageUpload.files[0]);
119
+
120
+ ocrResult.innerHTML = '<p>⏳ 處理中...</p>';
121
+
122
+ try {
123
+ const response = await fetch(IMAGE_API, {
124
+ method: 'POST',
125
+ body: formData
126
+ });
127
+
128
+ const data = await response.json();
129
+
130
+ ocrResult.innerHTML = `
131
+ <p><strong>📄 擷取文字:</strong> ${data.extracted_text}</p>
132
+ <p><strong>是否為詐騙訊息:</strong> ${data.analysis_result.status}</p>
133
+ <p><strong>模型預測可疑度:</strong> ${data.analysis_result.confidence}%</p>
134
+ <p><strong>可疑詞句分析:</strong> ${data.analysis_result.suspicious_keywords.join("、")}</p>
135
+ `;
136
+ } catch (error) {
137
+ ocrResult.innerHTML = `<p style="color:red;">❌ 分析失敗:${error.message}</p>`;
138
+ }
139
+ };
140
+ });