2024-09-23 11:11:45 +02:00
|
|
|
import axios from "axios";
|
|
|
|
|
2024-10-11 10:18:33 +02:00
|
|
|
let windownameGlobal = ""; // Global variable to hold the window name
|
|
|
|
let accesstoken = ""; // Variable to store the access token
|
2024-10-03 14:10:21 +02:00
|
|
|
|
2024-09-23 11:11:45 +02:00
|
|
|
onmessage = (event) => {
|
2024-10-11 10:18:33 +02:00
|
|
|
const { action, access_token, windowname } = event.data;
|
|
|
|
accesstoken = access_token;
|
|
|
|
windownameGlobal = windowname;
|
2024-10-03 14:10:21 +02:00
|
|
|
|
2024-09-23 14:54:13 +02:00
|
|
|
if (action === "start") {
|
2024-10-11 10:18:33 +02:00
|
|
|
fetchData(); // Start fetching data on 'start' action
|
2024-09-23 11:11:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 16:34:55 +02:00
|
|
|
const fetchData = () => {
|
2024-10-11 10:18:33 +02:00
|
|
|
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_get?access_token=" + accesstoken);
|
|
|
|
apiURL.hostname = windownameGlobal; // Set the hostname
|
2024-09-24 16:42:36 +02:00
|
|
|
|
2024-10-11 10:18:33 +02:00
|
|
|
console.log(apiURL.href); // Log the constructed URL
|
2024-09-24 16:42:36 +02:00
|
|
|
|
2024-10-03 14:10:21 +02:00
|
|
|
axios.get(apiURL.href)
|
2024-09-23 11:11:45 +02:00
|
|
|
.then(response => {
|
2024-10-11 10:18:33 +02:00
|
|
|
postMessage(response.data); // Send data back on success
|
|
|
|
setTimeout(fetchData, 100); // Schedule next fetch
|
2024-09-23 11:11:45 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log('Error fetching data:', error);
|
2024-10-11 10:18:33 +02:00
|
|
|
postMessage({ error: "failed fetching data" }); // Send error message
|
|
|
|
setTimeout(() => fetchData(), 1000); // Retry after 1 second
|
|
|
|
});
|
|
|
|
}
|