-
Notifications
You must be signed in to change notification settings - Fork 469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(draft): replace <webview> with <iframe> #1191
base: main
Are you sure you want to change the base?
Changes from all commits
c40a245
d8c01f9
5305361
166d8ac
1ee5611
b5301df
2f151e3
48fea5a
bcdb85f
9a5a297
5b0861c
78d9484
30f0b36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { APP_NAME, APP_SCHEMA } from '@onlook/models/constants'; | ||
import { BrowserWindow, app, shell } from 'electron'; | ||
import { BrowserWindow, app, shell, protocol } from 'electron'; | ||
import fixPath from 'fix-path'; | ||
import { createRequire } from 'node:module'; | ||
import os from 'node:os'; | ||
|
@@ -11,6 +11,7 @@ import { listenForIpcMessages, removeIpcListeners } from './events'; | |
import run from './run'; | ||
import terminal from './run/terminal'; | ||
import { updater } from './update'; | ||
import fs from 'node:fs'; | ||
|
||
// Help main inherit $PATH defined in dotfiles (.bashrc/.bash_profile/.zshrc/etc). | ||
fixPath(); | ||
|
@@ -142,6 +143,10 @@ const listenForExitEvents = () => { | |
|
||
const setupAppEventListeners = () => { | ||
app.whenReady().then(() => { | ||
protocol.handle('onlook', (request) => { | ||
const filePath = path.join(__dirname, '../preload/webview.js'); | ||
return new Response(fs.readFileSync(filePath)); | ||
}); | ||
listenForExitEvents(); | ||
initMainWindow(); | ||
}); | ||
|
@@ -190,6 +195,20 @@ const main = () => { | |
setupEnvironment(); | ||
configurePlatformSpecifics(); | ||
|
||
// Register onlook protocol handler for browser-compatible preload script urls | ||
protocol.registerSchemesAsPrivileged([ | ||
{ | ||
scheme: 'onlook', | ||
privileges: { | ||
standard: true, | ||
secure: true, | ||
allowServiceWorkers: true, | ||
supportFetchAPI: true, | ||
stream: true, | ||
}, | ||
Comment on lines
+202
to
+208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Enabling all these protocol privileges may expose security risks. Consider limiting to only required privileges |
||
}, | ||
]); | ||
|
||
if (!app.requestSingleInstanceLock()) { | ||
app.quit(); | ||
process.exit(0); | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,3 @@ | ||||||||||||||||||
import { contextBridge } from 'electron'; | ||||||||||||||||||
import { processDom } from './dom'; | ||||||||||||||||||
import { | ||||||||||||||||||
getChildrenCount, | ||||||||||||||||||
|
@@ -22,42 +21,67 @@ import { editText, startEditingText, stopEditingText } from './elements/text'; | |||||||||||||||||
import { setWebviewId } from './state'; | ||||||||||||||||||
import { getTheme, setTheme } from './theme'; | ||||||||||||||||||
|
||||||||||||||||||
const onlookApi = { | ||||||||||||||||||
// Misc | ||||||||||||||||||
processDom, | ||||||||||||||||||
getComputedStyleByDomId, | ||||||||||||||||||
updateElementInstance, | ||||||||||||||||||
setWebviewId, | ||||||||||||||||||
|
||||||||||||||||||
// Elements | ||||||||||||||||||
getElementAtLoc, | ||||||||||||||||||
getDomElementByDomId, | ||||||||||||||||||
setElementType, | ||||||||||||||||||
getElementType, | ||||||||||||||||||
getParentElement, | ||||||||||||||||||
getChildrenCount, | ||||||||||||||||||
|
||||||||||||||||||
// Actions | ||||||||||||||||||
getActionLocation, | ||||||||||||||||||
getActionElementByDomId, | ||||||||||||||||||
getInsertLocation, | ||||||||||||||||||
getRemoveActionFromDomId, | ||||||||||||||||||
|
||||||||||||||||||
// Theme | ||||||||||||||||||
getTheme, | ||||||||||||||||||
setTheme, | ||||||||||||||||||
|
||||||||||||||||||
// Drag | ||||||||||||||||||
startDrag, | ||||||||||||||||||
drag, | ||||||||||||||||||
endDrag, | ||||||||||||||||||
getElementIndex, | ||||||||||||||||||
endAllDrag, | ||||||||||||||||||
|
||||||||||||||||||
// Edit text | ||||||||||||||||||
startEditingText, | ||||||||||||||||||
editText, | ||||||||||||||||||
stopEditingText, | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
export type TOnlookWindow = typeof window & { | ||||||||||||||||||
_onlookWebviewId: string; | ||||||||||||||||||
onlook: { | ||||||||||||||||||
api: typeof onlookApi; | ||||||||||||||||||
bridge: { | ||||||||||||||||||
send: (channel: string, data?: any, transfer?: Transferable[]) => void; | ||||||||||||||||||
receive: (handler: (event: MessageEvent, data?: any) => void) => void; | ||||||||||||||||||
}; | ||||||||||||||||||
}; | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
export function setApi() { | ||||||||||||||||||
contextBridge.exposeInMainWorld('api', { | ||||||||||||||||||
// Misc | ||||||||||||||||||
processDom, | ||||||||||||||||||
getComputedStyleByDomId, | ||||||||||||||||||
updateElementInstance, | ||||||||||||||||||
setWebviewId, | ||||||||||||||||||
|
||||||||||||||||||
// Elements | ||||||||||||||||||
getElementAtLoc, | ||||||||||||||||||
getDomElementByDomId, | ||||||||||||||||||
setElementType, | ||||||||||||||||||
getElementType, | ||||||||||||||||||
getParentElement, | ||||||||||||||||||
getChildrenCount, | ||||||||||||||||||
|
||||||||||||||||||
// Actions | ||||||||||||||||||
getActionLocation, | ||||||||||||||||||
getActionElementByDomId, | ||||||||||||||||||
getInsertLocation, | ||||||||||||||||||
getRemoveActionFromDomId, | ||||||||||||||||||
|
||||||||||||||||||
// Theme | ||||||||||||||||||
getTheme, | ||||||||||||||||||
setTheme, | ||||||||||||||||||
|
||||||||||||||||||
// Drag | ||||||||||||||||||
startDrag, | ||||||||||||||||||
drag, | ||||||||||||||||||
endDrag, | ||||||||||||||||||
getElementIndex, | ||||||||||||||||||
endAllDrag, | ||||||||||||||||||
|
||||||||||||||||||
// Edit text | ||||||||||||||||||
startEditingText, | ||||||||||||||||||
editText, | ||||||||||||||||||
stopEditingText, | ||||||||||||||||||
}); | ||||||||||||||||||
(window as TOnlookWindow).onlook = { | ||||||||||||||||||
api: onlookApi, | ||||||||||||||||||
bridge: { | ||||||||||||||||||
send: (channel: string, data?: any, transfer?: Transferable[]) => { | ||||||||||||||||||
window.parent.postMessage({ type: channel, data }, '*', transfer); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Using '*' as targetOrigin in postMessage is a security risk. Should restrict to specific origins. |
||||||||||||||||||
}, | ||||||||||||||||||
receive: (handler: (event: MessageEvent, data?: any) => void) => { | ||||||||||||||||||
window.addEventListener('message', (event) => { | ||||||||||||||||||
handler(event, event.data); | ||||||||||||||||||
}); | ||||||||||||||||||
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Missing origin validation in message event listener. Should verify event.origin matches expected domains.
Suggested change
|
||||||||||||||||||
}, | ||||||||||||||||||
}, | ||||||||||||||||||
}; | ||||||||||||||||||
Comment on lines
+74
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Direct window object modification without Object.defineProperty may allow overwriting of onlook API. Consider using Object.defineProperty with configurable: false. |
||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { EditorAttributes, WebviewChannels } from '@onlook/models/constants'; | ||
import type { LayerNode } from '@onlook/models/element'; | ||
import { ipcRenderer } from 'electron'; | ||
import { buildLayerTree } from '../dom'; | ||
import type { TOnlookWindow } from '../api'; | ||
|
||
export function listenForDomMutation() { | ||
const targetNode = document.body; | ||
|
@@ -46,7 +46,7 @@ export function listenForDomMutation() { | |
} | ||
|
||
if (added.size > 0 || removed.size > 0) { | ||
ipcRenderer.sendToHost(WebviewChannels.WINDOW_MUTATED, { | ||
(window as TOnlookWindow).onlook.bridge.send(WebviewChannels.WINDOW_MUTATED, { | ||
added: Object.fromEntries(added), | ||
removed: Object.fromEntries(removed), | ||
}); | ||
Comment on lines
+49
to
52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Type assertion to TOnlookWindow could fail silently if window.onlook or bridge is undefined. Consider adding a null check or error handling. |
||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,7 +7,6 @@ import type { | |||||||||||||
ImageContentData, | ||||||||||||||
} from '@onlook/models/actions'; | ||||||||||||||
import { WebviewChannels } from '@onlook/models/constants'; | ||||||||||||||
import { ipcRenderer } from 'electron'; | ||||||||||||||
import { processDom } from '../dom'; | ||||||||||||||
import { groupElements, ungroupElements } from '../elements/dom/group'; | ||||||||||||||
import { insertImage, removeImage } from '../elements/dom/image'; | ||||||||||||||
|
@@ -25,6 +24,7 @@ import { | |||||||||||||
publishStyleUpdate, | ||||||||||||||
publishUngroupElement, | ||||||||||||||
} from './publish'; | ||||||||||||||
import type { TOnlookWindow } from '../api'; | ||||||||||||||
|
||||||||||||||
export function listenForEvents() { | ||||||||||||||
listenForWindowEvents(); | ||||||||||||||
|
@@ -34,12 +34,12 @@ export function listenForEvents() { | |||||||||||||
|
||||||||||||||
function listenForWindowEvents() { | ||||||||||||||
window.addEventListener('resize', () => { | ||||||||||||||
ipcRenderer.sendToHost(WebviewChannels.WINDOW_RESIZED); | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.send(WebviewChannels.WINDOW_RESIZED); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Type casting window on every bridge access is repetitive and error-prone. Consider creating a helper function or storing the bridge reference. |
||||||||||||||
}); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
function listenForEditEvents() { | ||||||||||||||
ipcRenderer.on(WebviewChannels.UPDATE_STYLE, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId, change } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
change: Change<Record<string, string>>; | ||||||||||||||
|
@@ -48,7 +48,7 @@ function listenForEditEvents() { | |||||||||||||
publishStyleUpdate(domId); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.INSERT_ELEMENT, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { element, location, editText } = data as { | ||||||||||||||
element: ActionElement; | ||||||||||||||
location: ActionLocation; | ||||||||||||||
|
@@ -60,13 +60,13 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.REMOVE_ELEMENT, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { location } = data as { location: ActionLocation }; | ||||||||||||||
removeElement(location); | ||||||||||||||
publishRemoveElement(location); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.MOVE_ELEMENT, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId, newIndex } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
newIndex: number; | ||||||||||||||
|
@@ -77,7 +77,7 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.EDIT_ELEMENT_TEXT, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId, content } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
content: string; | ||||||||||||||
|
@@ -88,7 +88,7 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.GROUP_ELEMENTS, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { parent, container, children } = data as { | ||||||||||||||
parent: ActionTarget; | ||||||||||||||
container: GroupContainer; | ||||||||||||||
|
@@ -100,7 +100,7 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.UNGROUP_ELEMENTS, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { parent, container, children } = data as { | ||||||||||||||
parent: ActionTarget; | ||||||||||||||
container: GroupContainer; | ||||||||||||||
|
@@ -112,7 +112,7 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.INSERT_IMAGE, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId, image } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
image: ImageContentData; | ||||||||||||||
|
@@ -121,15 +121,15 @@ function listenForEditEvents() { | |||||||||||||
publishStyleUpdate(domId); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.REMOVE_IMAGE, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
}; | ||||||||||||||
removeImage(domId); | ||||||||||||||
publishStyleUpdate(domId); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.CLEAN_AFTER_WRITE_TO_CODE, () => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive(() => { | ||||||||||||||
processDom(); | ||||||||||||||
}); | ||||||||||||||
Comment on lines
+132
to
134
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: This receive handler has no channel identifier and no data type checking. Consider adding type safety for the message data.
Suggested change
|
||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Reading file synchronously could block the main process. Consider using async fs.promises.readFile instead