Skip to content

Instantly share code, notes, and snippets.

@CharlieHess
Created September 5, 2019 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlieHess/2a11467f1eaabb55ada63a8c691cad64 to your computer and use it in GitHub Desktop.
Save CharlieHess/2a11467f1eaabb55ada63a8c691cad64 to your computer and use it in GitHub Desktop.
Proxy BrowserWindow with allowed methods
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</body>
<script>
document.addEventListener('DOMContentLoaded', () => {
const childWindow = window.open('https://google.com');
childWindow.setSize(400, 400);
childWindow.hide();
})
</script>
</html>
const {app, BrowserWindow} = require('electron');
const path = require('path');
let mainWindow;
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(app.getAppPath(), 'preload.js')
}
});
mainWindow.loadFile('index.html');
mainWindow.on('closed', function () {
mainWindow = null;
});
}
app.on('ready', createWindow);
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
if (mainWindow === null) {
createWindow();
}
});
const {remote} = require('electron');
const BrowserWindow = remote.BrowserWindow;
const ALLOWED_METHODS = [
'setSize'
];
window.open = (url) => {
const browserWindow = new BrowserWindow();
browserWindow.loadURL(url);
browserWindow.show();
return new Proxy(
browserWindow,
{
get: (target, propKey, receiver) => {
return ALLOWED_METHODS.includes(propKey) ? (...args) => {
return target[propKey].apply(target, args);
} : () => {
console.warn(`Tried to call unsupported method '${propKey}'`);
};
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment