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
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
preload.cjs · 9.3 KB · 209 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 | cards: { |
| 44 | open: (args) => ipcRenderer.invoke("cards:open", args), |
| 45 | }, |
| 46 | /** The Downloads panel: the list, and what to do with one (open, show, cancel, retry, remove) by its id. */ |
| 47 | downloads: { |
| 48 | list: () => ipcRenderer.invoke("downloads:list"), |
| 49 | act: (id, act) => ipcRenderer.invoke("downloads:act", { id, act }), |
| 50 | }, |
| 51 | /** Print, Save as PDF and zoom for one tab (by its webContents id). */ |
| 52 | page: { |
| 53 | print: (wcId) => ipcRenderer.invoke("page:print", { wcId }), |
| 54 | pdf: (wcId) => ipcRenderer.invoke("page:pdf", { wcId }), |
| 55 | zoom: (wcId, move) => ipcRenderer.invoke("page:zoom", { wcId, move }), |
| 56 | onZoom: sub("tab:zoom"), |
| 57 | }, |
| 58 | reader: { |
| 59 | open: (args) => ipcRenderer.invoke("reader:open", args), |
| 60 | ai: () => ipcRenderer.invoke("reader:ai"), |
| 61 | setProvider: (p) => ipcRenderer.invoke("reader:provider", p), |
| 62 | }, |
| 63 | /** The assistant chat. Keys never cross this bridge: only provider and model names. */ |
| 64 | chat: { |
| 65 | state: () => ipcRenderer.invoke("chat:state"), |
| 66 | send: (text) => ipcRenderer.invoke("chat:send", text), |
| 67 | stop: () => ipcRenderer.invoke("chat:stop"), |
| 68 | confirm: (id, ok) => ipcRenderer.invoke("chat:confirm", id, ok), |
| 69 | clear: () => ipcRenderer.invoke("chat:clear"), |
| 70 | setMode: (m) => ipcRenderer.invoke("chat:mode", m), |
| 71 | setModel: (id) => ipcRenderer.invoke("chat:model", id), |
| 72 | onEvent: sub("chat:event"), |
| 73 | }, |
| 74 | /** Keep: copies of pages (image and text), so their tabs can be closed. Local only. */ |
| 75 | keep: { |
| 76 | tab: (o) => ipcRenderer.invoke("keep:tab", o || {}), |
| 77 | list: (q) => ipcRenderer.invoke("keep:list", q || ""), |
| 78 | remove: (id) => ipcRenderer.invoke("keep:remove", id), |
| 79 | }, |
| 80 | /** Bookmarks: saved links on askrobots. */ |
| 81 | omnibox: { |
| 82 | suggest: (o) => ipcRenderer.invoke("omnibox:suggest", o || {}), |
| 83 | remote: (text) => ipcRenderer.invoke("omnibox:remote", String(text || "")), |
| 84 | run: (text, confirmed) => ipcRenderer.invoke("omnibox:run", { text: String(text || ""), confirmed: confirmed === true }), |
| 85 | }, |
| 86 | bookmarks: { |
| 87 | state: () => ipcRenderer.invoke("bookmarks:state"), |
| 88 | refresh: () => ipcRenderer.invoke("bookmarks:refresh"), |
| 89 | add: (url, title) => ipcRenderer.invoke("bookmarks:add", url, title), |
| 90 | search: (q) => ipcRenderer.invoke("bookmarks:search", q), |
| 91 | check: (url) => ipcRenderer.invoke("bookmarks:check", url), |
| 92 | setCheck: (on) => ipcRenderer.invoke("bookmarks:set-check", on), |
| 93 | onChanged: sub("bookmarks:changed"), |
| 94 | }, |
| 95 | /** Browsing history on the askrobots account (recorded only while signed in, with the history permission). */ |
| 96 | /** Site icons as data: URLs, by host (see favicons.ts). onUpdate gets {host, icon} (icon null: forgotten). */ |
| 97 | favicons: { |
| 98 | all: () => ipcRenderer.invoke("favicons:all"), |
| 99 | onUpdate: sub("favicon:update"), |
| 100 | }, |
| 101 | /** Plugins (style rules from gitstable). Calls answer {ok, value} or {ok: false, error}. */ |
| 102 | plugins: { |
| 103 | state: () => ipcRenderer.invoke("plugins:state"), |
| 104 | preview: (source) => ipcRenderer.invoke("plugins:preview", source), |
| 105 | install: (token) => ipcRenderer.invoke("plugins:install", token), |
| 106 | enable: (id, on) => ipcRenderer.invoke("plugins:enable", id, on), |
| 107 | remove: (id) => ipcRenderer.invoke("plugins:remove", id), |
| 108 | allOff: (off) => ipcRenderer.invoke("plugins:all-off", off), |
| 109 | onChanged: sub("plugins:changed"), |
| 110 | /** gitstable's Install button was followed: open Plugins with that release looked up. */ |
| 111 | onOpen: sub("plugins:open"), |
| 112 | }, |
| 113 | history: { |
| 114 | state: () => ipcRenderer.invoke("history:state"), |
| 115 | pause: (on) => ipcRenderer.invoke("history:pause", on), |
| 116 | open: () => ipcRenderer.invoke("history:open"), |
| 117 | forget: (scope, target) => ipcRenderer.invoke("history:forget", scope, target), |
| 118 | never: (target) => ipcRenderer.invoke("history:never", target), |
| 119 | onChanged: sub("history:changed"), |
| 120 | }, |
| 121 | /** What the browser is using (memory and CPU per tab, cache, disk). */ |
| 122 | resources: { |
| 123 | state: () => ipcRenderer.invoke("resources:state"), |
| 124 | clearCache: () => ipcRenderer.invoke("resources:clear-cache"), |
| 125 | }, |
| 126 | /** Connections to other services (MCP). Tokens never cross this bridge. */ |
| 127 | mcp: { |
| 128 | state: () => ipcRenderer.invoke("mcp:state"), |
| 129 | connect: (id) => ipcRenderer.invoke("mcp:connect", id), |
| 130 | cancel: (id) => ipcRenderer.invoke("mcp:cancel", id), |
| 131 | disconnect: (id) => ipcRenderer.invoke("mcp:disconnect", id), |
| 132 | }, |
| 133 | /** Tell the main process the loading-animation preference (always|system|never) so the menu can show it. */ |
| 134 | reportMotion(mode) { |
| 135 | ipcRenderer.send("pref:motion", mode); |
| 136 | }, |
| 137 | /** Per-request network progress: {id,url,type:start|headers|progress|end|redirect|error,...}. */ |
| 138 | onNetEvent: sub("net:event"), |
| 139 | /** Menu accelerator commands: new | close | focus-url | next | prev | reload. */ |
| 140 | onTabCommand(callback) { |
| 141 | const listener = (_event, cmd) => callback(cmd); |
| 142 | ipcRenderer.on("tab:cmd", listener); |
| 143 | return () => ipcRenderer.removeListener("tab:cmd", listener); |
| 144 | }, |
| 145 | /** A page asked for a new window (target=_blank / window.open) — open it as a tab. */ |
| 146 | onOpenTab(callback) { |
| 147 | const listener = (_event, url, opts) => callback(url, opts || {}); |
| 148 | ipcRenderer.on("tab:open", listener); |
| 149 | return () => ipcRenderer.removeListener("tab:open", listener); |
| 150 | }, |
| 151 | /** A right-click menu item the chrome does itself: search for the selection, or ask the assistant about it. */ |
| 152 | onMenuAction(callback) { |
| 153 | const listener = (_event, a) => callback(a); |
| 154 | ipcRenderer.on("ctx:action", listener); |
| 155 | return () => ipcRenderer.removeListener("ctx:action", listener); |
| 156 | }, |
| 157 | /** Automation: main asks the UI to do a tab operation; the handler's return value is the answer. */ |
| 158 | onControl(handler) { |
| 159 | ipcRenderer.on("ctl:req", async (_e, { id, op, args }) => { |
| 160 | try { |
| 161 | ipcRenderer.send("ctl:res", { id, result: await handler(op, args) }); |
| 162 | } catch (e) { |
| 163 | ipcRenderer.send("ctl:res", { id, error: String((e && e.message) || e) }); |
| 164 | } |
| 165 | }); |
| 166 | }, |
| 167 | /** Mirror of the tab list for the automation API. */ |
| 168 | syncTabs(list) { |
| 169 | ipcRenderer.send("tabs:sync", list); |
| 170 | }, |
| 171 | /** Fires when the automation API is listening ({port}). */ |
| 172 | onControlStatus(callback) { |
| 173 | ipcRenderer.on("ctl:status", (_e, payload) => callback(payload)); |
| 174 | }, |
| 175 | /** Current window spend, formatted. */ |
| 176 | spent() { |
| 177 | return ipcRenderer.invoke("x402:spent"); |
| 178 | }, |
| 179 | /** All receipts in the ledger. */ |
| 180 | receipts() { |
| 181 | return ipcRenderer.invoke("x402:receipts"); |
| 182 | }, |
| 183 | /** Subscribe to wallet balance updates ({usdc, address}). */ |
| 184 | onBalance(callback) { |
| 185 | const listener = (_event, payload) => callback(payload); |
| 186 | ipcRenderer.on("x402:balance", listener); |
| 187 | return () => ipcRenderer.removeListener("x402:balance", listener); |
| 188 | }, |
| 189 | /** The wallet address ({address}). */ |
| 190 | wallet() { |
| 191 | return ipcRenderer.invoke("x402:wallet"); |
| 192 | }, |
| 193 | /** Open an http(s) URL in the operating system's default browser. */ |
| 194 | openExternal(url) { |
| 195 | return ipcRenderer.invoke("x402:open-external", url); |
| 196 | }, |
| 197 | /** Copy text to the clipboard. */ |
| 198 | copy(text) { |
| 199 | return ipcRenderer.invoke("x402:copy", text); |
| 200 | }, |
| 201 | /** Copy the address and open the faucet in the OS browser. */ |
| 202 | fund() { |
| 203 | return ipcRenderer.invoke("x402:fund"); |
| 204 | }, |
| 205 | /** Refund the merchant wallet back to the buyer ({tx} or {error}). */ |
| 206 | refund() { |
| 207 | return ipcRenderer.invoke("x402:refund"); |
| 208 | }, |
| 209 | }); |