A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

dbbasic-browser/apps/browser/src/webview-preload.cjs
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
webview-preload.cjs · 6.7 KB · 129 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).
6const { ipcRenderer } = require("electron");
7
8const visible = (el) => !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
9const 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.
12function 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
22function 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.
32let lastLink = null;
33function 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}
39const 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.
44const editable = (t) => !!(t && (t.isContentEditable || (t.closest && t.closest('input, textarea, select, [contenteditable=""], [contenteditable="true"]'))));
45window.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// ---- 0c. ⌘P, ⌘⇧S, ⌘+ ⌘− ⌘0, ⌘⇧J (Ctrl elsewhere): print, save as PDF, zoom, downloads ----
53// The menu's keys never reach the menu while a page has the keyboard. As in Chrome, the page gets them first (a
54// document editor prints its own way, a map zooms itself); when it leaves one alone, the browser runs it. By the key's
55// place, so any layout works.
56const PAGE_KEYS = { KeyP: "print", Equal: "zoom-in", NumpadAdd: "zoom-in", Minus: "zoom-out", NumpadSubtract: "zoom-out", Digit0: "zoom-reset", Numpad0: "zoom-reset" };
57window.addEventListener("keydown", (e) => {
58 if (!e.isTrusted || e.altKey) return;
59 const mac = /Mac/.test(navigator.platform);
60 if (mac ? !e.metaKey || e.ctrlKey : !e.ctrlKey || e.metaKey) return;
61 const cmd = e.code === "KeyS" && e.shiftKey ? "save-pdf" : e.code === "KeyJ" && e.shiftKey ? "downloads" : e.shiftKey && e.code !== "Equal" ? null : PAGE_KEYS[e.code];
62 if (!cmd) return;
63 setTimeout(() => { if (!e.defaultPrevented) ipcRenderer.sendToHost("page-cmd", cmd); }, 0);
64});
65document.addEventListener("mouseover", (e) => reportLink(linkOf(e.target)), true);
66document.addEventListener("mouseout", (e) => { if (!e.relatedTarget || !linkOf(e.relatedTarget)) reportLink(null); }, true);
67document.addEventListener("focusin", (e) => reportLink(linkOf(e.target)), true);
68document.addEventListener("focusout", () => reportLink(null), true);
69window.addEventListener("pagehide", () => reportLink(null));
70
71// ---- 1a. "there's a login form here" -------------------------------------
72let formTimer = null, lastFormKey = "";
73function announceForm() {
74 clearTimeout(formTimer);
75 formTimer = setTimeout(() => {
76 const n = pwInputs().length;
77 if (!n) return;
78 const key = location.href + "#" + n;
79 if (key === lastFormKey) return;
80 lastFormKey = key;
81 ipcRenderer.send("vault:form");
82 }, 300);
83}
84document.addEventListener("DOMContentLoaded", () => {
85 announceForm();
86 new MutationObserver(announceForm).observe(document.documentElement, { childList: true, subtree: true });
87});
88
89// ---- 1b. "the user just submitted credentials" ---------------------------
90let lastSent = "";
91function reportSubmit(scope) {
92 const pw = pwInputs(scope).find((p) => p.value);
93 if (!pw) return;
94 const u = usernameFor(pw);
95 const payload = { username: u ? u.value : "", password: pw.value };
96 const key = payload.username + "\0" + payload.password;
97 if (key === lastSent) return; // submit + click + Enter all fire for one login
98 lastSent = key;
99 setTimeout(() => (lastSent = ""), 1500);
100 ipcRenderer.send("vault:submit", payload);
101}
102// isTrusted: only real user input counts, never a page's synthetic events.
103document.addEventListener("submit", (e) => { if (e.isTrusted) reportSubmit(e.target); }, true);
104document.addEventListener("keydown", (e) => {
105 if (e.isTrusted && e.key === "Enter" && e.target && e.target.type === "password") reportSubmit(e.target.form || document);
106}, true);
107document.addEventListener("click", (e) => {
108 if (!e.isTrusted || !e.target.closest) return;
109 const b = e.target.closest('button, input[type="submit"], [role="button"]');
110 if (b) reportSubmit(b.form || b.closest("form") || document);
111}, true);
112
113// ---- 2. fill, on command from main ---------------------------------------
114ipcRenderer.on("vault:fill", (_e, { origin, username, password, submit }) => {
115 if (location.origin !== origin) return; // defence in depth: main already checked
116 const pw = pwInputs()[0];
117 if (!pw) return;
118 const u = usernameFor(pw);
119 if (u && username) setValue(u, username);
120 setValue(pw, password);
121 if (submit) {
122 const form = pw.form;
123 if (form && form.requestSubmit) form.requestSubmit();
124 else {
125 const btn = (form || document).querySelector('button[type="submit"], input[type="submit"], button:not([type])');
126 if (btn) btn.click();
127 }
128 }
129});