Skip to content

Commit

Permalink
Actually expose some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Jul 1, 2023
1 parent 0328ac3 commit 7015477
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
7 changes: 0 additions & 7 deletions src/packager/electron-preload.js

This file was deleted.

80 changes: 80 additions & 0 deletions src/packager/electron-superpowers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const exposeBrowserWindowMethods = [
// https://www.electronjs.org/docs/latest/api/browser-window
'focus',
'blur',
'isFocused',
'show',
'showInactive',
'hide',
'isVisible',
'maximize',
'unmaximize',
'isMaximized',
'minimize',
'restore',
'isMinimized',
'isNormal',
'setAspectRatio',
'setBounds',
'getBounds',
'setContentBounds',
'getContentBounds',
'getNormalBounds',
'setEnabled',
'isEnabled',
'setSize',
'getSize',
'setContentSize',
'getContentSize',
'setMinimumSize',
'getMinimumSize',
'setMaximumSize',
'getMaximumSize',
'setResizable',
'isResizable',
'setMovable',
'isMovable',
'setMinimizable',
'isMinimizable',
'setMaximizable',
'isMaximizable',
'setClosable',
'isClosable',
'setAlwaysOnTop',
'isAlwaysOnTop',
'moveTop',
'center',
'setPosition',
'getPosition',
'setProgressBar',
'setOpacity',
'getOpacity',
'setFocusable',
'isFocusable'
];

const main = `
const {ipcMain, BrowserWindow} = require('electron');
const ALLOWED_METHODS = ${JSON.stringify(exposeBrowserWindowMethods)};
ipcMain.handle('superpowers/BrowserWindow', (event, name, ...args) => {
if (!ALLOWED_METHODS.includes(name)) {
throw new Error('Unsupported BrowserWindow method: ' + name);
}
return BrowserWindow.fromWebContents(event.sender)[name](...args);
});
`.trimStart();

const preload = `
const {contextBridge, ipcRenderer} = require('electron');
contextBridge.exposeInMainWorld('PackagerSuperpowers', {
BrowserWindow: (name, ...args) => ipcRenderer.invoke('superpowers/BrowserWindow', name, ...args)
});
`.trimStart();

export default {
main,
preload
};
14 changes: 9 additions & 5 deletions src/packager/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {APP_NAME, WEBSITE, COPYRIGHT_NOTICE, ACCENT_COLOR} from './brand';
import {OutdatedPackagerError} from '../common/errors';
import {darken} from './colors';
import {Adapter} from './adapter';
import electronPreloadJS from '!raw-loader!./electron-preload.js';
import electronSuperpowerScripts from './electron-superpowers';

const PROGRESS_LOADED_SCRIPTS = 0.1;

Expand Down Expand Up @@ -513,7 +513,8 @@ cd "$(dirname "$0")"
const contentsPrefix = isMac ? `${rootPrefix}${packageName}.app/Contents/` : rootPrefix;
const resourcesPrefix = isMac ? `${contentsPrefix}Resources/app/` : `${contentsPrefix}resources/app/`;
const electronMainName = 'electron-main.js';
const electronPreloadName = 'electron-preload.js';
const electronSuperpowersMainName = 'electron-superpowers.js';
const electronSuperpowersPreloadName = 'electron-preload.js';
const iconName = 'icon.png';

const icon = await Adapter.getAppIcon(this.options.app.icon);
Expand All @@ -527,7 +528,7 @@ cd "$(dirname "$0")"
zip.file(`${resourcesPrefix}package.json`, JSON.stringify(manifest, null, 4));

const mainJS = `'use strict';
const {app, BrowserWindow, Menu, shell, screen, dialog} = require('electron');
const {app, BrowserWindow, Menu, shell, screen, dialog, ipcMain} = require('electron');
const path = require('path');
const isWindows = process.platform === 'win32';
Expand Down Expand Up @@ -560,7 +561,7 @@ const createWindow = (windowOptions) => {
sandbox: true,
contextIsolation: true,
nodeIntegration: false,
preload: path.resolve(__dirname, ${JSON.stringify(electronPreloadName)}),
preload: path.resolve(__dirname, ${JSON.stringify(electronSuperpowersPreloadName)}),
},
show: true,
width: 480,
Expand Down Expand Up @@ -698,9 +699,12 @@ app.on('window-all-closed', () => {
app.whenReady().then(() => {
createProjectWindow(defaultProjectURL);
});
require('./' + ${JSON.stringify(electronSuperpowersMainName)});
`;
zip.file(`${resourcesPrefix}${electronMainName}`, mainJS);
zip.file(`${resourcesPrefix}${electronPreloadName}`, electronPreloadJS);
zip.file(`${resourcesPrefix}${electronSuperpowersMainName}`, electronSuperpowerScripts.main)
zip.file(`${resourcesPrefix}${electronSuperpowersPreloadName}`, electronSuperpowerScripts.preload);

for (const [path, data] of Object.entries(projectZip.files)) {
setFileFast(zip, `${resourcesPrefix}${path}`, data);
Expand Down

0 comments on commit 7015477

Please sign in to comment.