From 7021c0b1eb96c9d07915fda02ea678080d19320d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Am=C4=93lija=20I?= Date: Fri, 24 Jan 2025 19:44:07 +0200 Subject: [PATCH] =?UTF-8?q?=D0=A1hanging=20server=20url=20and=20creating?= =?UTF-8?q?=20a=20user=20ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 13 +++++++++- options.html | 30 ++++++++++++++++++++++ options.js | 25 ++++++++++++++++++ src/borderify.js | 67 ++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 options.html create mode 100644 options.js diff --git a/manifest.json b/manifest.json index 220f2f7..8e2ac6c 100644 --- a/manifest.json +++ b/manifest.json @@ -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" + } + } } \ No newline at end of file diff --git a/options.html b/options.html new file mode 100644 index 0000000..5fcf40e --- /dev/null +++ b/options.html @@ -0,0 +1,30 @@ + + + + + Настройки расширения + + + + +

Настройки

+
+ + +
+

+ + + \ No newline at end of file diff --git a/options.js b/options.js new file mode 100644 index 0000000..f8b1599 --- /dev/null +++ b/options.js @@ -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); \ No newline at end of file diff --git a/src/borderify.js b/src/borderify.js index 39de314..080848f 100644 --- a/src/borderify.js +++ b/src/borderify.js @@ -1,32 +1,73 @@ let serverUrl = "http://localhost:5000/submit"; -fetch(browser.runtime.getURL("config.json")) - .then(response => { - if (!response.ok) { - throw new Error(`Ошибка загрузки config.json: ${response.statusText}`); +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}`); + } + return response.json(); + }) + .then(config => { + serverUrl = config.FLASK_SERVER_URL; + console.log("URL сервера успешно загружен:", serverUrl); + }); } - return response.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) { const timeStamp = new Date().toISOString(); return { "@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',