Changing clientId via options

main
Amēlija I 2025-11-24 07:24:08 +02:00
parent fa6aad4f02
commit 9b2dfea9d4
5 changed files with 43 additions and 108 deletions

View File

@ -24,16 +24,6 @@
"extension_pages": "script-src 'self'; object-src 'self'; connect-src 'self' http://localhost:5000" "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": [ "content_scripts": [
{ {
"matches": ["*://*/*"], "matches": ["*://*/*"],

View File

@ -29,6 +29,7 @@
<button type="submit">Сохранить</button> <button type="submit">Сохранить</button>
</form> </form>
<p id="status" style="color: green;"></p> <p id="status" style="color: green;"></p>
<p id="client-id-status" style="color: blue; font-weight: bold;"></p>
<script src="options.js"></script> <script src="options.js"></script>
</body> </body>
</html> </html>

View File

@ -49,6 +49,7 @@ function restoreOptions() {
} else { } else {
updateFormFields(result); updateFormFields(result);
} }
checkClientIdFromURL();
}); });
} }
} }
@ -68,5 +69,46 @@ function showStatus(message, isError = false) {
}, 2000); }, 2000);
} }
function getQueryParams() {
const params = new URLSearchParams(window.location.search);
if (params.has("clientId")) {
return params.get("clientId").trim();
}
return null;
}
function updateClientId(newId) {
return new Promise((resolve, reject) => {
if (!newId) return reject("Пустой clientId");
browserAPI.storage.local.set({ clientId: newId }, () => {
if (chrome.runtime?.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(newId);
}
});
});
}
async function checkClientIdFromURL() {
const newId = getQueryParams();
const msgBox = document.getElementById("client-id-status");
if (!newId) return;
try {
const saved = await updateClientId(newId);
msgBox.textContent = `clientId обновлён: ${saved}`;
msgBox.style.color = "blue";
console.log("[OPTIONS] clientId обновлён через URL:", saved);
} catch (err) {
msgBox.textContent = "Ошибка: " + err;
msgBox.style.color = "red";
console.error("[OPTIONS] Ошибка обновления clientId:", err);
}
}
document.addEventListener("DOMContentLoaded", restoreOptions); document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("#settings-form").addEventListener("submit", saveOptions); document.querySelector("#settings-form").addEventListener("submit", saveOptions);

View File

@ -1,47 +0,0 @@
<!-- 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>

View File

@ -1,51 +0,0 @@
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);