diff --git a/gluon.d.ts b/gluon.d.ts index 4f67c21..1cb5369 100644 --- a/gluon.d.ts +++ b/gluon.d.ts @@ -194,22 +194,10 @@ type PageApi = { ): Promise, /** - * Print (save) the page as a PDF, optionally to a file. + * Print (export) the page as a PDF. * @returns Buffer of PDF data. */ printToPDF( - /** Optional print options (based on Chromium's headless standard). */ - options?: PrintToPDFOptions - ): Promise, - - /** - * Print (save) the page as a PDF to a file. - * @returns Buffer of PDF data. - */ - printToPDF( - /** Path to save the PDF to. Do not include to just get a Buffer. */ - path: string, - /** Optional print options (based on Chromium's headless standard). */ options?: PrintToPDFOptions ): Promise diff --git a/src/api/page.js b/src/api/page.js new file mode 100644 index 0000000..f21cb5f --- /dev/null +++ b/src/api/page.js @@ -0,0 +1,34 @@ +export default async (CDP, evaluate, { pageLoadPromise }) => { + return { + eval: evaluate, + loaded: pageLoadPromise, + + title: val => { + if (!val) return evaluate('document.title'); + return evaluate(`document.title = \`${val}\``); + }, + + reload: async (ignoreCache = false) => { + await CDP.send('Page.reload', { + ignoreCache + }); + }, + + printToPDF: async (options = {}) => { + if (options.margins) { + const { top, bottom, left, right } = options.margins; + options.marginTop = top; + options.marginBottom = bottom; + options.marginLeft = left; + options.marginRight = right; + + delete options.margins; + } + + const { data } = await CDP.send('Page.printToPDF', options); + const buffer = Buffer.from(data, 'base64'); + + return buffer; + } + }; +}; \ No newline at end of file diff --git a/src/launcher/inject.js b/src/launcher/inject.js index 9e10179..c51fa70 100644 --- a/src/launcher/inject.js +++ b/src/launcher/inject.js @@ -4,6 +4,7 @@ import { log, logInline } from '../lib/logger.js'; import IPCApi from '../lib/ipc.js'; import LocalCDP from '../lib/local/cdp.js'; +import PageApi from '../api/page.js'; import IdleApi from '../api/idle.js'; import ControlsApi from '../api/controls.js'; import V8CacheApi from '../api/v8Cache.js'; @@ -135,53 +136,6 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN }; const Window = { - page: { - eval: evalInWindow, - loaded: pageLoadPromise, - - title: val => { - if (!val) return evalInWindow('document.title'); - return evalInWindow(`document.title = \`${val}\``); - }, - - reload: async (ignoreCache = false) => { - await Window.cdp.send('Page.reload', { - ignoreCache - }); - }, - - printToPDF: async (...args) => { - let path, options = {}; - - if (args.length === 1) { - if (typeof args[0] === 'string') path = args[0]; - else options = { ...args[0] }; - } - - if (args.length === 2) { - path = args[0]; - options = { ...args[1] }; - } - - if (options.margins) { - const { top, bottom, left, right } = options.margins; - if (top) options.marginTop = top; - if (bottom) options.marginBottom = bottom; - if (left) options.marginLeft = left; - if (right) options.marginRight = right; - - delete options.margins; - } - - const { data } = await Window.cdp.send('Page.printToPDF', options); - const buffer = Buffer.from(data, 'base64'); - - if (path) await writeFile(path, buffer); - - return buffer; - } - }, - ipc: IPC, cdp: { @@ -236,6 +190,7 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN process.on('SIGTERM', interruptHandler); // process.on('uncaughtException', interruptHandler); + Window.page = await PageApi(Window.cdp, evalInWindow, { pageLoadPromise }); Window.idle = await IdleApi(Window.cdp, { browserType, closeHandlers }); Window.controls = await ControlsApi(Window.cdp); Window.v8Cache = await V8CacheApi(Window.cdp, evalInWindow, { browserType, dataPath });