Why a Synthetic Click Is Not Enough
LoginDock is a bookmark manager for sign-in pages. Opening a saved entry focuses the username field so the browser's own password manager offers its dropdown. The Pro feature automates the next step: when a site remembers several accounts, pick the one at the position you configured.
The naive implementation — element.click() from a content script, then dispatchEvent a few keydowns — fails immediately. The browser's autofill dropdown is a native UI surface, and it only opens for trusted events. Synthetic DOM events have isTrusted: false; the password manager ignores them. The only extension API that produces trusted input is chrome.debugger, via CDP's Input.dispatchMouseEvent and Input.dispatchKeyEvent.
That API comes with a visible cost: Chrome shows a "LoginDock started debugging this browser" banner while it is attached. We originally planned to keep debugger in optional_permissions and request it inside the popup click, but Chrome would not grant it at runtime in this flow, so the permission is declared at install and the banner is documented instead of hidden. The session detaches the moment the pick is done.
Trusted Input as a Session, Not a Fire-and-Forget
The first design sent one message — "pick account N" — and detached. It broke in two ways: a retry after a drifted dropdown re-attached the debugger (second banner, lost state), and a captcha click after detach had no trusted channel left. The fix is a session protocol with three messages:
ld-trusted-start— attach the debugger, pick the account, read the username back.ld-trusted-retry— clear the fields, pick the next position, read back again. The debugger stays attached.ld-trusted-end— optionally click the human-verification checkbox, then detach.
Sessions live in a Map keyed by tab id, and chrome.debugger.onDetach deletes the entry, so a user dismissing the banner can never leave a zombie session behind:
const trustedSessions = new Map(); // tabId -> { field }
if (chrome.debugger && chrome.debugger.onDetach) {
chrome.debugger.onDetach.addListener(source => {
if (source && source.tabId !== undefined) trustedSessions.delete(source.tabId);
});
}
The Pick Itself
With the debugger attached, the pick is deliberately dumb: click the username field at the coordinates the content script measured, wait for the native dropdown to render, press ArrowDown N times, press Enter. No DOM guessing about the dropdown — it is native UI and has no DOM.
async function selectAccount(tabId, field, accountIndex) {
await clickAt(tabId, field.x, field.y);
await sleep(500); // let the native dropdown render
const n = Math.min(Math.max(1, Math.floor(accountIndex) || 1), LD.MAX_ACCOUNT_INDEX);
for (let i = 0; i < n; i++) {
await keyTap(tabId, 'ArrowDown', 40);
await sleep(100);
}
await keyTap(tabId, 'Enter', 13);
await sleep(400); // let autofill write both fields
}
The sleeps are not polish; they are the protocol. The dropdown needs a frame to appear, and autofill needs a moment to write both fields before anything reads them back.
Read-Back Calibration: Dropdowns Drift
Here is the failure mode that makes naive automation dangerous: the browser's dropdown order is not stable. A login you used yesterday can move after a password change or a new saved account. "Position 2" silently becomes the wrong account — and the wrong account plus autofill is one Enter away from signing into the wrong identity.
So after every pick, the background reads the filled username back through the debugger and the content script compares it with the username remembered for that entry. Mismatch means: clear both fields, increment the position, retry — at most LD.MAX_TRUSTED_ATTEMPTS (3) times. The read-back is deliberately narrow:
// Only the username is read back — password values never leave the page.
const expr = `(function(){try{var el=document.querySelector(${JSON.stringify(selectors.username)});return el?String(el.value):null}catch(e){return null}})()`;
const res = await chrome.debugger.sendCommand({ tabId }, 'Runtime.evaluate', {
expression: expr, returnByValue: true
});
Clearing uses the native value setter plus input/change events, because React-style controlled inputs ignore a plain el.value = '':
var d = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), 'value');
if (d && d.set) d.set.call(el, ''); else el.value = '';
el.dispatchEvent(new Event('input', { bubbles: true }));
el.dispatchEvent(new Event('change', { bubbles: true }));
Fields inside a shadow root are skipped by calibration — the content script cannot hand out a selector that crosses the boundary, and a best-effort read that returns null simply ends the session without retrying.
The Invariant: Never Submit
Every safeguard above still assumes the dropdown opened. What if it didn't? Then the ArrowDowns went nowhere and the Enter keypress lands in the username field — which on most login forms submits it. An automation that can sign you in by accident is worse than no automation.
The content script therefore intercepts Enter and submit on the login fields, in the capture phase, for the whole trusted session. The keys still reach the dropdown (native UI consumes them before the page), but a form submission can never fire while we are driving. Clicking Sign in is always the user's own click. This is the invariant the whole feature is built around: the extension picks; the human submits.
The Verification Click, at the End
Pro also offers one best-effort click on "I'm not a robot" style checkboxes. It runs in ld-trusted-end, after the account is settled, using the same trusted channel, and then the debugger detaches. It is a convenience, not a solver: picture puzzles and audio challenges are left to the human, and if no checkbox is found, nothing happens.
What Never Leaves the Page
It is worth stating the data boundary plainly, because "automates login" sounds like "reads passwords":
- The password value is never read, stored or transmitted. The read-back expression selects the username field only.
- Filling is done by the browser's password manager, from the logins it already holds.
- Stored entries carry title, URL, icon and the last username — nothing else.
Trusted input is a powerful primitive; the design work is in bounding it. A session that attaches once, verifies what it picked, blocks submission, and detaches at the end is the difference between a convenience and a liability.