Skip to content
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

feat!: bump electron@^29.0.0 w/ supporting changes #263

Merged
merged 5 commits into from Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 25 additions & 7 deletions bin/templates/platform_www/cdv-electron-main.js
Expand Up @@ -25,7 +25,8 @@ const {
app,
BrowserWindow,
protocol,
ipcMain
ipcMain,
net
} = require('electron');
// Electron settings from .json file.
const cdvElectronSettings = require('./cdv-electron-settings.json');
Expand Down Expand Up @@ -74,6 +75,9 @@ function createWindow () {
const browserWindowOpts = Object.assign({}, cdvElectronSettings.browserWindow, { icon: appIcon });
browserWindowOpts.webPreferences.preload = path.join(app.getAppPath(), 'cdv-electron-preload.js');
browserWindowOpts.webPreferences.contextIsolation = true;
// @todo review if using default "sandbox" is possible. When enabled, "Unable to load preload script:" error occurs.
// Other require statements also fails.
browserWindowOpts.webPreferences.sandbox = false;

mainWindow = new BrowserWindow(browserWindowOpts);

Expand All @@ -99,12 +103,26 @@ function createWindow () {
}

function configureProtocol () {
protocol.registerFileProtocol(scheme, (request, cb) => {
const url = request.url.substr(basePath.length + 1);
cb({ path: path.normalize(path.join(__dirname, url)) }); // eslint-disable-line node/no-callback-literal
});

protocol.interceptFileProtocol('file', (_, cb) => { cb(null); });
// `protocol.handle` was added in Electron 25.0 and replaced the deprecated
// `protocol.{register,intercept}{String,Buffer,Stream,Http,File}Protocol`.
if (protocol.handle) {
// If using Electron 25.0+
protocol.handle(scheme, request => {
const url = request.url.substr(basePath.length + 1);
const fileUrl = `file://${path.normalize(path.join(__dirname, url))}`;
return net.fetch(fileUrl);
});
} else if (protocol.registerFileProtocol) {
// If using Electron 24.x and older
protocol.registerFileProtocol(scheme, (request, cb) => {
const url = request.url.substr(basePath.length + 1);
cb({ path: path.normalize(path.join(__dirname, url)) }); // eslint-disable-line node/no-callback-literal
});
protocol.interceptFileProtocol('file', (_, cb) => { cb(null); });
} else {
// Cant configure if missing `protocol.handle` and `protocol.registerFileProtocol`...
console.info('Unable to configure the protocol.');
}
}

// This method will be called when Electron has finished
Expand Down