|
1 | | -const { contextBridge, ipcRenderer } = require('electron'); |
| 1 | +'use strict'; |
2 | 2 |
|
3 | | -contextBridge.exposeInMainWorld('api', { |
4 | | - // Fetch posts |
5 | | - fetchBooru: (payload) => ipcRenderer.invoke('booru:fetch', payload), |
| 3 | +const { contextBridge, ipcRenderer, shell } = require('electron'); |
6 | 4 |
|
7 | | - // Config |
| 5 | +// Normalize site argument for helpers that accept either a site object or { site } |
| 6 | +const pickSite = (arg) => (arg && typeof arg === 'object' && 'site' in arg ? arg.site : arg); |
| 7 | + |
| 8 | +contextBridge.exposeInMainWorld('api', { |
| 9 | + // ---------------- Config ---------------- |
8 | 10 | loadConfig: () => ipcRenderer.invoke('config:load'), |
9 | 11 | saveConfig: (cfg) => ipcRenderer.invoke('config:save', cfg), |
10 | 12 |
|
11 | | - // External |
12 | | - openExternal: (url) => ipcRenderer.invoke('openExternal', url), |
| 13 | + // ---------------- Fetch posts ---------------- |
| 14 | + fetchBooru: (payload) => ipcRenderer.invoke('booru:fetch', payload), |
| 15 | + |
| 16 | + // ---------------- External/open ---------------- |
| 17 | + // Prefer shell.openExternal, fall back to a main-process handler |
| 18 | + openExternal: async (url) => { |
| 19 | + try { |
| 20 | + await shell.openExternal(url); |
| 21 | + return true; |
| 22 | + } catch { |
| 23 | + try { |
| 24 | + return await ipcRenderer.invoke('openExternal', url); |
| 25 | + } catch { |
| 26 | + return false; |
| 27 | + } |
| 28 | + } |
| 29 | + }, |
13 | 30 |
|
| 31 | + // ---------------- Images ---------------- |
14 | 32 | // Single image download |
15 | 33 | downloadImage: ({ url, siteName, fileName }) => |
16 | 34 | ipcRenderer.invoke('download:image', { url, siteName, fileName }), |
17 | 35 |
|
18 | | - // BULK download |
| 36 | + // Bulk download |
19 | 37 | downloadBulk: (items, options = {}) => |
20 | 38 | ipcRenderer.invoke('download:bulk', { items, options }), |
21 | 39 |
|
22 | 40 | // Image proxy (to data URL) |
23 | 41 | proxyImage: (url) => ipcRenderer.invoke('image:proxy', { url }), |
24 | 42 |
|
25 | | - // Favorites remote |
| 43 | + // ---------------- Remote favorites (site APIs) ---------------- |
| 44 | + // New name kept |
26 | 45 | booruFavorite: (payload) => ipcRenderer.invoke('booru:favorite', payload), |
| 46 | + // Back-compat alias |
| 47 | + favoritePost: (payload) => ipcRenderer.invoke('booru:favorite', payload), |
| 48 | + |
| 49 | + // ---------------- helpers ---------------- |
| 50 | + // Accept (site) or ({ site }) |
| 51 | + authCheck: (siteOrPayload) => |
| 52 | + ipcRenderer.invoke('booru:authCheck', { site: pickSite(siteOrPayload) }), |
| 53 | + |
| 54 | + // New helper name |
| 55 | + rateLimit: (siteOrPayload) => |
| 56 | + ipcRenderer.invoke('booru:rateLimit', { site: pickSite(siteOrPayload) }), |
| 57 | + // Back-compat alias used in older code |
| 58 | + rateLimitCheck: (siteOrPayload) => |
| 59 | + ipcRenderer.invoke('booru:rateLimit', { site: pickSite(siteOrPayload) }), |
27 | 60 |
|
28 | | - // Favorites local |
| 61 | + // ---------------- Local favorites (app storage) ---------------- |
| 62 | + // New short names |
29 | 63 | favKeys: () => ipcRenderer.invoke('favorites:keys'), |
30 | 64 | favList: () => ipcRenderer.invoke('favorites:list'), |
31 | 65 | favToggle: (post) => ipcRenderer.invoke('favorites:toggle', { post }), |
32 | 66 | favClear: () => ipcRenderer.invoke('favorites:clear'), |
33 | 67 |
|
34 | | - // Optional helpers |
35 | | - authCheck: (site) => ipcRenderer.invoke('booru:authCheck', { site }), |
36 | | - rateLimit: (site) => ipcRenderer.invoke('booru:rateLimit', { site }) |
| 68 | + getLocalFavoriteKeys: () => ipcRenderer.invoke('favorites:keys'), |
| 69 | + getLocalFavorites: () => ipcRenderer.invoke('favorites:list'), |
| 70 | + toggleLocalFavorite: (post) => ipcRenderer.invoke('favorites:toggle', { post }), |
| 71 | + clearLocalFavorites: () => ipcRenderer.invoke('favorites:clear'), |
37 | 72 | }); |
0 commit comments