From dcb43589804bd4489a44b1303d04190b965cd5ed Mon Sep 17 00:00:00 2001 From: Nikolay Bachiyski Date: Mon, 20 Jan 2020 15:29:20 +0200 Subject: [PATCH] Add an export npm script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Quite hacky at this point, though works. Approach: uses puppeteer to open a URL and press Space, while taking screenshots. We assume that at each transition the URL changes. Once it stops changing, we assume there are no more slides. Plenty of problems: - requires `node`; - assumes that the URL is visible without authentication and from the environment we are running the script from (there can be proxies, etc.); - no error handling – not sure what will happen if something breaks; - the assumption that the URL will change quickly enough is weak, I can see how it either finish too early or lead to an infinite loop. Knowing the number of slides in advance will be better; - assumes we have less than 1000 slides, so that we can take care of padding in file names. Seems the safest assumption compared to the ones above :) Can be lifted if we know the number of slides in advance; - Resolution is hard-coded at Full HD – 1920:1080. Quite typical projector resolution these days. --- bin/export.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 bin/export.js diff --git a/bin/export.js b/bin/export.js new file mode 100644 index 0000000..5ffe276 --- /dev/null +++ b/bin/export.js @@ -0,0 +1,36 @@ +const fsp = require("fs").promises; +const path = require("path"); +const puppeteer = require("puppeteer"); + +if (process.argv.length !== 4) { + console.error("Usage: npm run export [url] [output-dir]"); + process.exit(1); +} + +let url = process.argv[2]; +const outputDirectory = process.argv[3]; + +(async () => { + await fsp.mkdir(outputDirectory, { recursive: true }); + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.setViewport({ width: 1920, height: 1080 }); + let goingOn = true; + let slide = 1; + console.log(`▶️ Exporting all slides to ${outputDirectory}`); + await page.goto(url); + while (goingOn) { + const paddedSlide = + "0".repeat(2 - Math.floor(Math.log10(slide))) + slide.toString(); + console.log(` 📸 Exporting slide ${slide}…`); + await page.screenshot({ + path: path.join(outputDirectory, `slide${paddedSlide}.png`) + }); + slide++; + await page.keyboard.press("Space"); + goingOn = url !== page.url(); + url = page.url(); + } + await browser.close(); + console.log("✅ Done"); +})();