/** * Copyright 2023 Google Inc. All rights reserved. * * 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 {ElementHandle} from '../../api/ElementHandle.js'; import {Frame as BaseFrame} from '../../api/Frame.js'; import {PuppeteerLifeCycleEvent} from '../LifecycleWatcher.js'; import {EvaluateFunc, EvaluateFuncWith, HandleFor, NodeFor} from '../types.js'; import {withSourcePuppeteerURLIfNone} from '../util.js'; import {BrowsingContext} from './BrowsingContext.js'; import {HTTPResponse} from './HTTPResponse.js'; import {Page} from './Page.js'; import { MAIN_SANDBOX, PUPPETEER_SANDBOX, SandboxChart, Sandbox, } from './Sandbox.js'; /** * Puppeteer's Frame class could be viewed as a BiDi BrowsingContext implementation * @internal */ export class Frame extends BaseFrame { #page: Page; #context: BrowsingContext; sandboxes: SandboxChart; override _id: string; constructor(page: Page, context: BrowsingContext, parentId?: string | null) { super(); this.#page = page; this.#context = context; this._id = this.#context.id; this._parentId = parentId ?? undefined; this.sandboxes = { [MAIN_SANDBOX]: new Sandbox(context), [PUPPETEER_SANDBOX]: new Sandbox(context), }; } override page(): Page { return this.#page; } override name(): string { return this._name || ''; } override url(): string { return this.#context.url; } override parentFrame(): Frame | null { return this.#page.frame(this._parentId ?? ''); } override childFrames(): Frame[] { return this.#page.childFrames(this.#context.id); } override async evaluateHandle< Params extends unknown[], Func extends EvaluateFunc = EvaluateFunc >( pageFunction: Func | string, ...args: Params ): Promise>>> { return this.#context.evaluateHandle(pageFunction, ...args); } override async evaluate< Params extends unknown[], Func extends EvaluateFunc = EvaluateFunc >( pageFunction: Func | string, ...args: Params ): Promise>> { return this.#context.evaluate(pageFunction, ...args); } override async goto( url: string, options?: | { referer?: string | undefined; referrerPolicy?: string | undefined; timeout?: number | undefined; waitUntil?: | PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[] | undefined; } | undefined ): Promise { const navigationId = await this.#context.goto(url, options); return this.#page.getNavigationResponse(navigationId); } override setContent( html: string, options: { timeout?: number; waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[]; } ): Promise { return this.#context.setContent(html, options); } override content(): Promise { return this.#context.content(); } override title(): Promise { return this.#context.title(); } context(): BrowsingContext { return this.#context; } override $( selector: Selector ): Promise> | null> { return this.sandboxes[MAIN_SANDBOX].$(selector); } override $$( selector: Selector ): Promise>>> { return this.sandboxes[MAIN_SANDBOX].$$(selector); } override $eval< Selector extends string, Params extends unknown[], Func extends EvaluateFuncWith, Params> = EvaluateFuncWith< NodeFor, Params > >( selector: Selector, pageFunction: string | Func, ...args: Params ): Promise>> { pageFunction = withSourcePuppeteerURLIfNone(this.$eval.name, pageFunction); return this.sandboxes[MAIN_SANDBOX].$eval(selector, pageFunction, ...args); } override $$eval< Selector extends string, Params extends unknown[], Func extends EvaluateFuncWith< Array>, Params > = EvaluateFuncWith>, Params> >( selector: Selector, pageFunction: string | Func, ...args: Params ): Promise>> { pageFunction = withSourcePuppeteerURLIfNone(this.$$eval.name, pageFunction); return this.sandboxes[MAIN_SANDBOX].$$eval(selector, pageFunction, ...args); } override $x(expression: string): Promise>> { return this.sandboxes[MAIN_SANDBOX].$x(expression); } dispose(): void { this.#context.dispose(); } }