askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
webview-preload.cjs · 5.7 KB · 116 lines
| 1 | // Runs inside every page (the <webview> guest). It does exactly two things: |
| 2 | // 1. notices login forms / login submits and tells the MAIN process (not the page, |
| 3 | // not the chrome UI) so the password manager can offer to fill or save; |
| 4 | // 2. fills fields when main says so — after re-checking the origin itself. |
| 5 | // It exposes nothing to the page's own JavaScript (no contextBridge). |
| 6 | const { ipcRenderer } = require("electron"); |
| 7 | |
| 8 | const visible = (el) => !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length); |
| 9 | const pwInputs = (scope = document) => [...scope.querySelectorAll('input[type="password"]')].filter(visible); |
| 10 | |
| 11 | // The username field is the closest preceding visible text-ish input in the same form. |
| 12 | function usernameFor(pw) { |
| 13 | const scope = pw.form || document; |
| 14 | const cands = [...scope.querySelectorAll("input")].filter( |
| 15 | (i) => i !== pw && visible(i) && /^(text|email|tel|)$/i.test(i.getAttribute("type") || "") && |
| 16 | (i.compareDocumentPosition(pw) & Node.DOCUMENT_POSITION_FOLLOWING), |
| 17 | ); |
| 18 | const hinted = cands.filter((i) => /user|email|login/i.test(`${i.autocomplete} ${i.name} ${i.id}`)); |
| 19 | return (hinted.length ? hinted : cands).pop() || null; |
| 20 | } |
| 21 | |
| 22 | function setValue(el, v) { |
| 23 | // Use the native setter + real events so React/Vue-controlled inputs notice. |
| 24 | const set = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), "value").set; |
| 25 | set.call(el, v); |
| 26 | el.dispatchEvent(new Event("input", { bubbles: true })); |
| 27 | el.dispatchEvent(new Event("change", { bubbles: true })); |
| 28 | } |
| 29 | |
| 30 | // ---- 0. which link is under the pointer / keyboard focus (shown in the browser's status bar) ---- |
| 31 | // Sends only the link's absolute address to the browser UI; nothing else about the page. |
| 32 | let lastLink = null; |
| 33 | function reportLink(a) { |
| 34 | const href = a && a.href ? String(a.href) : null; |
| 35 | if (href === lastLink) return; |
| 36 | lastLink = href; |
| 37 | ipcRenderer.sendToHost("link-hover", href); |
| 38 | } |
| 39 | const linkOf = (t) => (t && t.closest ? t.closest("a[href], area[href]") : null); |
| 40 | |
| 41 | // ---- 0b. ⌘← / ⌘→ (Alt+← / Alt+→ elsewhere) go back and forward, as in other browsers ---- |
| 42 | // Only when the key is not for the page: never in a text field (there it moves the cursor), and not when the page |
| 43 | // handled it itself (a slideshow, an editor). The check waits until the page's own listeners have run. |
| 44 | const editable = (t) => !!(t && (t.isContentEditable || (t.closest && t.closest('input, textarea, select, [contenteditable=""], [contenteditable="true"]')))); |
| 45 | window.addEventListener("keydown", (e) => { |
| 46 | if (!e.isTrusted || e.shiftKey || (e.key !== "ArrowLeft" && e.key !== "ArrowRight")) return; |
| 47 | const mac = /Mac/.test(navigator.platform); |
| 48 | if (mac ? !e.metaKey || e.altKey || e.ctrlKey : !e.altKey || e.metaKey || e.ctrlKey) return; |
| 49 | if (editable(e.target) || editable(document.activeElement)) return; |
| 50 | setTimeout(() => { if (!e.defaultPrevented) ipcRenderer.sendToHost("nav-key", e.key === "ArrowLeft" ? "back" : "forward"); }, 0); |
| 51 | }); |
| 52 | document.addEventListener("mouseover", (e) => reportLink(linkOf(e.target)), true); |
| 53 | document.addEventListener("mouseout", (e) => { if (!e.relatedTarget || !linkOf(e.relatedTarget)) reportLink(null); }, true); |
| 54 | document.addEventListener("focusin", (e) => reportLink(linkOf(e.target)), true); |
| 55 | document.addEventListener("focusout", () => reportLink(null), true); |
| 56 | window.addEventListener("pagehide", () => reportLink(null)); |
| 57 | |
| 58 | // ---- 1a. "there's a login form here" ------------------------------------- |
| 59 | let formTimer = null, lastFormKey = ""; |
| 60 | function announceForm() { |
| 61 | clearTimeout(formTimer); |
| 62 | formTimer = setTimeout(() => { |
| 63 | const n = pwInputs().length; |
| 64 | if (!n) return; |
| 65 | const key = location.href + "#" + n; |
| 66 | if (key === lastFormKey) return; |
| 67 | lastFormKey = key; |
| 68 | ipcRenderer.send("vault:form"); |
| 69 | }, 300); |
| 70 | } |
| 71 | document.addEventListener("DOMContentLoaded", () => { |
| 72 | announceForm(); |
| 73 | new MutationObserver(announceForm).observe(document.documentElement, { childList: true, subtree: true }); |
| 74 | }); |
| 75 | |
| 76 | // ---- 1b. "the user just submitted credentials" --------------------------- |
| 77 | let lastSent = ""; |
| 78 | function reportSubmit(scope) { |
| 79 | const pw = pwInputs(scope).find((p) => p.value); |
| 80 | if (!pw) return; |
| 81 | const u = usernameFor(pw); |
| 82 | const payload = { username: u ? u.value : "", password: pw.value }; |
| 83 | const key = payload.username + "\0" + payload.password; |
| 84 | if (key === lastSent) return; // submit + click + Enter all fire for one login |
| 85 | lastSent = key; |
| 86 | setTimeout(() => (lastSent = ""), 1500); |
| 87 | ipcRenderer.send("vault:submit", payload); |
| 88 | } |
| 89 | // isTrusted: only real user input counts, never a page's synthetic events. |
| 90 | document.addEventListener("submit", (e) => { if (e.isTrusted) reportSubmit(e.target); }, true); |
| 91 | document.addEventListener("keydown", (e) => { |
| 92 | if (e.isTrusted && e.key === "Enter" && e.target && e.target.type === "password") reportSubmit(e.target.form || document); |
| 93 | }, true); |
| 94 | document.addEventListener("click", (e) => { |
| 95 | if (!e.isTrusted || !e.target.closest) return; |
| 96 | const b = e.target.closest('button, input[type="submit"], [role="button"]'); |
| 97 | if (b) reportSubmit(b.form || b.closest("form") || document); |
| 98 | }, true); |
| 99 | |
| 100 | // ---- 2. fill, on command from main --------------------------------------- |
| 101 | ipcRenderer.on("vault:fill", (_e, { origin, username, password, submit }) => { |
| 102 | if (location.origin !== origin) return; // defence in depth: main already checked |
| 103 | const pw = pwInputs()[0]; |
| 104 | if (!pw) return; |
| 105 | const u = usernameFor(pw); |
| 106 | if (u && username) setValue(u, username); |
| 107 | setValue(pw, password); |
| 108 | if (submit) { |
| 109 | const form = pw.form; |
| 110 | if (form && form.requestSubmit) form.requestSubmit(); |
| 111 | else { |
| 112 | const btn = (form || document).querySelector('button[type="submit"], input[type="submit"], button:not([type])'); |
| 113 | if (btn) btn.click(); |
| 114 | } |
| 115 | } |
| 116 | }); |