Skip to content

Commit b5301df

Browse files
use onlook:// protocol to inject preload script in website (TODO: serve in onlook:// urls in vite)
1 parent 1ee5611 commit b5301df

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

apps/studio/electron/main/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { APP_NAME, APP_SCHEMA } from '@onlook/models/constants';
2-
import { BrowserWindow, app, shell } from 'electron';
2+
import { BrowserWindow, app, shell, protocol } from 'electron';
33
import fixPath from 'fix-path';
44
import { createRequire } from 'node:module';
55
import os from 'node:os';
@@ -11,6 +11,7 @@ import { listenForIpcMessages, removeIpcListeners } from './events';
1111
import run from './run';
1212
import terminal from './run/terminal';
1313
import { updater } from './update';
14+
import fs from 'node:fs';
1415

1516
// Help main inherit $PATH defined in dotfiles (.bashrc/.bash_profile/.zshrc/etc).
1617
fixPath();
@@ -142,6 +143,10 @@ const listenForExitEvents = () => {
142143

143144
const setupAppEventListeners = () => {
144145
app.whenReady().then(() => {
146+
protocol.handle('onlook', (request) => {
147+
const filePath = path.join(__dirname, '../preload/webview.js');
148+
return new Response(fs.readFileSync(filePath));
149+
});
145150
listenForExitEvents();
146151
initMainWindow();
147152
});
@@ -190,6 +195,20 @@ const main = () => {
190195
setupEnvironment();
191196
configurePlatformSpecifics();
192197

198+
// Register onlook protocol handler for browser-compatible preload script urls
199+
protocol.registerSchemesAsPrivileged([
200+
{
201+
scheme: 'onlook',
202+
privileges: {
203+
standard: true,
204+
secure: true,
205+
allowServiceWorkers: true,
206+
supportFetchAPI: true,
207+
stream: true,
208+
},
209+
},
210+
]);
211+
193212
if (!app.requestSingleInstanceLock()) {
194213
app.quit();
195214
process.exit(0);

apps/studio/src/lib/editor/engine/frameview.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,23 @@ export const FrameView = forwardRef<IFrameView, IFrameViewProps>(({ preload, ...
3939
const handleIframeLoad = useCallback(() => {
4040
const iframe = iframeRef.current;
4141
if (!iframe || !preload) {
42+
console.error('No iframe or preload');
4243
return;
4344
}
4445

4546
const injectPreloadScript = () => {
4647
try {
4748
const doc = iframe.contentDocument;
4849
if (!doc) {
50+
console.error('No document found');
4951
return;
5052
}
5153

5254
const script = doc.createElement('script');
5355
script.src = preload;
5456
doc.head.appendChild(script);
5557
} catch (error) {
56-
console.error('Failed to inject preload script:', error);
58+
console.error('Preload injection failed:', error);
5759
}
5860
};
5961

apps/studio/src/routes/editor/WebviewArea/Frame.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ const Frame = observer(
342342
selected ? getSelectedOutlineColor() : 'outline-transparent',
343343
)}
344344
src={settings.url}
345-
preload={`file://${window.env.WEBVIEW_PRELOAD_PATH}`}
345+
preload="onlook://webview-preload.js"
346346
sandbox="allow-popups allow-scripts allow-same-origin"
347347
style={{
348348
width: clampedDimensions.width,

0 commit comments

Comments
 (0)