diff --git a/README.md b/README.md index eef75d9..4bfceb8 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,7 @@ Here are the options available for the browser factory: | `connectionDelay` | `0` | Delay to apply between each operation for debugging purposes | | `customFlags` | none | An array of flags to pass to the command line. Eg: `['--option1', '--option2=someValue']` | | `debugLogger` | `null` | A string (e.g "php://stdout"), or resource, or PSR-3 logger instance to print debug messages | +| `disableJavascript` | `false` | Disable JavaScript execution on every page created by the browser | | `disableNotifications` | `false` | Disable browser notifications | | `enableImages` | `true` | Toggles loading of images | | `envVariables` | none | An array of environment variables to pass to the process (example DISPLAY variable) | diff --git a/src/Browser.php b/src/Browser.php index d2f579d..48e0c5a 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -43,6 +43,13 @@ class Browser */ protected $pagePreScript; + /** + * Whether JavaScript execution must be disabled on every new page. + * + * @var bool + */ + protected $pageScriptExecutionDisabled = false; + public function __construct(Connection $connection) { $this->connection = $connection; @@ -103,6 +110,14 @@ public function setPagePreScript(?string $script = null): void $this->pagePreScript = $script; } + /** + * Disable or enable JavaScript execution for every new page created by this browser. + */ + public function setPageScriptExecutionDisabled(bool $disabled = true): void + { + $this->pageScriptExecutionDisabled = $disabled; + } + /** * Closes the browser. * @@ -258,6 +273,11 @@ public function getPage($targetId) $page->addPreScript($this->pagePreScript); } + // disable javascript execution if requested at the browser level + if ($this->pageScriptExecutionDisabled) { + $page->setScriptExecution(false)->await(); + } + $this->pages[$targetId] = $page; return $page; diff --git a/src/Browser/BrowserProcess.php b/src/Browser/BrowserProcess.php index 17c4a1d..fa7de8e 100644 --- a/src/Browser/BrowserProcess.php +++ b/src/Browser/BrowserProcess.php @@ -159,6 +159,11 @@ public function start($binary, $options): void // create browser instance $this->browser = new ProcessAwareBrowser($connection, $this); + + // disable javascript execution for new pages if requested + if (\array_key_exists('disableJavascript', $options) && (true === $options['disableJavascript'])) { + $this->browser->setPageScriptExecutionDisabled(true); + } } /** diff --git a/src/BrowserFactory.php b/src/BrowserFactory.php index f9f5461..0588bf2 100644 --- a/src/BrowserFactory.php +++ b/src/BrowserFactory.php @@ -31,6 +31,7 @@ class BrowserFactory * - connectionDelay: Delay to apply between each operation for debugging purposes (default: none) * - customFlags: An array of flags to pass to the command line. * - debugLogger: A string (e.g "php://stdout"), or resource, or PSR-3 logger instance to print debug messages (default: none) + * - disableJavascript: Disable JavaScript execution on every page created by the browser (default: false) * - disableNotifications: Disable browser notifications (default: false) * - enableImages: Toggles loading of images (default: true) * - envVariables: An array of environment variables to pass to the process (example DISPLAY variable) diff --git a/tests/BrowserFactoryTest.php b/tests/BrowserFactoryTest.php index 4c599dc..0a376d7 100644 --- a/tests/BrowserFactoryTest.php +++ b/tests/BrowserFactoryTest.php @@ -111,6 +111,38 @@ public function testOptions(): void self::assertSame([], $factory->getOptions()); } + public function testDisableJavascriptOption(): void + { + $factory = new BrowserFactory(); + + $browser = $factory->createBrowser([ + 'disableJavascript' => true, + ]); + + $page = $browser->createPage(); + $page->navigate(self::sitePath('javascript.html'))->waitForNavigation(); + + self::assertSame( + 'javascript disabled', + $page->evaluate('document.body.innerText')->getReturnValue() + ); + } + + public function testDisableJavascriptOptionDefault(): void + { + $factory = new BrowserFactory(); + + $browser = $factory->createBrowser(); + + $page = $browser->createPage(); + $page->navigate(self::sitePath('javascript.html'))->waitForNavigation(); + + self::assertSame( + 'javascript enabled', + $page->evaluate('document.body.innerText')->getReturnValue() + ); + } + public function testConnectToBrowser(): void { // create a browser