Created
September 5, 2019 15:24
-
-
Save CharlieHess/2a11467f1eaabb55ada63a8c691cad64 to your computer and use it in GitHub Desktop.
Proxy BrowserWindow with allowed methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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