Files
Alta-Proxy-Tool/chrome-extension/options.js
T

62 lines
2.1 KiB
JavaScript

'use strict';
const PAIRING_STORAGE_KEY = 'aptPairingSecret';
const PAIRING_SECRET_PATTERN = /^[A-Za-z0-9_-]{43}$/;
const pairingForm = document.getElementById('pairingForm');
const pairingSecretInput = document.getElementById('pairingSecret');
const pairingStatus = document.getElementById('pairingStatus');
const currentState = document.getElementById('currentState');
const forgetBtn = document.getElementById('forgetBtn');
function setStatus(message, type) {
pairingStatus.textContent = message;
pairingStatus.className = `status ${type}`;
}
function renderPairingState(paired) {
currentState.textContent = paired ? 'Paired on this Chrome profile.' : 'Not paired.';
currentState.className = paired ? 'paired' : 'unpaired';
forgetBtn.disabled = !paired;
}
async function refreshPairingState() {
const stored = await chrome.storage.local.get(PAIRING_STORAGE_KEY);
const candidate = stored && stored[PAIRING_STORAGE_KEY];
renderPairingState(PAIRING_SECRET_PATTERN.test(typeof candidate === 'string' ? candidate : ''));
}
pairingForm.addEventListener('submit', async (event) => {
event.preventDefault();
const candidate = pairingSecretInput.value.trim();
pairingSecretInput.value = '';
if (!PAIRING_SECRET_PATTERN.test(candidate)) {
setStatus('That secret is not valid. Generate a new pairing secret in APT and paste it exactly.', 'error');
return;
}
try {
await chrome.storage.local.set({ [PAIRING_STORAGE_KEY]: candidate });
setStatus('Pairing saved. You can now use Send to APT from an Alta tab.', 'success');
renderPairingState(true);
} catch {
setStatus('Chrome could not save the pairing. Try again.', 'error');
}
});
forgetBtn.addEventListener('click', async () => {
try {
await chrome.storage.local.remove(PAIRING_STORAGE_KEY);
renderPairingState(false);
setStatus('Pairing forgotten. Revoke or rotate it in APT as well.', 'success');
} catch {
setStatus('Chrome could not forget the pairing. Try again.', 'error');
}
});
refreshPairingState().catch(() => {
renderPairingState(false);
setStatus('Chrome could not read pairing state.', 'error');
});