api/page: move to separate file
This commit is contained in:
parent
fdd7ea4dad
commit
2649c37c20
14
gluon.d.ts
vendored
14
gluon.d.ts
vendored
@ -194,22 +194,10 @@ type PageApi = {
|
|||||||
): Promise<void>,
|
): Promise<void>,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print (save) the page as a PDF, optionally to a file.
|
* Print (export) the page as a PDF.
|
||||||
* @returns Buffer of PDF data.
|
* @returns Buffer of PDF data.
|
||||||
*/
|
*/
|
||||||
printToPDF(
|
printToPDF(
|
||||||
/** Optional print options (based on Chromium's headless standard). */
|
|
||||||
options?: PrintToPDFOptions
|
|
||||||
): Promise<Buffer>,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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). */
|
/** Optional print options (based on Chromium's headless standard). */
|
||||||
options?: PrintToPDFOptions
|
options?: PrintToPDFOptions
|
||||||
): Promise<Buffer>
|
): Promise<Buffer>
|
||||||
|
34
src/api/page.js
Normal file
34
src/api/page.js
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
@ -4,6 +4,7 @@ import { log, logInline } from '../lib/logger.js';
|
|||||||
import IPCApi from '../lib/ipc.js';
|
import IPCApi from '../lib/ipc.js';
|
||||||
import LocalCDP from '../lib/local/cdp.js';
|
import LocalCDP from '../lib/local/cdp.js';
|
||||||
|
|
||||||
|
import PageApi from '../api/page.js';
|
||||||
import IdleApi from '../api/idle.js';
|
import IdleApi from '../api/idle.js';
|
||||||
import ControlsApi from '../api/controls.js';
|
import ControlsApi from '../api/controls.js';
|
||||||
import V8CacheApi from '../api/v8Cache.js';
|
import V8CacheApi from '../api/v8Cache.js';
|
||||||
@ -135,53 +136,6 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN
|
|||||||
};
|
};
|
||||||
|
|
||||||
const Window = {
|
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,
|
ipc: IPC,
|
||||||
|
|
||||||
cdp: {
|
cdp: {
|
||||||
@ -236,6 +190,7 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN
|
|||||||
process.on('SIGTERM', interruptHandler);
|
process.on('SIGTERM', interruptHandler);
|
||||||
// process.on('uncaughtException', interruptHandler);
|
// process.on('uncaughtException', interruptHandler);
|
||||||
|
|
||||||
|
Window.page = await PageApi(Window.cdp, evalInWindow, { pageLoadPromise });
|
||||||
Window.idle = await IdleApi(Window.cdp, { browserType, closeHandlers });
|
Window.idle = await IdleApi(Window.cdp, { browserType, closeHandlers });
|
||||||
Window.controls = await ControlsApi(Window.cdp);
|
Window.controls = await ControlsApi(Window.cdp);
|
||||||
Window.v8Cache = await V8CacheApi(Window.cdp, evalInWindow, { browserType, dataPath });
|
Window.v8Cache = await V8CacheApi(Window.cdp, evalInWindow, { browserType, dataPath });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user