Skip to content

Instantly share code, notes, and snippets.

@henrikbjorn
Last active June 7, 2018 06:37
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 henrikbjorn/1c03420f74274cb762fdd1202e8fa5db to your computer and use it in GitHub Desktop.
Save henrikbjorn/1c03420f74274cb762fdd1202e8fa5db to your computer and use it in GitHub Desktop.
// @flow
require('dotenv').config()
const electron = require('electron')
const path = require('path')
const { app, BrowserWindow, ipcMain } = electron
let mainWindow
function createWindow() {
const webPreferences = {
preload: path.join(__dirname, 'preload.js')
}
mainWindow = new BrowserWindow({ webPreferences, width: 1440, height: 900 })
mainWindow.loadURL('https://google.com')
mainWindow.on('closed', () => {
mainWindow = null
})
if (process.env.NODE_ENV == "development") {
mainWindow.webContents.openDevTools()
}
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
app.on('open-url', () => {
if (mainWindow) {
mainWindow.webContents.send('postMessage', 'Send a message via postMessage')
}
})
ipcMain.on('postMessage', () => {
console.log('We received a postMessage from the preload script')
})
// @flow
const { ipcRenderer } = require('electron')
window.addEventListener('message', ({ data }) => {
// our embedded site did a window.postMessage and therefor we will
// proxy it back to our main process
ipcRenderer.send('postMessage', data)
})
ipcRenderer.on('postMessage', (event, ...args) => {
// We received an event on the postMessage channel from
// the main process. Do a window.postMessage to forward it
window.postMessage([], '*')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment