Last active
June 7, 2018 06:37
-
-
Save henrikbjorn/1c03420f74274cb762fdd1202e8fa5db to your computer and use it in GitHub Desktop.
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
// @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') | |
}) |
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
// @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