Skip to content
9 changes: 6 additions & 3 deletions resources/js/electron-plugin/dist/preload/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import remote from "@electron/remote";
import { ipcRenderer } from "electron";
import { ipcRenderer, contextBridge } from "electron";
const Native = {
on: (event, callback) => {
ipcRenderer.on('native-event', (_, data) => {
Expand All @@ -15,8 +15,7 @@ const Native = {
menu.popup({ window: remote.getCurrentWindow() });
}
};
window.Native = Native;
window.remote = remote;
contextBridge.exposeInMainWorld('Native', Native);
ipcRenderer.on('log', (event, { level, message, context }) => {
if (level === 'error') {
console.error(`[${level}] ${message}`, context);
Expand Down Expand Up @@ -52,3 +51,7 @@ ipcRenderer.on('native-event', (event, data) => {
});
}
});
contextBridge.exposeInMainWorld('native:initialized', (function () {
window.dispatchEvent(new CustomEvent('native:init'));
return true;
})());
11 changes: 3 additions & 8 deletions resources/js/electron-plugin/dist/server/api/menuBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { compileMenu } from "./helper/index.js";
import state from "../state.js";
import { menubar } from "menubar";
import { notifyLaravel } from "../utils.js";
import { fileURLToPath } from 'url';
import { enable } from "@electron/remote/main/index.js";
import mergePreferences from "../webPreferences.js";
const router = express.Router();
router.post("/label", (req, res) => {
var _a;
Expand Down Expand Up @@ -51,7 +51,7 @@ router.post("/create", (req, res) => {
state.activeMenuBar.tray.destroy();
shouldSendCreatedEvent = false;
}
const { width, height, url, label, alwaysOnTop, vibrancy, backgroundColor, transparency, icon, showDockIcon, onlyShowContextMenu, windowPosition, showOnAllWorkspaces, contextMenu, tooltip, resizable, event, } = req.body;
const { width, height, url, label, alwaysOnTop, vibrancy, backgroundColor, transparency, icon, showDockIcon, onlyShowContextMenu, windowPosition, showOnAllWorkspaces, contextMenu, tooltip, resizable, webPreferences, event, } = req.body;
if (onlyShowContextMenu) {
const tray = new Tray(icon || state.icon.replace("icon.png", "IconTemplate.png"));
tray.setContextMenu(buildMenu(contextMenu));
Expand Down Expand Up @@ -81,12 +81,7 @@ router.post("/create", (req, res) => {
vibrancy,
backgroundColor,
transparent: transparency,
webPreferences: {
preload: fileURLToPath(new URL('../../electron-plugin/dist/preload/index.mjs', import.meta.url)),
nodeIntegration: true,
sandbox: false,
contextIsolation: false,
}
webPreferences: mergePreferences(webPreferences)
}
});
state.activeMenuBar.on("after-create-window", () => {
Expand Down
13 changes: 2 additions & 11 deletions resources/js/electron-plugin/dist/server/api/window.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import express from 'express';
import { BrowserWindow } from 'electron';
import state from '../state.js';
import { fileURLToPath } from 'url';
import { notifyLaravel, goToUrl, appendWindowIdToUrl } from '../utils.js';
import windowStateKeeper from 'electron-window-state';
import mergePreferences from '../webPreferences.js';
import { enable } from "@electron/remote/main/index.js";
const router = express.Router();
router.post('/maximize', (req, res) => {
Expand Down Expand Up @@ -152,15 +152,6 @@ router.post('/open', (req, res) => {
res.sendStatus(200);
return;
}
let preloadPath = fileURLToPath(new URL('../../electron-plugin/dist/preload/index.mjs', import.meta.url));
const defaultWebPreferences = {
backgroundThrottling: false,
spellcheck: false,
preload: preloadPath,
sandbox: false,
contextIsolation: false,
nodeIntegration: true,
};
let windowState = undefined;
if (req.body.rememberState === true) {
windowState = windowStateKeeper({
Expand All @@ -187,7 +178,7 @@ router.post('/open', (req, res) => {
focusable,
skipTaskbar,
hiddenInMissionControl,
autoHideMenuBar }, (process.platform === 'linux' ? { icon: state.icon } : {})), { webPreferences: Object.assign(Object.assign({}, webPreferences), defaultWebPreferences), fullscreen,
autoHideMenuBar }, (process.platform === 'linux' ? { icon: state.icon } : {})), { webPreferences: mergePreferences(webPreferences), fullscreen,
fullscreenable,
kiosk }));
if ((process.env.NODE_ENV === 'development' || showDevTools === true) && showDevTools !== false) {
Expand Down
15 changes: 15 additions & 0 deletions resources/js/electron-plugin/dist/server/webPreferences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { fileURLToPath } from 'url';
let preloadPath = fileURLToPath(new URL('../../electron-plugin/dist/preload/index.mjs', import.meta.url));
const defaultWebPreferences = {
spellcheck: false,
nodeIntegration: false,
backgroundThrottling: false,
};
const requiredWebPreferences = {
sandbox: false,
preload: preloadPath,
contextIsolation: true,
};
export default function (userWebPreferences = {}) {
return Object.assign(Object.assign(Object.assign({}, defaultWebPreferences), userWebPreferences), requiredWebPreferences);
}
39 changes: 32 additions & 7 deletions resources/js/electron-plugin/src/preload/index.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import remote from "@electron/remote";
import {ipcRenderer} from "electron";
import {ipcRenderer, contextBridge} from "electron";

// -------------------------------------------------------------------
// The Native helper
// -------------------------------------------------------------------
const Native = {
on: (event, callback) => {
ipcRenderer.on('native-event', (_, data) => {
Expand All @@ -19,12 +22,11 @@ const Native = {
}
};

// @ts-ignore
window.Native = Native;

// @ts-ignore
window.remote = remote;
contextBridge.exposeInMainWorld('Native', Native);

// -------------------------------------------------------------------
// Log events
// -------------------------------------------------------------------
ipcRenderer.on('log', (event, {level, message, context}) => {
if (level === 'error') {
console.error(`[${level}] ${message}`, context)
Expand All @@ -35,7 +37,10 @@ ipcRenderer.on('log', (event, {level, message, context}) => {
}
});

// Add Livewire event listeners

// -------------------------------------------------------------------
// Livewire event listeners
// -------------------------------------------------------------------
ipcRenderer.on('native-event', (event, data) => {

// Strip leading slashes
Expand Down Expand Up @@ -82,3 +87,23 @@ ipcRenderer.on('native-event', (event, data) => {
})
}
})

// -------------------------------------------------------------------
// Let the client know preload is fully evaluated
// -------------------------------------------------------------------
contextBridge.exposeInMainWorld('native:initialized', (function() {
// This is admittedly a bit hacky. Due to context isolation
// we don't have direct access to the renderer window object,
// but by assigning a bridge function that executes itself inside
// the renderer context we can hack around it.

// It's recommended to use window.postMessage & dispatch an
// event from the renderer itself, but we're loading webcontent
// from localhost. We don't have a renderer process we can access.
// Though this is hacky it works well and is the quickest way to do this
// without sprinkling additional logic all over the place.

window.dispatchEvent(new CustomEvent('native:init'));

return true;
})())
9 changes: 3 additions & 6 deletions resources/js/electron-plugin/src/server/api/menuBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { menubar } from "menubar";
import { notifyLaravel } from "../utils.js";
import { fileURLToPath } from 'url'
import { enable } from "@electron/remote/main/index.js";
import mergePreferences from "../webPreferences.js";

const router = express.Router();

Expand Down Expand Up @@ -88,6 +89,7 @@ router.post("/create", (req, res) => {
contextMenu,
tooltip,
resizable,
webPreferences,
event,
} = req.body;

Expand Down Expand Up @@ -129,12 +131,7 @@ router.post("/create", (req, res) => {
vibrancy,
backgroundColor,
transparent: transparency,
webPreferences: {
preload: fileURLToPath(new URL('../../electron-plugin/dist/preload/index.mjs', import.meta.url)),
nodeIntegration: true,
sandbox: false,
contextIsolation: false,
}
webPreferences: mergePreferences(webPreferences)
}
});

Expand Down
17 changes: 2 additions & 15 deletions resources/js/electron-plugin/src/server/api/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import state from '../state.js';
import { fileURLToPath } from 'url'
import { notifyLaravel, goToUrl, appendWindowIdToUrl } from '../utils.js';
import windowStateKeeper from 'electron-window-state';
import mergePreferences from '../webPreferences.js'

import {enable} from "@electron/remote/main/index.js";

Expand Down Expand Up @@ -247,17 +248,6 @@ router.post('/open', (req, res) => {
return;
}

let preloadPath = fileURLToPath(new URL('../../electron-plugin/dist/preload/index.mjs', import.meta.url));

const defaultWebPreferences = {
backgroundThrottling: false,
spellcheck: false,
preload: preloadPath,
sandbox: false,
contextIsolation: false,
nodeIntegration: true,
};

let windowState: windowStateKeeper.State | undefined = undefined;

if (req.body.rememberState === true) {
Expand Down Expand Up @@ -301,10 +291,7 @@ router.post('/open', (req, res) => {
hiddenInMissionControl,
autoHideMenuBar,
...(process.platform === 'linux' ? {icon: state.icon} : {}),
webPreferences: {
...webPreferences,
...defaultWebPreferences
},
webPreferences: mergePreferences(webPreferences),
fullscreen,
fullscreenable,
kiosk,
Expand Down
24 changes: 24 additions & 0 deletions resources/js/electron-plugin/src/server/webPreferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { fileURLToPath } from 'url'

let preloadPath = fileURLToPath(new URL('../../electron-plugin/dist/preload/index.mjs', import.meta.url));

const defaultWebPreferences = {
spellcheck: false,
nodeIntegration: false,
backgroundThrottling: false,
};

const requiredWebPreferences = {
sandbox: false,
preload: preloadPath,
contextIsolation: true,
}

export default function(userWebPreferences: object = {}): object
{
return {
...defaultWebPreferences,
...userWebPreferences,
...requiredWebPreferences
}
}
3 changes: 0 additions & 3 deletions resources/js/electron.vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ export default defineConfig({
},
},
plugins: [externalizeDepsPlugin()]
},
preload: {
plugins: [externalizeDepsPlugin()]
}
});
5 changes: 0 additions & 5 deletions resources/js/src/preload/index.js

This file was deleted.