Skip to content

Commit a4a02fc

Browse files
Add possibility to prevent navigation away from current domain or page (#250)
Co-authored-by: Willem Leuverink <willem@leuver.ink>
1 parent ce8a88e commit a4a02fc

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

resources/js/electron-plugin/dist/server/api/window.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function getWindowData(id) {
145145
};
146146
}
147147
router.post('/open', (req, res) => {
148-
let { id, x, y, frame, width, height, minWidth, minHeight, maxWidth, maxHeight, focusable, skipTaskbar, hiddenInMissionControl, hasShadow, url, resizable, movable, minimizable, maximizable, closable, title, alwaysOnTop, titleBarStyle, trafficLightPosition, vibrancy, backgroundColor, transparency, showDevTools, fullscreen, fullscreenable, kiosk, autoHideMenuBar, webPreferences, zoomFactor, suppressNewWindows, } = req.body;
148+
let { id, x, y, frame, width, height, minWidth, minHeight, maxWidth, maxHeight, focusable, skipTaskbar, hiddenInMissionControl, hasShadow, url, resizable, movable, minimizable, maximizable, closable, title, alwaysOnTop, titleBarStyle, trafficLightPosition, vibrancy, backgroundColor, transparency, showDevTools, fullscreen, fullscreenable, kiosk, autoHideMenuBar, webPreferences, zoomFactor, preventLeaveDomain, preventLeavePage, suppressNewWindows, } = req.body;
149149
if (state.windows[id]) {
150150
state.windows[id].show();
151151
state.windows[id].focus();
@@ -261,6 +261,18 @@ router.post('/open', (req, res) => {
261261
window.webContents.on('dom-ready', () => {
262262
window.webContents.setZoomFactor(parseFloat(zoomFactor));
263263
});
264+
if (preventLeaveDomain || preventLeavePage) {
265+
window.webContents.on('will-navigate', (event, target) => {
266+
const origUrl = new URL(url);
267+
const targetUrl = new URL(target);
268+
if (preventLeaveDomain && targetUrl.hostname !== origUrl.hostname) {
269+
event.preventDefault();
270+
}
271+
if (preventLeavePage && (targetUrl.origin !== origUrl.origin || targetUrl.pathname !== origUrl.pathname)) {
272+
event.preventDefault();
273+
}
274+
});
275+
}
264276
window.webContents.on('did-finish-load', () => {
265277
window.show();
266278
});

resources/js/electron-plugin/src/server/api/window.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ router.post('/open', (req, res) => {
234234
autoHideMenuBar,
235235
webPreferences,
236236
zoomFactor,
237+
preventLeaveDomain,
238+
preventLeavePage,
237239
suppressNewWindows,
238240
} = req.body;
239241

@@ -397,6 +399,21 @@ router.post('/open', (req, res) => {
397399
window.webContents.setZoomFactor(parseFloat(zoomFactor));
398400
});
399401

402+
if (preventLeaveDomain || preventLeavePage) {
403+
window.webContents.on('will-navigate', (event, target) => {
404+
const origUrl = new URL(url);
405+
const targetUrl = new URL(target);
406+
407+
if (preventLeaveDomain && targetUrl.hostname !== origUrl.hostname) {
408+
event.preventDefault();
409+
}
410+
411+
if (preventLeavePage && (targetUrl.origin !== origUrl.origin || targetUrl.pathname !== origUrl.pathname)) {
412+
event.preventDefault();
413+
}
414+
});
415+
}
416+
400417
window.webContents.on('did-finish-load', () => {
401418
window.show();
402419
});

0 commit comments

Comments
 (0)