Created
January 12, 2023 12:41
-
-
Save SkySails/8570c187ab7c976b70fd84835db5f27c to your computer and use it in GitHub Desktop.
macOS `webContents.print` error repro
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"> | |
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP --> | |
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'"> | |
<link href="./styles.css" rel="stylesheet"> | |
<title>Hello World!</title> | |
</head> | |
<body> | |
<h1>Hello World!</h1> | |
We are using Node.js <span id="node-version"></span>, | |
Chromium <span id="chrome-version"></span>, | |
and Electron <span id="electron-version"></span>.<br /><br /> | |
<button id="print-native">Print native</button> | |
<button id="print-js">Print JS</button> | |
<h2>Notes</h2> | |
<ul> | |
<li>Pressing "Print native" on macOS with no installed printers should yield no dialog and an error in the console</li> | |
<li>Pressing "Print JS" on macOS with no installed printers should yield a dialog, and when said dialog is closed, `true` should show up in the log</li> | |
</ul> | |
<!-- You can also require other files to run in this process --> | |
<script src="./renderer.js"></script> | |
</body> | |
</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, ipcMain } = require('electron') | |
const path = require('path') | |
function createWindow() { | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
preload: path.join(__dirname, 'preload.js') | |
} | |
}) | |
mainWindow.loadFile('index.html') | |
registerPrintHandlers(mainWindow); | |
} | |
app.whenReady().then(() => { | |
createWindow() | |
app.on('activate', function () { | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
}) | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) | |
/** @param window {BrowserWindow} */ | |
function registerPrintHandlers(window) { | |
ipcMain.handle("print-native", () => | |
new Promise(async (resolve, reject) => window.webContents.print(undefined, (s, f) => { | |
if (f) return reject(f); | |
resolve(s); | |
})), | |
) | |
ipcMain.handle("print-js", () => | |
new Promise(async (resolve, reject) => window.webContents | |
.executeJavaScript("window.print()") | |
.then(() => resolve(true)).catch(reject) | |
), | |
) | |
} |
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
{ | |
"name": "spiffy-association-entertain-88tpu", | |
"productName": "spiffy-association-entertain-88tpu", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "skysail", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "20.1.4" | |
} | |
} |
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 { contextBridge, ipcRenderer } = require("electron") | |
/** | |
* The preload script runs before. It has access to web APIs | |
* as well as Electron's renderer process modules and some | |
* polyfilled Node.js functions. | |
* | |
* https://www.electronjs.org/docs/latest/tutorial/sandbox | |
*/ | |
window.addEventListener('DOMContentLoaded', () => { | |
const replaceText = (selector, text) => { | |
const element = document.getElementById(selector) | |
if (element) element.innerText = text | |
} | |
for (const type of ['chrome', 'node', 'electron']) { | |
replaceText(`${type}-version`, process.versions[type]) | |
} | |
}) | |
contextBridge.exposeInMainWorld("electronAPI", { | |
printNative: () => ipcRenderer.invoke("print-native"), | |
printJS: () => ipcRenderer.invoke("print-js"), | |
}) |
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
document.getElementById("print-native").addEventListener("click", () => { | |
window.electronAPI.printNative().then(console.log).catch(console.error) | |
}) | |
document.getElementById("print-js").addEventListener("click", () => { | |
window.electronAPI.printJS().then(console.log).catch(console.error) | |
}) |
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
/* styles.css */ | |
/* Add styles here to customize the appearance of your app */ | |
* { | |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment