From a6e7b3270859408484aae17b3ca84c23968546c7 Mon Sep 17 00:00:00 2001 From: Soufiane Ghzal Date: Sat, 13 Oct 2018 15:23:09 +0200 Subject: [PATCH] added demo for persisting browser #42 --- CHANGELOG.md | 2 +- examples/persistent-browser.php | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 examples/persistent-browser.php diff --git a/CHANGELOG.md b/CHANGELOG.md index dbe90cdf..0db0f89f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ ## 0.3.0 -> *20xx-xx-xx* (not released) +> *2018-08-13* > Make a crawl instance sharable among multiple scripts diff --git a/examples/persistent-browser.php b/examples/persistent-browser.php new file mode 100644 index 00000000..07deffc9 --- /dev/null +++ b/examples/persistent-browser.php @@ -0,0 +1,50 @@ + 'php://stdout' + ]); + } catch (\HeadlessChromium\Exception\BrowserConnectionFailed $e) { + // The browser was probably closed + // Keep $browser null and start it again bellow + } +} + +// if $browser is null then create a new chrome instance +if (!$browser) { + $factory = new \HeadlessChromium\BrowserFactory(); + $browser = $factory->createBrowser([ + 'headless' => false, + 'keepAlive' => true + ]); + + // save the uri to be able to connect again to browser + file_put_contents($socketFile, $browser->getSocketUri()); +} + +// do something with the browser +$page = $browser->createPage(); + +$page->navigate('http://example.com')->waitForNavigation(); \ No newline at end of file