Changing ID via the address bar

main
Amēlija I 2025-11-14 08:04:29 +02:00
parent 5ba118cfa2
commit fa6aad4f02
5 changed files with 143 additions and 4 deletions

27
background.js 100644
View File

@ -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();

View File

@ -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": ["<all_urls>"]
}
],
"content_scripts": [
{
"matches": ["*://*/*"],

View File

@ -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 {

47
update_id.html 100644
View File

@ -0,0 +1,47 @@
<!-- chrome-extension://cimkehmfiogjfgmglapeabbnhjkdhnmj/update_id.html moz-...UUID edge- -->
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<title>Установка ID</title>
<style>
body {
font-family: system-ui, sans-serif;
background-color: #f7f9fc;
color: #222;
margin: 40px;
text-align: center;
}
.card {
background: white;
border-radius: 12px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
padding: 30px;
max-width: 500px;
margin: auto;
}
.success {
color: green;
font-weight: bold;
}
.error {
color: red;
font-weight: bold;
}
code {
background: #eef3fa;
padding: 4px 8px;
border-radius: 4px;
font-size: 14px;
}
</style>
</head>
<body>
<div class="card">
<h2>Установка идентификатора клиента</h2>
<div id="status">Обработка...</div>
</div>
<script src="update_id.js"></script>
</body>
</html>

51
update_id.js 100644
View File

@ -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 = `<div class="error">Ошибка: отсутствует параметр clientId</div>
<p>Используйте: <code>?clientId=your ID</code></p>`;
return;
}
console.log("[UI] Получен clientId из URL:", params.clientId);
const savedId = await updateClientId(params.clientId);
statusEl.innerHTML = `<div class="success"> ID успешно установлен!</div>
<p>Новый идентификатор: <code>${savedId}</code></p>
<p>Страница закроется через 5 секунд...</p>`;
setTimeout(() => {
window.close();
}, 5000);
} catch (error) {
console.error("[UI] Ошибка:", error);
statusEl.innerHTML = `<div class="error">Ошибка: ${error.message}</div>`;
}
}
document.addEventListener("DOMContentLoaded", processUrlParams);