-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (153 loc) · 6.45 KB
/
script.js
File metadata and controls
191 lines (153 loc) · 6.45 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
document.addEventListener("DOMContentLoaded", function () {
const getStartedBtn = document.getElementById("getStartedBtn");
if (getStartedBtn) {
getStartedBtn.addEventListener("click", function () {
window.location.href = "studashboard.html"; // Redirects to Student Dashboard
});
}
});
function showAlert(message) {
alert(message);
}
function redirectToSentiment() {
window.location.href = "sentiment.html"; // Redirect to Sentiment Analysis page
}
document.addEventListener("DOMContentLoaded", function () {
const analyzeButton = document.getElementById("analyzeBtn");
if (analyzeButton) {
analyzeButton.addEventListener("click", async function () {
const text = document.getElementById("userText").value;
if (!text.trim()) {
alert("Please enter some text to analyze.");
return;
}
try {
const response = await fetch("http://localhost:5000/analyze", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text }),
});
const result = await response.json();
const resultElement = document.getElementById("result");
if (result.error) {
resultElement.innerHTML = `<span style="color: red;">${result.error}</span>`;
} else {
resultElement.innerHTML =
`Sentiment: <strong>${result.label}</strong> <br> Score: <strong>${result.score.toFixed(3)}</strong>`;
}
} catch (error) {
console.error("Error:", error);
document.getElementById("result").innerHTML = `<span style="color: red;">Error analyzing sentiment.</span>`;
}
});
}
});
function redirectToBehavior() {
window.location.href = 'behavior.html';
}
// Replace with your Mixpanel project token and unique user identifier.
const MIXPANEL_TOKEN = "YOUR_MIXPANEL_TOKEN";
const DISTINCT_ID = "unique_user_id";
// Function to track an event via the Mixpanel API
function trackEvent(eventName, additionalProperties = {}) {
const eventData = {
event: eventName,
properties: {
token: MIXPANEL_TOKEN,
distinct_id: DISTINCT_ID,
time: Math.floor(Date.now() / 1000),
...additionalProperties
}
};
// Convert event data to JSON and then Base64 encode it
const jsonData = JSON.stringify(eventData);
const base64Data = btoa(jsonData);
const url = `https://api.mixpanel.com/track/?data=${encodeURIComponent(base64Data)}`;
fetch(url, { method: "GET" })
.then(response => response.text())
.then(result => console.log(`Mixpanel API response for "${eventName}":`, result))
.catch(error => console.error("Error calling Mixpanel API:", error));
}
// Track page view when the DOM is fully loaded
document.addEventListener("DOMContentLoaded", () => {
trackEvent("Page View");
});
// Track button click event
document.getElementById("actionBtn").addEventListener("click", () => {
trackEvent("Button Clicked", { button: "actionBtn" });
alert("Button clicked! Event tracked.");
});
// Track form submission event
document.getElementById("sampleForm").addEventListener("submit", (e) => {
e.preventDefault();
const nameValue = document.getElementById("nameInput").value;
trackEvent("Form Submitted", { name: nameValue });
alert("Form submitted! Event tracked.");
});
function startChat() {
// Redirect to ChatGPT messaging page
window.location.href = "https://chat.openai.com/";
}
let mediaRecorder;
let audioChunks = [];
// Check if MediaRecorder is supported
if (!navigator.mediaDevices || !window.MediaRecorder) {
document.getElementById('status').textContent = "MediaRecorder is not supported in this browser.";
}
document.getElementById('startBtn').addEventListener('click', async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = event => {
if (event.data.size > 0) {
audioChunks.push(event.data);
}
};
mediaRecorder.onstart = () => {
audioChunks = []; // Clear any previous recording
document.getElementById('status').textContent = "Recording...";
};
mediaRecorder.onstop = () => {
document.getElementById('status').textContent = "Recording stopped.";
// Enable the analyze button after recording
document.getElementById('analyzeBtn').disabled = false;
};
mediaRecorder.start();
document.getElementById('startBtn').disabled = true;
document.getElementById('stopBtn').disabled = false;
} catch (error) {
console.error("Error accessing microphone:", error);
document.getElementById('status').textContent = "Error accessing microphone.";
}
});
document.getElementById('stopBtn').addEventListener('click', () => {
if (mediaRecorder && mediaRecorder.state !== "inactive") {
mediaRecorder.stop();
document.getElementById('startBtn').disabled = false;
document.getElementById('stopBtn').disabled = true;
}
});
document.getElementById('analyzeBtn').addEventListener('click', () => {
// Create a Blob from the recorded audio chunks
const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
// Prepare form data to send the audio file
const formData = new FormData();
formData.append('file', audioBlob, 'recording.webm');
// Append any additional required parameters for Beyond Verbal API here
document.getElementById('status').textContent = "Analyzing voice...";
// Make the API call to Beyond Verbal (replace URL and add any necessary headers/authentication)
fetch('YOUR_BV_API_ENDPOINT', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
// Display the API response in the result div
document.getElementById('result').textContent = JSON.stringify(data, null, 2);
document.getElementById('status').textContent = "Analysis complete.";
})
.catch(error => {
console.error("Error during analysis:", error);
document.getElementById('status').textContent = "Error during analysis.";
});
});