Monorepo for Aesthetic.Computer
aesthetic.computer
1/**
2 * Aesthetic Computer - Webview Preload Script
3 *
4 * This preload runs inside the webview (the AC content) and provides
5 * a bridge to communicate with the parent Electron renderer (flip-view.html).
6 *
7 * Uses ipcRenderer.sendToHost() which is specifically designed for webview → parent communication.
8 */
9
10const { ipcRenderer } = require('electron');
11
12// Expose a minimal API for AC content to communicate with Electron
13window.acElectron = {
14 // Open a new AC Pane window
15 openWindow: (options = {}) => {
16 const { url, index = 0, total = 1 } = typeof options === 'string' ? { url: options } : options;
17 console.log('[webview-preload] openWindow:', url, index, total);
18 ipcRenderer.sendToHost('ac-open-window', { url, index, total });
19 },
20
21 // Close this window
22 closeWindow: () => {
23 console.log('[webview-preload] closeWindow');
24 ipcRenderer.sendToHost('ac-close-window');
25 },
26
27 // Check if we're in Electron
28 isElectron: true,
29
30 // Platform info
31 platform: process.platform,
32
33 // USB flash support (Linux only)
34 listBlockDevices: () => ipcRenderer.invoke('usb:list-devices'),
35 flashImage: (opts) => ipcRenderer.invoke('usb:flash-image', opts),
36};
37
38// Forward USB flash progress events from main process to window
39ipcRenderer.on('usb:flash-progress', (event, data) => {
40 window.dispatchEvent(new CustomEvent('usb:flash-progress', { detail: data }));
41});
42
43// Also listen for messages from the content and forward them
44// This catches postMessage calls from bios.mjs
45window.addEventListener('message', (e) => {
46 if (e.source !== window) return; // Only handle messages from this window
47
48 if (e.data?.type === 'ac-open-window') {
49 console.log('[webview-preload] Caught ac-open-window postMessage:', e.data);
50 ipcRenderer.sendToHost('ac-open-window', {
51 url: e.data.url,
52 index: e.data.index || 0,
53 total: e.data.total || 1,
54 });
55 } else if (e.data?.type === 'ac-close-window') {
56 console.log('[webview-preload] Caught ac-close-window postMessage');
57 ipcRenderer.sendToHost('ac-close-window');
58 }
59});
60
61console.log('[webview-preload] AC Electron bridge loaded');