Сhanging server url and creating a user ID
parent
8d878a1592
commit
7021c0b1eb
|
@ -22,5 +22,16 @@
|
||||||
"matches": ["*://*/*"],
|
"matches": ["*://*/*"],
|
||||||
"js": ["src/borderify.js"]
|
"js": ["src/borderify.js"]
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
|
||||||
|
"options_ui": {
|
||||||
|
"page": "options.html",
|
||||||
|
"open_in_tab": true
|
||||||
|
},
|
||||||
|
|
||||||
|
"browser_specific_settings": {
|
||||||
|
"gecko": {
|
||||||
|
"id": "addon@example.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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>
|
|
@ -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);
|
|
@ -1,17 +1,54 @@
|
||||||
let serverUrl = "http://localhost:5000/submit";
|
let serverUrl = "http://localhost:5000/submit";
|
||||||
|
|
||||||
fetch(browser.runtime.getURL("config.json"))
|
function loadServerUrl() {
|
||||||
.then(response => {
|
return browser.storage.sync.get("serverUrl").then((result) => {
|
||||||
if (!response.ok) {
|
if (result.serverUrl) {
|
||||||
throw new Error(`Ошибка загрузки config.json: ${response.statusText}`);
|
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();
|
}).catch((error) => {
|
||||||
})
|
console.error("Ошибка загрузки URL сервера:", error);
|
||||||
.then(config => {
|
});
|
||||||
serverUrl = config.FLASK_SERVER_URL;
|
}
|
||||||
console.log("URL сервера успешно загружен:", serverUrl);
|
|
||||||
})
|
loadServerUrl();
|
||||||
.catch(error => console.error("Ошибка загрузки конфигурации:", error));
|
|
||||||
|
|
||||||
|
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) {
|
function createEventJSON(eventType, data) {
|
||||||
|
@ -20,13 +57,17 @@ function createEventJSON(eventType, data) {
|
||||||
"@version": 1,
|
"@version": 1,
|
||||||
"type": `browserext.${eventType}`,
|
"type": `browserext.${eventType}`,
|
||||||
"TimeStamp": timeStamp,
|
"TimeStamp": timeStamp,
|
||||||
"data": data,
|
"data": {...data, clientId },
|
||||||
"@timestamp": timeStamp
|
"@timestamp": timeStamp
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function sendDataToServer(eventData) {
|
async function sendDataToServer(eventData) {
|
||||||
|
if (!serverUrl) {
|
||||||
|
console.error("URL сервера не задан. Проверьте настройки.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const response = await fetch(serverUrl, {
|
const response = await fetch(serverUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
Loading…
Reference in New Issue