From fa6aad4f028a6f7dffe9a1d9913c47cd9ad815a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Am=C4=93lija=20I?= Date: Fri, 14 Nov 2025 08:04:29 +0200 Subject: [PATCH] Changing ID via the address bar --- background.js | 27 +++++++++++++++++++++++++ manifest.json | 14 +++++++++++++ src/borderify.js | 8 ++++---- update_id.html | 47 ++++++++++++++++++++++++++++++++++++++++++++ update_id.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 background.js create mode 100644 update_id.html create mode 100644 update_id.js diff --git a/background.js b/background.js new file mode 100644 index 0000000..a02309d --- /dev/null +++ b/background.js @@ -0,0 +1,27 @@ +const browserAPI = globalThis.browser ?? globalThis.chrome; +let clientId = null; + +async function ensureClientId() { + const data = await browserAPI.storage.local.get("clientId"); + + if (!data.clientId) { + const newId = `client_${Date.now().toString(36)}`; + await browserAPI.storage.local.set({ clientId: newId }); + clientId = newId; + console.log("[BG] Создан новый clientId:", newId); + } else { + clientId = data.clientId; + console.log("[BG] Загружен clientId:", clientId); + } +} + +browserAPI.storage.onChanged.addListener((changes, area) => { + if (area === "local" && changes.clientId) { + clientId = changes.clientId.newValue; + console.log("[BG] clientId обновлён:", clientId); + } +}); + +browserAPI.runtime.onInstalled.addListener(ensureClientId); +browserAPI.runtime.onStartup.addListener(ensureClientId); +ensureClientId(); \ No newline at end of file diff --git a/manifest.json b/manifest.json index dd453a0..6ab8d53 100644 --- a/manifest.json +++ b/manifest.json @@ -10,6 +10,10 @@ "alarms" ], + "background": { + "service_worker": "background.js" + }, + "host_permissions": [ "http://localhost:5000/*", "https://zpdai.rkg.lv/*", @@ -20,6 +24,16 @@ "extension_pages": "script-src 'self'; object-src 'self'; connect-src 'self' http://localhost:5000" }, + "web_accessible_resources": [ + { + "resources": [ + "update_id.html", + "update_id.js" + ], + "matches": [""] + } + ], + "content_scripts": [ { "matches": ["*://*/*"], diff --git a/src/borderify.js b/src/borderify.js index 23dad17..338081d 100644 --- a/src/borderify.js +++ b/src/borderify.js @@ -46,7 +46,7 @@ function processSettings(result) { function handleClientId() { if (typeof browser !== 'undefined') { - browserAPI.storage.sync.get("clientId") + browserAPI.storage.local.get("clientId") .then((result) => { if (result.clientId) { clientId = result.clientId; @@ -57,7 +57,7 @@ function handleClientId() { }) .catch(console.error); } else { - browserAPI.storage.sync.get("clientId", (result) => { + browserAPI.storage.local.get("clientId", (result) => { if (chrome.runtime.lastError) { console.error("Ошибка загрузки ID клиента:", chrome.runtime.lastError); } else if (result.clientId) { @@ -81,11 +81,11 @@ function generateUUID() { function generateAndSaveId() { clientId = generateUUID(); if (typeof browser !== 'undefined') { - browserAPI.storage.sync.set({ clientId }) + browserAPI.storage.local.set({ clientId }) .then(() => console.log("ID клиента создан и сохранен:", clientId)) .catch(console.error); } else { - browserAPI.storage.sync.set({ clientId }, () => { + browserAPI.storage.local.set({ clientId }, () => { if (chrome.runtime.lastError) { console.error("Ошибка сохранения ID клиента:", chrome.runtime.lastError); } else { diff --git a/update_id.html b/update_id.html new file mode 100644 index 0000000..d593815 --- /dev/null +++ b/update_id.html @@ -0,0 +1,47 @@ + + + + + + Установка ID + + + +
+

Установка идентификатора клиента

+
Обработка...
+
+ + + + \ No newline at end of file diff --git a/update_id.js b/update_id.js new file mode 100644 index 0000000..ba1b3c4 --- /dev/null +++ b/update_id.js @@ -0,0 +1,51 @@ +const browserAPI = globalThis.browser ?? globalThis.chrome; +const statusEl = document.getElementById("status"); + +function getQueryParams() { + const params = new URLSearchParams(window.location.search); + const entries = {}; + for (const [key, value] of params.entries()) { + entries[key] = value; + } + return entries; +} + +async function updateClientId(newId) { + if (!newId || !newId.trim()) { + throw new Error("Пустой ID"); + } + + await browserAPI.storage.local.set({ clientId: newId }); + console.log("[UI] clientId установлен:", newId); + return newId; +} + +async function processUrlParams() { + try { + const params = getQueryParams(); + + if (!params.clientId) { + statusEl.innerHTML = `
Ошибка: отсутствует параметр clientId
+

Используйте: ?clientId=your ID

`; + return; + } + + console.log("[UI] Получен clientId из URL:", params.clientId); + + const savedId = await updateClientId(params.clientId); + + statusEl.innerHTML = `
ID успешно установлен!
+

Новый идентификатор: ${savedId}

+

Страница закроется через 5 секунд...

`; + + setTimeout(() => { + window.close(); + }, 5000); + + } catch (error) { + console.error("[UI] Ошибка:", error); + statusEl.innerHTML = `
Ошибка: ${error.message}
`; + } +} + +document.addEventListener("DOMContentLoaded", processUrlParams); \ No newline at end of file