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

0 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 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).
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});
52document.addEventListener("mouseover", (e) => reportLink(linkOf(e.target)), true);
53document.addEventListener("mouseout", (e) => { if (!e.relatedTarget || !linkOf(e.relatedTarget)) reportLink(null); }, true);
54document.addEventListener("focusin", (e) => reportLink(linkOf(e.target)), true);
55document.addEventListener("focusout", () => reportLink(null), true);
56window.addEventListener("pagehide", () => reportLink(null));
57
58// ---- 1a. "there's a login form here" -------------------------------------
59let formTimer = null, lastFormKey = "";
60function 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}
71document.addEventListener("DOMContentLoaded", () => {
72 announceForm();
73 new MutationObserver(announceForm).observe(document.documentElement, { childList: true, subtree: true });
74});
75
76// ---- 1b. "the user just submitted credentials" ---------------------------
77let lastSent = "";
78function 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.
90document.addEventListener("submit", (e) => { if (e.isTrusted) reportSubmit(e.target); }, true);
91document.addEventListener("keydown", (e) => {
92 if (e.isTrusted && e.key === "Enter" && e.target && e.target.type === "password") reportSubmit(e.target.form || document);
93}, true);
94document.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 ---------------------------------------
101ipcRenderer.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});