Files
CustosPublic/extension/popup.js

51 lines
2.1 KiB
JavaScript

// The toolbar popup: the logins Custos has for the active tab's site; a click fills the page's login form
// (the top frame's content script asks for the secrets itself, so they go only where they belong).
const SENTENCES = {
not_connected: 'Connect this browser in Custos first: Settings → Browser.',
not_running: 'Custos is not running. Start it, then try again.',
not_custos: 'Something other than Custos answered. Nothing was sent.',
locked: 'Custos is locked. Unlock it, then try again.',
denied: 'Custos did not allow this browser to use your logins.',
not_found: 'That login is no longer in Custos.',
no_match: 'Custos fills only web pages (https, or this computer).',
no_answer: 'Custos did not answer.',
};
const say = (code) => SENTENCES[code] ?? 'Custos could not do that.';
const note = document.getElementById('note');
const list = document.getElementById('logins');
async function show() {
const r = await chrome.runtime.sendMessage({ kind: 'match-tab' });
if (r?.origin) document.getElementById('site').textContent = new URL(r.origin).host;
if (!r?.ok) {
note.textContent = say(r?.error);
return;
}
if (!r.logins.length) {
note.textContent = 'Custos has no login for this site.';
return;
}
note.hidden = true;
for (const l of r.logins) {
const li = document.createElement('li');
const b = Object.assign(document.createElement('button'), { type: 'button' });
b.append(
Object.assign(document.createElement('span'), { className: 'title', textContent: l.title }),
Object.assign(document.createElement('span'), { className: 'user', textContent: l.username || '—' }),
);
b.addEventListener('click', async () => {
const done = await chrome.tabs.sendMessage(r.tab, { kind: 'fill-now', id: l.id }, { frameId: 0 }).catch(() => null);
if (done?.ok) window.close();
else {
note.hidden = false;
note.textContent = done?.error ?? 'This page has no login form Custos can fill.';
}
});
li.append(b);
list.append(li);
}
list.querySelector('button')?.focus();
}
void show();