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
preload.cjs · 8.0 KB · 182 lines
| 1 | // Preload: the only bridge between the chrome UI and the main process. |
| 2 | // CommonJS on purpose — preload runs before the ESM app and stays tiny and sandbox-safe. |
| 3 | const { contextBridge, ipcRenderer } = require("electron"); |
| 4 | |
| 5 | const homepageArg = process.argv.find((a) => a.startsWith("--homepage=")); |
| 6 | const homepage = homepageArg ? homepageArg.slice("--homepage=".length) : ""; |
| 7 | const wvArg = process.argv.find((a) => a.startsWith("--webview-preload=")); |
| 8 | const webviewPreload = wvArg ? wvArg.slice("--webview-preload=".length) : ""; |
| 9 | const sub = (channel) => (callback) => { |
| 10 | const listener = (_event, payload) => callback(payload); |
| 11 | ipcRenderer.on(channel, listener); |
| 12 | return () => ipcRenderer.removeListener(channel, listener); |
| 13 | }; |
| 14 | |
| 15 | contextBridge.exposeInMainWorld("x402", { |
| 16 | /** Optional URL to open on startup (from the HOMEPAGE env var), or "". */ |
| 17 | homepage, |
| 18 | /** file:// URL of the script injected into every page (login detection + fill). */ |
| 19 | webviewPreload, |
| 20 | /** Password manager. Secrets never cross this bridge — see vault-service.ts. */ |
| 21 | vault: { |
| 22 | state: () => ipcRenderer.invoke("vault:state"), |
| 23 | create: (master) => ipcRenderer.invoke("vault:create", master), |
| 24 | unlock: (master) => ipcRenderer.invoke("vault:unlock", master), |
| 25 | lock: () => ipcRenderer.invoke("vault:lock"), |
| 26 | list: (wcId) => ipcRenderer.invoke("vault:list", wcId), |
| 27 | add: (args) => ipcRenderer.invoke("vault:add", args), |
| 28 | remove: (id) => ipcRenderer.invoke("vault:remove", id), |
| 29 | generate: (len) => ipcRenderer.invoke("vault:generate", len), |
| 30 | accept: (wcId) => ipcRenderer.invoke("vault:accept", wcId), |
| 31 | dismiss: (wcId) => ipcRenderer.invoke("vault:dismiss", wcId), |
| 32 | fill: (args) => ipcRenderer.invoke("vault:fill", args), |
| 33 | onState: sub("vault:state"), |
| 34 | onOffer: sub("vault:offer"), |
| 35 | }, |
| 36 | /** Subscribe to payment events (paid / skipped / quoted). Returns an unsubscribe fn. */ |
| 37 | onEvent(callback) { |
| 38 | const listener = (_event, payload) => callback(payload); |
| 39 | ipcRenderer.on("x402:event", listener); |
| 40 | return () => ipcRenderer.removeListener("x402:event", listener); |
| 41 | }, |
| 42 | /** AI Reader. Keys never cross this bridge: only provider/model names. */ |
| 43 | reader: { |
| 44 | open: (args) => ipcRenderer.invoke("reader:open", args), |
| 45 | ai: () => ipcRenderer.invoke("reader:ai"), |
| 46 | setProvider: (p) => ipcRenderer.invoke("reader:provider", p), |
| 47 | }, |
| 48 | /** The assistant chat. Keys never cross this bridge: only provider and model names. */ |
| 49 | chat: { |
| 50 | state: () => ipcRenderer.invoke("chat:state"), |
| 51 | send: (text) => ipcRenderer.invoke("chat:send", text), |
| 52 | stop: () => ipcRenderer.invoke("chat:stop"), |
| 53 | confirm: (id, ok) => ipcRenderer.invoke("chat:confirm", id, ok), |
| 54 | clear: () => ipcRenderer.invoke("chat:clear"), |
| 55 | setMode: (m) => ipcRenderer.invoke("chat:mode", m), |
| 56 | setModel: (id) => ipcRenderer.invoke("chat:model", id), |
| 57 | onEvent: sub("chat:event"), |
| 58 | }, |
| 59 | /** Keep: copies of pages (image and text), so their tabs can be closed. Local only. */ |
| 60 | keep: { |
| 61 | tab: (o) => ipcRenderer.invoke("keep:tab", o || {}), |
| 62 | list: (q) => ipcRenderer.invoke("keep:list", q || ""), |
| 63 | remove: (id) => ipcRenderer.invoke("keep:remove", id), |
| 64 | }, |
| 65 | /** Bookmarks: saved links on askrobots. */ |
| 66 | omnibox: { |
| 67 | suggest: (o) => ipcRenderer.invoke("omnibox:suggest", o || {}), |
| 68 | remote: (text) => ipcRenderer.invoke("omnibox:remote", String(text || "")), |
| 69 | run: (text, confirmed) => ipcRenderer.invoke("omnibox:run", { text: String(text || ""), confirmed: confirmed === true }), |
| 70 | }, |
| 71 | bookmarks: { |
| 72 | state: () => ipcRenderer.invoke("bookmarks:state"), |
| 73 | refresh: () => ipcRenderer.invoke("bookmarks:refresh"), |
| 74 | add: (url, title) => ipcRenderer.invoke("bookmarks:add", url, title), |
| 75 | search: (q) => ipcRenderer.invoke("bookmarks:search", q), |
| 76 | check: (url) => ipcRenderer.invoke("bookmarks:check", url), |
| 77 | setCheck: (on) => ipcRenderer.invoke("bookmarks:set-check", on), |
| 78 | onChanged: sub("bookmarks:changed"), |
| 79 | }, |
| 80 | /** Browsing history on the askrobots account (recorded only while signed in, with the history permission). */ |
| 81 | /** Site icons as data: URLs, by host (see favicons.ts). onUpdate gets {host, icon} (icon null: forgotten). */ |
| 82 | favicons: { |
| 83 | all: () => ipcRenderer.invoke("favicons:all"), |
| 84 | onUpdate: sub("favicon:update"), |
| 85 | }, |
| 86 | history: { |
| 87 | state: () => ipcRenderer.invoke("history:state"), |
| 88 | pause: (on) => ipcRenderer.invoke("history:pause", on), |
| 89 | open: () => ipcRenderer.invoke("history:open"), |
| 90 | forget: (scope, target) => ipcRenderer.invoke("history:forget", scope, target), |
| 91 | never: (target) => ipcRenderer.invoke("history:never", target), |
| 92 | onChanged: sub("history:changed"), |
| 93 | }, |
| 94 | /** What the browser is using (memory and CPU per tab, cache, disk). */ |
| 95 | resources: { |
| 96 | state: () => ipcRenderer.invoke("resources:state"), |
| 97 | clearCache: () => ipcRenderer.invoke("resources:clear-cache"), |
| 98 | }, |
| 99 | /** Connections to other services (MCP). Tokens never cross this bridge. */ |
| 100 | mcp: { |
| 101 | state: () => ipcRenderer.invoke("mcp:state"), |
| 102 | connect: (id) => ipcRenderer.invoke("mcp:connect", id), |
| 103 | cancel: (id) => ipcRenderer.invoke("mcp:cancel", id), |
| 104 | disconnect: (id) => ipcRenderer.invoke("mcp:disconnect", id), |
| 105 | }, |
| 106 | /** Tell the main process the loading-animation preference (always|system|never) so the menu can show it. */ |
| 107 | reportMotion(mode) { |
| 108 | ipcRenderer.send("pref:motion", mode); |
| 109 | }, |
| 110 | /** Per-request network progress: {id,url,type:start|headers|progress|end|redirect|error,...}. */ |
| 111 | onNetEvent: sub("net:event"), |
| 112 | /** Menu accelerator commands: new | close | focus-url | next | prev | reload. */ |
| 113 | onTabCommand(callback) { |
| 114 | const listener = (_event, cmd) => callback(cmd); |
| 115 | ipcRenderer.on("tab:cmd", listener); |
| 116 | return () => ipcRenderer.removeListener("tab:cmd", listener); |
| 117 | }, |
| 118 | /** A page asked for a new window (target=_blank / window.open) — open it as a tab. */ |
| 119 | onOpenTab(callback) { |
| 120 | const listener = (_event, url, opts) => callback(url, opts || {}); |
| 121 | ipcRenderer.on("tab:open", listener); |
| 122 | return () => ipcRenderer.removeListener("tab:open", listener); |
| 123 | }, |
| 124 | /** A right-click menu item the chrome does itself: search for the selection, or ask the assistant about it. */ |
| 125 | onMenuAction(callback) { |
| 126 | const listener = (_event, a) => callback(a); |
| 127 | ipcRenderer.on("ctx:action", listener); |
| 128 | return () => ipcRenderer.removeListener("ctx:action", listener); |
| 129 | }, |
| 130 | /** Automation: main asks the UI to do a tab operation; the handler's return value is the answer. */ |
| 131 | onControl(handler) { |
| 132 | ipcRenderer.on("ctl:req", async (_e, { id, op, args }) => { |
| 133 | try { |
| 134 | ipcRenderer.send("ctl:res", { id, result: await handler(op, args) }); |
| 135 | } catch (e) { |
| 136 | ipcRenderer.send("ctl:res", { id, error: String((e && e.message) || e) }); |
| 137 | } |
| 138 | }); |
| 139 | }, |
| 140 | /** Mirror of the tab list for the automation API. */ |
| 141 | syncTabs(list) { |
| 142 | ipcRenderer.send("tabs:sync", list); |
| 143 | }, |
| 144 | /** Fires when the automation API is listening ({port}). */ |
| 145 | onControlStatus(callback) { |
| 146 | ipcRenderer.on("ctl:status", (_e, payload) => callback(payload)); |
| 147 | }, |
| 148 | /** Current window spend, formatted. */ |
| 149 | spent() { |
| 150 | return ipcRenderer.invoke("x402:spent"); |
| 151 | }, |
| 152 | /** All receipts in the ledger. */ |
| 153 | receipts() { |
| 154 | return ipcRenderer.invoke("x402:receipts"); |
| 155 | }, |
| 156 | /** Subscribe to wallet balance updates ({usdc, address}). */ |
| 157 | onBalance(callback) { |
| 158 | const listener = (_event, payload) => callback(payload); |
| 159 | ipcRenderer.on("x402:balance", listener); |
| 160 | return () => ipcRenderer.removeListener("x402:balance", listener); |
| 161 | }, |
| 162 | /** The wallet address ({address}). */ |
| 163 | wallet() { |
| 164 | return ipcRenderer.invoke("x402:wallet"); |
| 165 | }, |
| 166 | /** Open an http(s) URL in the operating system's default browser. */ |
| 167 | openExternal(url) { |
| 168 | return ipcRenderer.invoke("x402:open-external", url); |
| 169 | }, |
| 170 | /** Copy text to the clipboard. */ |
| 171 | copy(text) { |
| 172 | return ipcRenderer.invoke("x402:copy", text); |
| 173 | }, |
| 174 | /** Copy the address and open the faucet in the OS browser. */ |
| 175 | fund() { |
| 176 | return ipcRenderer.invoke("x402:fund"); |
| 177 | }, |
| 178 | /** Refund the merchant wallet back to the buyer ({tx} or {error}). */ |
| 179 | refund() { |
| 180 | return ipcRenderer.invoke("x402:refund"); |
| 181 | }, |
| 182 | }); |