Skip to content

Commit

Permalink
added method setContentUrl to set url when using html method
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorfigueiraacin authored and freekmurze committed May 19, 2022
1 parent 9c4f283 commit 038cfec
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
24 changes: 23 additions & 1 deletion bin/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ const callChrome = async pup => {
});
}

const contentUrl = request.options.contentUrl;
const parsedContentUrl = contentUrl ? contentUrl.replace(/\/$/, "") : undefined;
let pageContent;

if (contentUrl) {
pageContent = fs.readFileSync(request.url.replace('file://', ''));
request.url = contentUrl;
}

page.on('request', interceptedRequest => {
var headers = interceptedRequest.headers();

Expand Down Expand Up @@ -140,7 +149,20 @@ const callChrome = async pup => {
}
}

interceptedRequest.continue({headers});
if (pageContent) {
const interceptedUrl = interceptedRequest.url().replace(/\/$/, "");

// if content url matches the intercepted request url, will return the content fetched from the local file system
if (interceptedUrl === parsedContentUrl) {
interceptedRequest.respond({
headers,
body: pageContent,
});
return;
}
}

interceptedRequest.continue({ headers });
});

if (request.options && request.options.dismissDialogs) {
Expand Down
12 changes: 12 additions & 0 deletions docs/miscellaneous-options/using-url-for-html-content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Using url for html content
weight: 21
---

Using the method *setContentUrl* you can set the base url of the request when using the *html* method. Sometimes you may have relative paths in your code. When passing a html page to puppeteer, you don't have a base url set. So any relative path present in your html content will not fetch correctly.

```php
Browsershot::html('<html>... relative paths to fetch ...</html>')
->setContentUrl('https://www.example.com')
...
```
5 changes: 5 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,11 @@ public function setEnvironmentOptions(array $options = []): self
return $this->setOption('env', $options);
}

public function setContentUrl(string $contentUrl): self
{
return $this->html ? $this->setOption('contentUrl', $contentUrl) : $this;
}

protected function getOptionArgs(): array
{
$args = $this->chromiumArguments;
Expand Down
27 changes: 27 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1481,3 +1481,30 @@

$this->assertFileDoesNotExist($targetPath);
});

it('will allow passing a content url', function () {
$instance = Browsershot::html('<h1>Hello world!!</h1>')
->setContentUrl('https://example.com');

$response = $instance->createScreenshotCommand('screenshot.png');

$responseUrl = $response['url'];
unset($response['url']);

$this->assertEquals([
'action' => 'screenshot',
'options' => [
'path' => 'screenshot.png',
'viewport' => [
'width' => 800,
'height' => 600,
],
'args' => [],
'type' => 'png',
'displayHeaderFooter' => false,
'contentUrl' => 'https://example.com',
],
], $response);

$this->assertStringContainsString("file://", $responseUrl);
});

0 comments on commit 038cfec

Please sign in to comment.