Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting the initial page would be nice #631

Open
divinity76 opened this issue Jun 19, 2024 · 2 comments
Open

getting the initial page would be nice #631

divinity76 opened this issue Jun 19, 2024 · 2 comments

Comments

@divinity76
Copy link
Contributor

divinity76 commented Jun 19, 2024

When a browser is created, a default page is also created, and the first call to browser->createPage() creates a second page.
Single-page scripts (which I assume is the majority of scripts) could benefit from accessing the initial page, it would be nice if we could get a

$factory = new \HeadlessChromium\BrowserFactory("chromium-browser");
$browser = $factory->createBrowser();
$page = $browser->getInitialPage();

which would presumably be somewhat faster and use less resources than $page = $browser->createPage();

just a "nice to have" feature, speeding up single-page scripts, but not important.

Also I checked if this initial page is available from $browser->getPages(); - it isn't. (have not looked into why.)

@ethaniel
Copy link

I've noticed that in headless mode, $browser->getPages() returns an empty array, so you do need to create a page.
When headless is off, $browser->getPages() returns the initial page.

@ethaniel
Copy link

ethaniel commented Jun 20, 2024

Simple workaround:

$pages = $browser->getPages();
if (isset($pages[0])) {
	$page = $pages[0];
} else {
	$page = $browser->createPage();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants