Skip to content

Instantly share code, notes, and snippets.

@codebytere
Last active February 1, 2022 10:30
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 codebytere/0f0e776b138452d6815ef4efc16aa0d1 to your computer and use it in GitHub Desktop.
Save codebytere/0f0e776b138452d6815ef4efc16aa0d1 to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<button onclick="window.print();" style="display: block; background-color: darkgreen; color: white; padding: 16px 32px; cursor: pointer;">WINDOW PRINT</button>
<button onclick="printMe()" style="display: block; background-color: purple; color: white; padding: 16px 32px; cursor: pointer;">WEBCONTENTS PRINT</button>
<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>.
<script>
// You can also require other files to run in this process
const { ipcRenderer } = require('electron')
function printMe() {
ipcRenderer.send('wc-print')
}
</script>
</body>
</html>
const {app, BrowserWindow, ipcMain} = require('electron')
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
})
mainWindow.loadFile('index.html')
mainWindow.on('closed', () => {
mainWindow = null
})
}
ipcMain.on('wc-print', async () => {
const win = new BrowserWindow({show: false})
await win.loadURL('http://example.com/')
win.webContents.print({}, (success, reason) => {
console.log(`printing ${success ? 'did' : 'did not'} succeed: ${reason}`)
})
win.on('closed', () => { win = null })
})
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', () => {
if (mainWindow === null) createWindow()
})
{
"name": "painful-dad-fly-nck7l",
"productName": "painful-dad-fly-nck7l",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "codebytere",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "999999999999.9.9"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment