playwright/src/client/page.ts

677 lines
28 KiB
TypeScript
Raw Normal View History

/**
* Copyright 2017 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Events } from './events';
import { assert } from '../utils/utils';
import { TimeoutSettings } from '../utils/timeoutSettings';
import * as channels from '../protocol/channels';
import { parseError, serializeError } from '../protocol/serializers';
import { Accessibility } from './accessibility';
import { BrowserContext } from './browserContext';
import { ChannelOwner } from './channelOwner';
import { ConsoleMessage } from './consoleMessage';
import { Dialog } from './dialog';
import { Download } from './download';
import { ElementHandle, determineScreenshotType } from './elementHandle';
import { Worker } from './worker';
import { Frame, verifyLoadState, WaitForNavigationOptions } from './frame';
2020-10-19 20:07:33 +03:00
import { Keyboard, Mouse, Touchscreen } from './input';
import { assertMaxArguments, serializeArgument, parseResult, JSHandle } from './jsHandle';
import { Request, Response, Route, RouteHandler, WebSocket, validateHeaders } from './network';
import { FileChooser } from './fileChooser';
import { Buffer } from 'buffer';
import { ChromiumCoverage } from './chromiumCoverage';
import { Waiter } from './waiter';
import * as api from '../../types/types';
import * as structs from '../../types/structs';
import * as fs from 'fs';
2020-09-29 21:51:00 +03:00
import * as path from 'path';
import * as util from 'util';
import { Size, URLMatch, Headers, LifecycleEvent, WaitForEventOptions, SelectOption, SelectOptionOptions, FilePayload, WaitForFunctionOptions } from './types';
import { evaluationScript, urlMatches } from './clientHelper';
import { isString, isRegExp, isObject, mkdirIfNeeded, headersObjectToArray } from '../utils/utils';
import { isSafeCloseError } from '../utils/errors';
import { Video } from './video';
import type { ChromiumBrowserContext } from './chromiumBrowserContext';
2020-09-29 21:51:00 +03:00
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
const mkdirAsync = util.promisify(fs.mkdir);
type PDFOptions = Omit<channels.PagePdfParams, 'width' | 'height' | 'margin'> & {
width?: string | number,
height?: string | number,
margin?: {
top?: string | number,
bottom?: string | number,
left?: string | number,
right?: string | number
},
path?: string,
};
type Listener = (...args: any[]) => void;
export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitializer> implements api.Page {
private _browserContext: BrowserContext;
_ownedContext: BrowserContext | undefined;
private _mainFrame: Frame;
private _frames = new Set<Frame>();
_workers = new Set<Worker>();
private _closed = false;
private _viewportSize: Size | null;
private _routes: { url: URLMatch, handler: RouteHandler }[] = [];
readonly accessibility: Accessibility;
readonly keyboard: Keyboard;
readonly mouse: Mouse;
2020-10-19 20:07:33 +03:00
readonly touchscreen: Touchscreen;
coverage: ChromiumCoverage | null = null;
pdf: (options?: PDFOptions) => Promise<Buffer>;
readonly _bindings = new Map<string, (source: structs.BindingSource, ...args: any[]) => any>();
readonly _timeoutSettings: TimeoutSettings;
_isPageCall = false;
private _video: Video | null = null;
static from(page: channels.PageChannel): Page {
return (page as any)._object;
}
static fromNullable(page: channels.PageChannel | undefined): Page | null {
return page ? Page.from(page) : null;
}
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PageInitializer) {
super(parent, type, guid, initializer);
this.setMaxListeners(0);
this._browserContext = parent as BrowserContext;
this._timeoutSettings = new TimeoutSettings(this._browserContext._timeoutSettings);
this.accessibility = new Accessibility(this._channel);
this.keyboard = new Keyboard(this._channel);
this.mouse = new Mouse(this._channel);
2020-10-19 20:07:33 +03:00
this.touchscreen = new Touchscreen(this._channel);
this._mainFrame = Frame.from(initializer.mainFrame);
this._mainFrame._page = this;
this._frames.add(this._mainFrame);
this._viewportSize = initializer.viewportSize || null;
this._closed = initializer.isClosed;
this._channel.on('bindingCall', ({ binding }) => this._onBinding(BindingCall.from(binding)));
this._channel.on('close', () => this._onClose());
this._channel.on('console', ({ message }) => this.emit(Events.Page.Console, ConsoleMessage.from(message)));
this._channel.on('crash', () => this._onCrash());
this._channel.on('dialog', ({ dialog }) => this.emit(Events.Page.Dialog, Dialog.from(dialog)));
this._channel.on('domcontentloaded', () => this.emit(Events.Page.DOMContentLoaded));
this._channel.on('download', ({ download }) => this.emit(Events.Page.Download, Download.from(download)));
this._channel.on('fileChooser', ({ element, isMultiple }) => this.emit(Events.Page.FileChooser, new FileChooser(this, ElementHandle.from(element), isMultiple)));
this._channel.on('frameAttached', ({ frame }) => this._onFrameAttached(Frame.from(frame)));
this._channel.on('frameDetached', ({ frame }) => this._onFrameDetached(Frame.from(frame)));
this._channel.on('load', () => this.emit(Events.Page.Load));
this._channel.on('pageError', ({ error }) => this.emit(Events.Page.PageError, parseError(error)));
this._channel.on('popup', ({ page }) => this.emit(Events.Page.Popup, Page.from(page)));
this._channel.on('request', ({ request }) => this.emit(Events.Page.Request, Request.from(request)));
this._channel.on('requestFailed', ({ request, failureText, responseEndTiming }) => this._onRequestFailed(Request.from(request), responseEndTiming, failureText));
this._channel.on('requestFinished', ({ request, responseEndTiming }) => this._onRequestFinished(Request.from(request), responseEndTiming));
this._channel.on('response', ({ response }) => this.emit(Events.Page.Response, Response.from(response)));
this._channel.on('route', ({ route, request }) => this._onRoute(Route.from(route), Request.from(request)));
this._channel.on('video', ({ relativePath }) => this.video()!._setRelativePath(relativePath));
this._channel.on('webSocket', ({ webSocket }) => this.emit(Events.Page.WebSocket, WebSocket.from(webSocket)));
this._channel.on('worker', ({ worker }) => this._onWorker(Worker.from(worker)));
if ((this._browserContext as ChromiumBrowserContext)._isChromium) {
this.coverage = new ChromiumCoverage(this._channel);
this.pdf = options => this._pdf(options);
} else {
this.pdf = undefined as any;
}
}
private _onRequestFailed(request: Request, responseEndTiming: number, failureText: string | undefined) {
request._failureText = failureText || null;
if (request._timing)
request._timing.responseEnd = responseEndTiming;
this.emit(Events.Page.RequestFailed, request);
}
private _onRequestFinished(request: Request, responseEndTiming: number) {
if (request._timing)
request._timing.responseEnd = responseEndTiming;
this.emit(Events.Page.RequestFinished, request);
}
private _onFrameAttached(frame: Frame) {
frame._page = this;
this._frames.add(frame);
if (frame._parentFrame)
frame._parentFrame._childFrames.add(frame);
this.emit(Events.Page.FrameAttached, frame);
}
private _onFrameDetached(frame: Frame) {
this._frames.delete(frame);
frame._detached = true;
if (frame._parentFrame)
frame._parentFrame._childFrames.delete(frame);
this.emit(Events.Page.FrameDetached, frame);
}
private _onRoute(route: Route, request: Request) {
for (const {url, handler} of this._routes) {
if (urlMatches(request.url(), url)) {
handler(route, request);
return;
}
}
this._browserContext._onRoute(route, request);
}
async _onBinding(bindingCall: BindingCall) {
const func = this._bindings.get(bindingCall._initializer.name);
if (func) {
bindingCall.call(func);
return;
}
this._browserContext._onBinding(bindingCall);
}
_onWorker(worker: Worker): void {
this._workers.add(worker);
worker._page = this;
this.emit(Events.Page.Worker, worker);
}
_onClose() {
this._closed = true;
this._browserContext._pages.delete(this);
this.emit(Events.Page.Close);
}
private _onCrash() {
this.emit(Events.Page.Crash);
}
context(): BrowserContext {
return this._browserContext;
}
async opener(): Promise<Page | null> {
return Page.fromNullable((await this._channel.opener()).page);
}
mainFrame(): Frame {
return this._mainFrame;
}
frame(frameSelector: string | { name?: string, url?: URLMatch }): Frame | null {
const name = isString(frameSelector) ? frameSelector : frameSelector.name;
const url = isObject(frameSelector) ? frameSelector.url : undefined;
assert(name || url, 'Either name or url matcher should be specified');
return this.frames().find(f => {
if (name)
return f.name() === name;
return urlMatches(f.url(), url);
}) || null;
}
frames(): Frame[] {
return [...this._frames];
}
setDefaultNavigationTimeout(timeout: number) {
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
this._channel.setDefaultNavigationTimeoutNoReply({ timeout });
}
setDefaultTimeout(timeout: number) {
this._timeoutSettings.setDefaultTimeout(timeout);
this._channel.setDefaultTimeoutNoReply({ timeout });
}
video(): Video | null {
if (this._video)
return this._video;
if (!this._browserContext._options.recordVideo)
return null;
this._video = new Video(this);
// In case of persistent profile, we already have it.
if (this._initializer.videoRelativePath)
this._video._setRelativePath(this._initializer.videoRelativePath);
return this._video;
}
private _attributeToPage<T>(func: () => T): T {
try {
this._isPageCall = true;
return func();
} finally {
this._isPageCall = false;
}
}
async $(selector: string): Promise<ElementHandle<SVGElement | HTMLElement> | null> {
return this._attributeToPage(() => this._mainFrame.$(selector));
}
waitForSelector(selector: string, options: channels.FrameWaitForSelectorOptions & { state: 'attached' | 'visible' }): Promise<ElementHandle<SVGElement | HTMLElement>>;
waitForSelector(selector: string, options?: channels.FrameWaitForSelectorOptions): Promise<ElementHandle<SVGElement | HTMLElement> | null>;
async waitForSelector(selector: string, options?: channels.FrameWaitForSelectorOptions): Promise<ElementHandle<SVGElement | HTMLElement> | null> {
return this._attributeToPage(() => this._mainFrame.waitForSelector(selector, options));
}
async dispatchEvent(selector: string, type: string, eventInit?: any, options?: channels.FrameDispatchEventOptions): Promise<void> {
return this._attributeToPage(() => this._mainFrame.dispatchEvent(selector, type, eventInit, options));
}
async evaluateHandle<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg): Promise<structs.SmartHandle<R>> {
assertMaxArguments(arguments.length, 2);
return this._attributeToPage(() => this._mainFrame.evaluateHandle(pageFunction, arg));
}
async $eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element, Arg, R>, arg?: Arg): Promise<R> {
assertMaxArguments(arguments.length, 3);
return this._attributeToPage(() => this._mainFrame.$eval(selector, pageFunction, arg));
}
async $$eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element[], Arg, R>, arg?: Arg): Promise<R> {
assertMaxArguments(arguments.length, 3);
return this._attributeToPage(() => this._mainFrame.$$eval(selector, pageFunction, arg));
}
async $$(selector: string): Promise<ElementHandle<SVGElement | HTMLElement>[]> {
return this._attributeToPage(() => this._mainFrame.$$(selector));
}
async addScriptTag(options: { url?: string; path?: string; content?: string; type?: string; } = {}): Promise<ElementHandle> {
return this._attributeToPage(() => this._mainFrame.addScriptTag(options));
}
async addStyleTag(options: { url?: string; path?: string; content?: string; } = {}): Promise<ElementHandle> {
return this._attributeToPage(() => this._mainFrame.addStyleTag(options));
}
async exposeFunction(name: string, callback: Function) {
return this._wrapApiCall('page.exposeFunction', async () => {
await this._channel.exposeBinding({ name });
const binding = (source: structs.BindingSource, ...args: any[]) => callback(...args);
this._bindings.set(name, binding);
});
}
async exposeBinding(name: string, callback: (source: structs.BindingSource, ...args: any[]) => any, options: { handle?: boolean } = {}) {
return this._wrapApiCall('page.exposeBinding', async () => {
await this._channel.exposeBinding({ name, needsHandle: options.handle });
this._bindings.set(name, callback);
});
}
async setExtraHTTPHeaders(headers: Headers) {
return this._wrapApiCall('page.setExtraHTTPHeaders', async () => {
validateHeaders(headers);
await this._channel.setExtraHTTPHeaders({ headers: headersObjectToArray(headers) });
});
}
url(): string {
return this._attributeToPage(() => this._mainFrame.url());
}
async content(): Promise<string> {
return this._attributeToPage(() => this._mainFrame.content());
}
async setContent(html: string, options?: channels.FrameSetContentOptions): Promise<void> {
return this._attributeToPage(() => this._mainFrame.setContent(html, options));
}
async goto(url: string, options?: channels.FrameGotoOptions): Promise<Response | null> {
return this._attributeToPage(() => this._mainFrame.goto(url, options));
}
async reload(options: channels.PageReloadOptions = {}): Promise<Response | null> {
return this._wrapApiCall('page.reload', async () => {
const waitUntil = verifyLoadState('waitUntil', options.waitUntil === undefined ? 'load' : options.waitUntil);
return Response.fromNullable((await this._channel.reload({ ...options, waitUntil })).response);
});
}
async waitForLoadState(state?: LifecycleEvent, options?: { timeout?: number }): Promise<void> {
return this._attributeToPage(() => this._mainFrame.waitForLoadState(state, options));
}
async waitForNavigation(options?: WaitForNavigationOptions): Promise<Response | null> {
return this._attributeToPage(() => this._mainFrame.waitForNavigation(options));
}
async waitForRequest(urlOrPredicate: string | RegExp | ((r: Request) => boolean), options: { timeout?: number } = {}): Promise<Request> {
const predicate = (request: Request) => {
if (isString(urlOrPredicate) || isRegExp(urlOrPredicate))
return urlMatches(request.url(), urlOrPredicate);
return urlOrPredicate(request);
};
return this.waitForEvent(Events.Page.Request, { predicate, timeout: options.timeout });
}
async waitForResponse(urlOrPredicate: string | RegExp | ((r: Response) => boolean), options: { timeout?: number } = {}): Promise<Response> {
const predicate = (response: Response) => {
if (isString(urlOrPredicate) || isRegExp(urlOrPredicate))
return urlMatches(response.url(), urlOrPredicate);
return urlOrPredicate(response);
};
return this.waitForEvent(Events.Page.Response, { predicate, timeout: options.timeout });
}
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
const waiter = new Waiter();
2020-07-22 09:48:21 +03:00
waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`);
if (event !== Events.Page.Crash)
waiter.rejectOnEvent(this, Events.Page.Crash, new Error('Page crashed'));
if (event !== Events.Page.Close)
waiter.rejectOnEvent(this, Events.Page.Close, new Error('Page closed'));
const result = await waiter.waitForEvent(this, event, predicate as any);
waiter.dispose();
return result;
}
async goBack(options: channels.PageGoBackOptions = {}): Promise<Response | null> {
return this._wrapApiCall('page.goBack', async () => {
const waitUntil = verifyLoadState('waitUntil', options.waitUntil === undefined ? 'load' : options.waitUntil);
return Response.fromNullable((await this._channel.goBack({ ...options, waitUntil })).response);
});
}
async goForward(options: channels.PageGoForwardOptions = {}): Promise<Response | null> {
return this._wrapApiCall('page.goForward', async () => {
const waitUntil = verifyLoadState('waitUntil', options.waitUntil === undefined ? 'load' : options.waitUntil);
return Response.fromNullable((await this._channel.goForward({ ...options, waitUntil })).response);
});
}
async emulateMedia(params: { media?: 'screen' | 'print' | null, colorScheme?: 'dark' | 'light' | 'no-preference' | null } = {}) {
return this._wrapApiCall('page.emulateMedia', async () => {
await this._channel.emulateMedia({
media: params.media === null ? 'null' : params.media,
colorScheme: params.colorScheme === null ? 'null' : params.colorScheme,
});
});
}
async setViewportSize(viewportSize: Size) {
return this._wrapApiCall('page.setViewportSize', async () => {
this._viewportSize = viewportSize;
await this._channel.setViewportSize({ viewportSize });
});
}
viewportSize(): Size | null {
return this._viewportSize;
}
async evaluate<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg): Promise<R> {
assertMaxArguments(arguments.length, 2);
return this._attributeToPage(() => this._mainFrame.evaluate(pageFunction, arg));
}
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any) {
return this._wrapApiCall('page.addInitScript', async () => {
const source = await evaluationScript(script, arg);
await this._channel.addInitScript({ source });
});
}
async route(url: URLMatch, handler: RouteHandler): Promise<void> {
return this._wrapApiCall('page.route', async () => {
this._routes.push({ url, handler });
if (this._routes.length === 1)
await this._channel.setNetworkInterceptionEnabled({ enabled: true });
});
}
async unroute(url: URLMatch, handler?: RouteHandler): Promise<void> {
return this._wrapApiCall('page.unroute', async () => {
this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler));
if (this._routes.length === 0)
await this._channel.setNetworkInterceptionEnabled({ enabled: false });
});
}
async screenshot(options: channels.PageScreenshotOptions & { path?: string } = {}): Promise<Buffer> {
return this._wrapApiCall('page.screenshot', async () => {
const copy = { ...options };
if (!copy.type)
copy.type = determineScreenshotType(options);
const result = await this._channel.screenshot(copy);
const buffer = Buffer.from(result.binary, 'base64');
if (options.path) {
await mkdirIfNeeded(options.path);
await fsWriteFileAsync(options.path, buffer);
}
return buffer;
});
}
async title(): Promise<string> {
return this._attributeToPage(() => this._mainFrame.title());
}
async bringToFront(): Promise<void> {
return this._wrapApiCall('page.bringToFront', async () => {
await this._channel.bringToFront();
});
}
async close(options: { runBeforeUnload?: boolean } = {runBeforeUnload: undefined}) {
try {
await this._wrapApiCall('page.close', async () => {
await this._channel.close(options);
if (this._ownedContext)
await this._ownedContext.close();
});
} catch (e) {
if (isSafeCloseError(e))
return;
throw e;
}
}
isClosed(): boolean {
return this._closed;
}
async click(selector: string, options?: channels.FrameClickOptions) {
return this._attributeToPage(() => this._mainFrame.click(selector, options));
}
async dblclick(selector: string, options?: channels.FrameDblclickOptions) {
return this._attributeToPage(() => this._mainFrame.dblclick(selector, options));
}
2020-10-19 20:07:33 +03:00
async tap(selector: string, options?: channels.FrameTapOptions) {
return this._attributeToPage(() => this._mainFrame.tap(selector, options));
}
async fill(selector: string, value: string, options?: channels.FrameFillOptions) {
return this._attributeToPage(() => this._mainFrame.fill(selector, value, options));
}
async focus(selector: string, options?: channels.FrameFocusOptions) {
return this._attributeToPage(() => this._mainFrame.focus(selector, options));
}
async textContent(selector: string, options?: channels.FrameTextContentOptions): Promise<null|string> {
return this._attributeToPage(() => this._mainFrame.textContent(selector, options));
}
async innerText(selector: string, options?: channels.FrameInnerTextOptions): Promise<string> {
return this._attributeToPage(() => this._mainFrame.innerText(selector, options));
}
async innerHTML(selector: string, options?: channels.FrameInnerHTMLOptions): Promise<string> {
return this._attributeToPage(() => this._mainFrame.innerHTML(selector, options));
}
async getAttribute(selector: string, name: string, options?: channels.FrameGetAttributeOptions): Promise<string | null> {
return this._attributeToPage(() => this._mainFrame.getAttribute(selector, name, options));
}
async isChecked(selector: string, options?: channels.FrameIsCheckedOptions): Promise<boolean> {
return this._attributeToPage(() => this._mainFrame.isChecked(selector, options));
}
async isDisabled(selector: string, options?: channels.FrameIsDisabledOptions): Promise<boolean> {
return this._attributeToPage(() => this._mainFrame.isDisabled(selector, options));
}
async isEditable(selector: string, options?: channels.FrameIsEditableOptions): Promise<boolean> {
return this._attributeToPage(() => this._mainFrame.isEditable(selector, options));
}
async isEnabled(selector: string, options?: channels.FrameIsEnabledOptions): Promise<boolean> {
return this._attributeToPage(() => this._mainFrame.isEnabled(selector, options));
}
async isHidden(selector: string, options?: channels.FrameIsHiddenOptions): Promise<boolean> {
return this._attributeToPage(() => this._mainFrame.isHidden(selector, options));
}
async isVisible(selector: string, options?: channels.FrameIsVisibleOptions): Promise<boolean> {
return this._attributeToPage(() => this._mainFrame.isVisible(selector, options));
}
async hover(selector: string, options?: channels.FrameHoverOptions) {
return this._attributeToPage(() => this._mainFrame.hover(selector, options));
}
async selectOption(selector: string, values: string | api.ElementHandle | SelectOption | string[] | api.ElementHandle[] | SelectOption[] | null, options?: SelectOptionOptions): Promise<string[]> {
return this._attributeToPage(() => this._mainFrame.selectOption(selector, values, options));
}
async setInputFiles(selector: string, files: string | FilePayload | string[] | FilePayload[], options?: channels.FrameSetInputFilesOptions): Promise<void> {
return this._attributeToPage(() => this._mainFrame.setInputFiles(selector, files, options));
}
async type(selector: string, text: string, options?: channels.FrameTypeOptions) {
return this._attributeToPage(() => this._mainFrame.type(selector, text, options));
}
async press(selector: string, key: string, options?: channels.FramePressOptions) {
return this._attributeToPage(() => this._mainFrame.press(selector, key, options));
}
async check(selector: string, options?: channels.FrameCheckOptions) {
return this._attributeToPage(() => this._mainFrame.check(selector, options));
}
async uncheck(selector: string, options?: channels.FrameUncheckOptions) {
return this._attributeToPage(() => this._mainFrame.uncheck(selector, options));
}
async waitForTimeout(timeout: number) {
await this._mainFrame.waitForTimeout(timeout);
}
async waitForFunction<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg, options?: WaitForFunctionOptions): Promise<structs.SmartHandle<R>> {
return this._attributeToPage(() => this._mainFrame.waitForFunction(pageFunction, arg, options));
}
workers(): Worker[] {
return [...this._workers];
}
on(event: string | symbol, listener: Listener): this {
if (event === Events.Page.FileChooser && !this.listenerCount(event))
this._channel.setFileChooserInterceptedNoReply({ intercepted: true });
super.on(event, listener);
return this;
}
addListener(event: string | symbol, listener: Listener): this {
if (event === Events.Page.FileChooser && !this.listenerCount(event))
this._channel.setFileChooserInterceptedNoReply({ intercepted: true });
super.addListener(event, listener);
return this;
}
off(event: string | symbol, listener: Listener): this {
super.off(event, listener);
if (event === Events.Page.FileChooser && !this.listenerCount(event))
this._channel.setFileChooserInterceptedNoReply({ intercepted: false });
return this;
}
removeListener(event: string | symbol, listener: Listener): this {
super.removeListener(event, listener);
if (event === Events.Page.FileChooser && !this.listenerCount(event))
this._channel.setFileChooserInterceptedNoReply({ intercepted: false });
return this;
}
async _pdf(options: PDFOptions = {}): Promise<Buffer> {
const transportOptions: channels.PagePdfParams = { ...options } as channels.PagePdfParams;
if (transportOptions.margin)
transportOptions.margin = { ...transportOptions.margin };
if (typeof options.width === 'number')
transportOptions.width = options.width + 'px';
if (typeof options.height === 'number')
transportOptions.height = options.height + 'px';
for (const margin of ['top', 'right', 'bottom', 'left']) {
const index = margin as 'top' | 'right' | 'bottom' | 'left';
if (options.margin && typeof options.margin[index] === 'number')
transportOptions.margin![index] = transportOptions.margin![index] + 'px';
}
const result = await this._channel.pdf(transportOptions);
const buffer = Buffer.from(result.pdf, 'base64');
2020-09-29 21:51:00 +03:00
if (options.path) {
await mkdirAsync(path.dirname(options.path), { recursive: true });
await fsWriteFileAsync(options.path, buffer);
2020-09-29 21:51:00 +03:00
}
return buffer;
}
}
export class BindingCall extends ChannelOwner<channels.BindingCallChannel, channels.BindingCallInitializer> {
static from(channel: channels.BindingCallChannel): BindingCall {
return (channel as any)._object;
}
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.BindingCallInitializer) {
super(parent, type, guid, initializer);
}
async call(func: (source: structs.BindingSource, ...args: any[]) => any) {
try {
const frame = Frame.from(this._initializer.frame);
const source = {
context: frame._page!.context(),
page: frame._page!,
frame
};
let result: any;
if (this._initializer.handle)
result = await func(source, JSHandle.from(this._initializer.handle));
else
result = await func(source, ...this._initializer.args!.map(parseResult));
this._channel.resolve({ result: serializeArgument(result) }).catch(() => {});
} catch (e) {
this._channel.reject({ error: serializeError(e) }).catch(() => {});
}
}
}