Skip to content

Instantly share code, notes, and snippets.

@cornellb28
Forked from matt-allan/index.html
Created December 9, 2020 08:03
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 cornellb28/10b6bb05ad27fa9cec7971a5146eb9e3 to your computer and use it in GitHub Desktop.
Save cornellb28/10b6bb05ad27fa9cec7971a5146eb9e3 to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Context Bridge Example</title>
<link rel="stylesheet" type="text/css" href="./styles.css">
</head>
<body>
<main class="app">
<button onclick="window.app.setFullscreen(true)">Enter fullscreen</button>
<button onclick="window.app.setFullscreen(false)">Exit fullscreen</button>
</main>
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</body>
</html>
// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain} = require('electron')
const path = require('path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
allowRunningInsecureContent: false,
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false,
sandbox: true,
preload: path.join(app.getAppPath(), 'preload.js'),
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
ipcMain.handle('setFullscreen', (event, flag) => {
if (mainWindow) {
mainWindow.setFullScreen(flag)
}
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
const { ipcRenderer, contextBridge } = require('electron')
contextBridge.exposeInMainWorld(
'app',
{
setFullscreen: (flag) => ipcRenderer.invoke('setFullscreen', flag),
}
)
.app {
text-align: center;
margin: 10rem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment