-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
137 lines (122 loc) · 5.09 KB
/
script.js
File metadata and controls
137 lines (122 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* script.js */
document.addEventListener('DOMContentLoaded', function() {
const analyzeButton = document.getElementById('analyzeButton');
const clearButton = document.getElementById('clearButton');
const inputText = document.getElementById('inputText');
const resultDiv = document.getElementById('result');
const historyList = document.getElementById('historyList');
const probabilityDiv = document.getElementById('probability');
let queries = [];
let probabilityChart;
const loadingIndicator = document.createElement('div');
loadingIndicator.textContent = 'Analyzing...';
loadingIndicator.style.display = 'none';
document.querySelector('.container').appendChild(loadingIndicator);
analyzeButton.addEventListener('click', async function() {
const text = inputText.value;
if (!text.trim()) {
alert('Please enter text to analyze.');
return;
}
loadingIndicator.style.display = 'block';
resultDiv.innerHTML = '';
probabilityDiv.style.display = 'none';
try {
const response = await fetch('http://localhost:5001/api/setUserQuery', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: text }),
});
if (!response.ok) {
throw new Error('Network response was not ok.');
}
const data = await response.json();
loadingIndicator.style.display = 'none';
resultDiv.innerHTML = formatText(data.message);
resultDiv.className = '';
resultDiv.classList.add('result-' + data.status);
queries.push({ query: text, result: data.message, status: data.status, probability: data.probability });
updateHistory();
updateProbability(data.probability);
document.getElementById('copyButton').style.display = 'block';
} catch (error) {
loadingIndicator.style.display = 'none';
console.error('Error:', error);
resultDiv.innerHTML = 'An error occurred during analysis.';
resultDiv.className = 'result-danger';
probabilityDiv.style.display = 'none';
}
});
clearButton.addEventListener('click', function() {
inputText.value = '';
resultDiv.innerHTML = '';
resultDiv.className = '';
probabilityDiv.style.display = 'none';
document.getElementById('copyButton').style.display = 'none';
});
function updateHistory() {
historyList.innerHTML = '';
queries.forEach(item => {
const listItem = document.createElement('li');
listItem.textContent = `Query: ${item.query} - Result: ${item.result} - Probability: ${item.probability}%`;
listItem.classList.add('result-' + item.status);
historyList.appendChild(listItem);
});
}
function formatText(text) {
// Replace **text** with <strong>text</strong> for bold
text = text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
return text;
}
function updateProbability(probability) {
probabilityDiv.style.display = 'block';
if (probabilityChart) {
probabilityChart.destroy();
}
const ctx = document.getElementById('probabilityChart').getContext('2d');
probabilityChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Scam Probability'],
datasets: [{
label: 'Percentage',
data: [probability],
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1,
}],
},
options: {
scales: {
y: {
beginAtZero: true,
max: 100,
},
},
},
});
}
// Modal functionality
const helpModal = document.getElementById('helpModal');
const helpButton = document.getElementById('helpButton');
const closeModal = document.querySelector('.close');
document.addEventListener('click', function(event) {
if (event.target.id === 'helpButton') {
helpModal.style.display = 'block';
} else if (event.target === closeModal || event.target === helpModal) {
helpModal.style.display = 'none';
}
});
// Copy to clipboard functionality
const copyButton = document.createElement('button');
copyButton.id = 'copyButton';
copyButton.textContent = 'Copy';
copyButton.style.display = 'none';
document.querySelector('.input-group').appendChild(copyButton);
copyButton.addEventListener('click', function() {
const resultText = document.getElementById('result').textContent;
navigator.clipboard.writeText(resultText)
.then(() => alert('Result copied to clipboard!'))
.catch(err => console.error('Could not copy text: ', err));
});
});