adding password

main
Amēlija I 2025-02-14 20:01:02 +02:00
parent 7021c0b1eb
commit e9a5d6bbf9
3 changed files with 26 additions and 10 deletions

View File

@ -22,6 +22,10 @@
URL сервера: URL сервера:
<input type="text" id="server-url" placeholder="Введите URL сервера" /> <input type="text" id="server-url" placeholder="Введите URL сервера" />
</label> </label>
<label>
Пароль:
<input type="text" id="password" placeholder="Введите пароль" />
</label>
<button type="submit">Сохранить</button> <button type="submit">Сохранить</button>
</form> </form>
<p id="status" style="color: green;"></p> <p id="status" style="color: green;"></p>

View File

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

View File

@ -1,10 +1,15 @@
let serverUrl = "http://localhost:5000/submit"; let serverUrl = "http://localhost:5000/submit";
let password = "";
function loadServerUrl() { function loadSettings() {
return browser.storage.sync.get("serverUrl").then((result) => { return browser.storage.sync.get(["serverUrl", "password"]).then((result) => {
if (result.serverUrl) { if (result.serverUrl) {
serverUrl = result.serverUrl; serverUrl = result.serverUrl;
console.log("URL сервера загружен из настроек:", serverUrl); console.log("URL сервера загружен из настроек:", serverUrl);
}
if (result.password) {
password = result.password;
console.log("Пароль загружен из настроек.");
} else { } else {
return fetch(browser.runtime.getURL("config.json")) return fetch(browser.runtime.getURL("config.json"))
.then(response => { .then(response => {
@ -15,6 +20,7 @@ function loadServerUrl() {
}) })
.then(config => { .then(config => {
serverUrl = config.FLASK_SERVER_URL; serverUrl = config.FLASK_SERVER_URL;
password = config.PASSWORD || password;
console.log("URL сервера успешно загружен:", serverUrl); console.log("URL сервера успешно загружен:", serverUrl);
}); });
} }
@ -23,7 +29,7 @@ function loadServerUrl() {
}); });
} }
loadServerUrl(); loadSettings();
let clientId = null; let clientId = null;
@ -64,14 +70,17 @@ function createEventJSON(eventType, data) {
async function sendDataToServer(eventData) { async function sendDataToServer(eventData) {
if (!serverUrl) { if (!serverUrl || !password) {
console.error("URL сервера не задан. Проверьте настройки."); console.error("URL сервера или пароль не заданы. Проверьте настройки.");
return; return;
} }
try { try {
const response = await fetch(serverUrl, { const response = await fetch(serverUrl, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: {
'Content-Type': 'application/json',
'X-Password': password,
},
body: JSON.stringify(eventData), body: JSON.stringify(eventData),
}); });