Сhanging server url and creating a user ID

main
Amēlija I 2025-01-24 19:44:07 +02:00
parent 8d878a1592
commit 7021c0b1eb
4 changed files with 121 additions and 14 deletions

View File

@ -22,5 +22,16 @@
"matches": ["*://*/*"],
"js": ["src/borderify.js"]
}
]
],
"options_ui": {
"page": "options.html",
"open_in_tab": true
},
"browser_specific_settings": {
"gecko": {
"id": "addon@example.com"
}
}
}

30
options.html 100644
View File

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Настройки расширения</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
label {
display: block;
margin-bottom: 10px;
}
</style>
</head>
<body>
<h1>Настройки</h1>
<form id="settings-form">
<label>
URL сервера:
<input type="text" id="server-url" placeholder="Введите URL сервера" />
</label>
<button type="submit">Сохранить</button>
</form>
<p id="status" style="color: green;"></p>
<script src="options.js"></script>
</body>
</html>

25
options.js 100644
View File

@ -0,0 +1,25 @@
function saveOptions(event) {
event.preventDefault();
const serverUrl = document.querySelector("#server-url").value;
if (serverUrl.trim() === "") {
alert("URL сервера не может быть пустым!");
return;
}
browser.storage.sync.set({ serverUrl }).then(() => {
const status = document.querySelector("#status");
status.textContent = "Настройки сохранены!";
setTimeout(() => (status.textContent = ""), 2000);
});
}
function restoreOptions() {
browser.storage.sync.get("serverUrl").then((result) => {
const serverUrl = result.serverUrl || "http://localhost:5000/submit";
document.querySelector("#server-url").value = serverUrl;
});
}
document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("#settings-form").addEventListener("submit", saveOptions);

View File

@ -1,6 +1,12 @@
let serverUrl = "http://localhost:5000/submit";
fetch(browser.runtime.getURL("config.json"))
function loadServerUrl() {
return browser.storage.sync.get("serverUrl").then((result) => {
if (result.serverUrl) {
serverUrl = result.serverUrl;
console.log("URL сервера загружен из настроек:", serverUrl);
} else {
return fetch(browser.runtime.getURL("config.json"))
.then(response => {
if (!response.ok) {
throw new Error(`Ошибка загрузки config.json: ${response.statusText}`);
@ -10,8 +16,39 @@ fetch(browser.runtime.getURL("config.json"))
.then(config => {
serverUrl = config.FLASK_SERVER_URL;
console.log("URL сервера успешно загружен:", serverUrl);
})
.catch(error => console.error("Ошибка загрузки конфигурации:", error));
});
}
}).catch((error) => {
console.error("Ошибка загрузки URL сервера:", error);
});
}
loadServerUrl();
let clientId = null;
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
browser.storage.sync.get("clientId").then((result) => {
if (result.clientId) {
clientId = result.clientId;
console.log("ID клиента загружен:", clientId);
} else {
clientId = generateUUID();
browser.storage.sync.set({ clientId }).then(() => {
console.log("ID клиента создан и сохранен:", clientId);
});
}
}).catch((error) => {
console.error("Ошибка загрузки ID клиента:", error);
});
function createEventJSON(eventType, data) {
@ -20,13 +57,17 @@ function createEventJSON(eventType, data) {
"@version": 1,
"type": `browserext.${eventType}`,
"TimeStamp": timeStamp,
"data": data,
"data": {...data, clientId },
"@timestamp": timeStamp
};
}
async function sendDataToServer(eventData) {
if (!serverUrl) {
console.error("URL сервера не задан. Проверьте настройки.");
return;
}
try {
const response = await fetch(serverUrl, {
method: 'POST',