diff --git a/app/index.html b/app/index.html index 81ca874..b20d1ba 100644 --- a/app/index.html +++ b/app/index.html @@ -1288,6 +1288,10 @@ else if (event.channel === "reportError") { reportError(event.args[0]); } + else if (event.channel === "printImage") { + // the game's print kits and art studios (game.html ajPrint) + window.client.printImage(event.args[0]); + } else if (event.channel === "openPage") { // main.js opens http and https addresses only window.client.openPage(String(event.args[0])); diff --git a/app/main.js b/app/main.js index 35ad8ff..633575f 100644 --- a/app/main.js +++ b/app/main.js @@ -513,6 +513,50 @@ ipcMain.on("openPage", (event, url) => { } }); +// a picture the game prints (its print kits and art studios, through the +// game page's ajPrint): {image, a JPEG in base64; width; height}. it goes on +// a page of its own, in a window that is never shown and runs no script, +// and the system's print dialog opens for it; nothing of it is kept after. +const MAX_PRINT_BASE64 = 40 * 1024 * 1024; +ipcMain.on("printImage", (event, picture) => { + const image = picture && typeof picture.image === "string" ? picture.image : ""; + if (!image || image.length > MAX_PRINT_BASE64 || !/^[A-Za-z0-9+/=\r\n]+$/.test(image)) { + return; + } + const landscape = Number(picture.width) > Number(picture.height); + let folder; + try { + folder = fs.mkdtempSync(path.join(app.getPath("temp"), "burrow-print-")); + fs.writeFileSync(path.join(folder, "picture.jpg"), Buffer.from(image, "base64")); + fs.writeFileSync(path.join(folder, "print.html"), ""); + } + catch (error) { + return; + } + const cleanUp = () => { + try { + fs.rmdirSync(folder, {recursive: true}); + } + catch (error) { + // the temporary folder goes another time + } + }; + const page = new BrowserWindow({show: false, webPreferences: {javascript: false, sandbox: true, contextIsolation: true}}); + page.webContents.once("did-finish-load", () => { + page.webContents.print({silent: false, printBackground: true}, () => { + page.destroy(); + cleanUp(); + }); + }); + page.webContents.once("did-fail-load", () => { + page.destroy(); + cleanUp(); + }); + page.loadFile(path.join(folder, "print.html")); +}); + // a burrow:// link (above), once the login page is there to take it: only // the account page's nonce goes on, and the window comes to the front. const accountNonce = link => { diff --git a/app/preload.js b/app/preload.js index b346411..7e0ff75 100644 --- a/app/preload.js +++ b/app/preload.js @@ -26,6 +26,8 @@ contextBridge.exposeInMainWorld("client", { showFile: file => ipcRenderer.send("showFile", file), toggleFullscreen: () => ipcRenderer.send("toggleFullscreen"), openPage: url => ipcRenderer.send("openPage", url), + // a picture from the game to print (main.js): {image, width, height} + printImage: picture => ipcRenderer.send("printImage", picture), // the player's own theatre video file, which the game page may then load; "" when stopped. ownVideo: link => ipcRenderer.invoke("ownVideo", link), // Discord rich presence (discord.js): {appId, state, start, url}, or null.