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

Implement evaluate() (squashed commit) #185

Merged
merged 1 commit into from Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -386,6 +386,16 @@ Browsershot also can get the body of an html page after JavaScript has been exec
Browsershot::url('https://example.com')->bodyHtml(); // returns the html of the body
```

### Evaluate

Browsershot can get the evaluation of an html page:

```php
Browsershot::url('https://example.com')
->deviceScaleFactor(2)
->evaluate("window.devicePixelRatio"); // returns 2
```

### Misc

#### Setting an arbitrary option
Expand Down
18 changes: 16 additions & 2 deletions bin/browser.js
Expand Up @@ -2,6 +2,20 @@ const puppeteer = require('puppeteer');

const request = JSON.parse(process.argv[2]);

const getOutput = async (page, request) => {
let output;

if (request.action == 'evaluate') {
output = await page.evaluate(request.options.pageFunction);

return output;
}

output = await page[request.action](request.options);

return output.toString('base64');
};

const callChrome = async () => {
let browser;
let page;
Expand Down Expand Up @@ -73,10 +87,10 @@ const callChrome = async () => {
request.options.clip = await element.boundingBox();
}

output = await page[request.action](request.options);
output = await getOutput(page, request);

if (!request.options.path) {
console.log(output.toString('base64'));
console.log(output);
}

await browser.close();
Expand Down
20 changes: 19 additions & 1 deletion src/Browsershot.php
Expand Up @@ -414,6 +414,13 @@ public function savePdf(string $targetPath)
}
}

public function evaluate(string $pageFunction): string
{
$command = $this->createEvaluateCommand($pageFunction);

return $this->callBrowser($command);
}

public function applyManipulations(string $imagePath)
{
Image::load($imagePath)
Expand Down Expand Up @@ -471,6 +478,17 @@ public function createPdfCommand($targetPath = null): array
return $command;
}

public function createEvaluateCommand(string $pageFunction): array
{
$url = $this->html ? $this->createTemporaryHtmlFile() : $this->url;

$options = [
'pageFunction' => $pageFunction,
];

return $this->createCommand($url, 'evaluate', $options);
}

protected function getOptionArgs(): array
{
$args = [];
Expand Down Expand Up @@ -537,7 +555,7 @@ protected function callBrowser(array $command)
$process->run();

if ($process->isSuccessful()) {
return $process->getOutput();
return rtrim($process->getOutput());
}

if ($process->getExitCode() === 2) {
Expand Down
9 changes: 9 additions & 0 deletions tests/BrowsershotTest.php
Expand Up @@ -828,4 +828,13 @@ public function it_can_set_type_of_screenshot()

$this->assertMimeType('image/jpeg', $targetPath);
}

/** @test */
public function it_can_evaluate_page()
{
$result = Browsershot::url('https://example.com')
->evaluate('1 + 1');

$this->assertEquals('2', $result);
}
}