Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Fix and build docs

Ondra Urbanf963eb688f11
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 87c74a060..ced912394 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@
- Added `Apify.utils.requestLikeBrowser` which uses `requestExtended` and disguises as a request made by browser.
- Added `Apify.utils.puppeteer.addInterceptRequestHandler` and `removeInterceptRequestHandler` which
can be used to add multiple request interception handlers to Puppeteer's pages.
+- Added `puppeteerModule` to `LaunchPuppeteerOptions` which enables use of other Puppeteer modules,
+ such as `puppeteer-extra` instead of plain `puppeteer`.
0.13.7 / 2019-04-04
===================
diff --git a/docs/api/KeyValueStore.md b/docs/api/KeyValueStore.md
index 87e65e910..b69991089 100644
--- a/docs/api/KeyValueStore.md
+++ b/docs/api/KeyValueStore.md
@@ -215,7 +215,10 @@ access the value in the remote key value store.
## `keyValueStore.forEachKey(iteratee, [options])` ⇒ <code>Promise</code>
Iterates over key value store keys, yielding each in turn to an `iteratee` function.
-Each invocation of `iteratee` is called with three arguments: `(item, index, info)`.
+Each invocation of `iteratee` is called with three arguments: `(key, index, info)`, where `key`
+is the record key, `index` is a zero-based index of the key in the current iteration
+(regardless of `options.exclusiveStartKey`) and `info` is an object that contains a single property `size`
+indicating size of the record in bytes.
If the `iteratee` function returns a Promise then it is awaited before the next call.
If it throws an error, the iteration is aborted and the `forEachKey` function throws the error.
diff --git a/docs/api/LiveViewServer.md b/docs/api/LiveViewServer.md
new file mode 100644
index 000000000..6afa0de63
--- /dev/null
+++ b/docs/api/LiveViewServer.md
@@ -0,0 +1,115 @@
+---
+id: liveviewserver
+title: LiveViewServer
+---
+<a name="LiveViewServer"></a>
+
+`LiveViewServer` enables serving of browser snapshots via web sockets. It includes its own client
+that provides a simple frontend to viewing the captured snapshots. A snapshot consists of three
+pieces of information, the currently opened URL, the content of the page (HTML) and its screenshot.
+
+`LiveViewServer` is useful when you want to be able to inspect the current browser status on demand.
+When no client is connected, the webserver consumes very low resources so it should have a close
+to zero impact on performance. Only once a client connects the server will start serving snapshots.
+Once no longer needed, it can be disabled again in the client to remove any performance impact.
+
+NOTE: Screenshot taking in browser typically takes around 300ms. So having the `LiveViewServer`
+always serve snapshots will have a significant impact on performance.
+
+When using [`PuppeteerPool`](puppeteerpool), the `LiveViewServer` can be
+easily used just by providing the `useLiveView = true` option to the [`PuppeteerPool`](puppeteerpool).
+It can also be initiated via [`PuppeteerCrawler`](puppeteercrawler) `puppeteerPoolOptions`.
+
+It will take snapshots of the first page of the latest browser. Taking snapshots of only a
+single page improves performance and stability dramatically in high concurrency situations.
+
+When running locally, it is often best to use a headful browser for debugging, since it provides
+a better view into the browser, including DevTools, but `LiveViewServer` works too.
+
+
+* [LiveViewServer](liveviewserver)
+ * [`new LiveViewServer([options])`](#new_LiveViewServer_new)
+ * [`.start()`](#LiveViewServer+start) ⇒ <code>Promise</code>
+ * [`.stop()`](#LiveViewServer+stop) ⇒ <code>Promise</code>
+ * [`.serve(page)`](#LiveViewServer+serve) ⇒ <code>Promise</code>
+ * [`.isRunning()`](#LiveViewServer+isRunning) ⇒ <code>boolean</code>
+ * [`.hasClients()`](#LiveViewServer+hasClients) ⇒ <code>boolean</code>
+
+<a name="new_LiveViewServer_new"></a>
+
+## `new LiveViewServer([options])`
+<table>
+<thead>
+<tr>
+<th>Param</th><th>Type</th><th>Default</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td><code>[options]</code></td><td><code>Object</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>All <code>LiveViewServer</code> parameters are passed
+ via an options object with the following keys:</p>
+</td></tr><tr>
+<td><code>[options.screenshotDirectoryPath]</code></td><td><code>string</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>By default, the screenshots are saved to
+ the <code>live_view</code> directory in the process&#39; working directory. Provide a different
+ absolute path to change the settings.</p>
+</td></tr><tr>
+<td><code>[options.maxScreenshotFiles]</code></td><td><code>number</code></td><td><code>10</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Limits the number of screenshots stored
+ by the server. This is to prevent using up too much disk space.</p>
+</td></tr></tbody>
+</table>
+<a name="LiveViewServer+start"></a>
+
+## `liveViewServer.start()` ⇒ <code>Promise</code>
+Starts the HTTP server with web socket connections enabled.
+Snapshots will not be created until a client has connected.
+
+<a name="LiveViewServer+stop"></a>
+
+## `liveViewServer.stop()` ⇒ <code>Promise</code>
+Prevents the server from receiving more connections. Existing connections
+will not be terminated, but the server will not prevent a process exit.
+
+<a name="LiveViewServer+serve"></a>
+
+## `liveViewServer.serve(page)` ⇒ <code>Promise</code>
+Serves the snapshot to all connected clients.
+
+```json
+{
+ "pageUrl": "https://www.example.com",
+ "htmlContent": "<html><body> ....",
+ "screenshotIndex": 3
+}
+```
+
+Screenshots are not served directly, only their index number
+which is used by client to retrieve the screenshot.
+
+<table>
+<thead>
+<tr>
+<th>Param</th><th>Type</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td><code>page</code></td><td><code>Page</code></td>
+</tr>
+<tr>
+</tr></tbody>
+</table>
+<a name="LiveViewServer+isRunning"></a>
+
+## `liveViewServer.isRunning()` ⇒ <code>boolean</code>
+<a name="LiveViewServer+hasClients"></a>
+
+## `liveViewServer.hasClients()` ⇒ <code>boolean</code>
diff --git a/docs/api/PuppeteerPool.md b/docs/api/PuppeteerPool.md
index 1ceec6261..534a26737 100644
--- a/docs/api/PuppeteerPool.md
+++ b/docs/api/PuppeteerPool.md
@@ -64,6 +64,13 @@ await puppeteerPool.destroy();
<td colspan="3"><p>All <code>PuppeteerPool</code> parameters are passed
via an options object with the following keys:</p>
</td></tr><tr>
+<td><code>[options.useLiveView]</code></td><td><code>boolean</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>Enables the use of a preconfigured <a href="liveviewserver"><code>LiveViewServer</code></a> that serves snapshots
+ just before a page would be recycled by <code>PuppeteerPool</code>. If there are no clients
+ connected, it has close to zero impact on performance.</p>
+</td></tr><tr>
<td><code>[options.maxOpenPagesPerInstance]</code></td><td><code>Number</code></td><td><code>50</code></td>
</tr>
<tr>
@@ -172,14 +179,12 @@ This is unlike `browser.close()` which will forcibly terminate the browser and a
<a name="PuppeteerPool+recyclePage"></a>
## `puppeteerPool.recyclePage(page)` ⇒ <code>Promise</code>
-Recycles the page, which means that it will be enqueued for future reuse
-instead of spawning a new tab. This is done automatically unless the `reusePages`
-option of the `PuppeteerPool` constructor is set to false. You can also use this
-function manually to tell the pool that you no longer need this specific page
-and it can be reused in the future.
-
-Using this function without setting the `reusePages` option to false causes
-this function to trigger a page.close().
+Closes the page, unless the `reuseTabs` option is set to true.
+Then it would only flag the page for a future reuse, without actually closing it.
+
+NOTE: LiveView snapshotting is tied to this function. When `useLiveView` option
+is set to true, a snapshot of the page will be taken just before closing the page
+or flagging it for reuse.
<table>
<thead>
diff --git a/docs/api/puppeteer.md b/docs/api/puppeteer.md
index 23c185f35..5b4b8edbf 100644
--- a/docs/api/puppeteer.md
+++ b/docs/api/puppeteer.md
@@ -24,6 +24,9 @@ await puppeteer.injectJQuery(page);
* [`puppeteer`](#puppeteer) : <code>object</code>
+ * [`.addInterceptRequestHandler`](#puppeteer.addInterceptRequestHandler) ⇒ <code>Promise</code>
+ * [`.removeInterceptRequestHandler`](#puppeteer.removeInterceptRequestHandler) ⇒ <code>Promise</code>
+ * [`.gotoExtended`](#puppeteer.gotoExtended) ⇒ <code>Promise&lt;Response&gt;</code>
* [`.hideWebDriver(page)`](#puppeteer.hideWebDriver) ⇒ <code>Promise</code>
* [`.injectFile(page, filePath, [options])`](#puppeteer.injectFile) ⇒ <code>Promise</code>
* [`.injectJQuery(page)`](#puppeteer.injectJQuery) ⇒ <code>Promise</code>
@@ -32,6 +35,127 @@ await puppeteer.injectJQuery(page);
* [`.cacheResponses(page, cache, responseUrlRules)`](#puppeteer.cacheResponses) ⇒ <code>Promise</code>
* [`.compileScript(scriptString, context)`](#puppeteer.compileScript) ⇒ <code>function</code>
+<a name="puppeteer.addInterceptRequestHandler"></a>
+
+## `puppeteer.addInterceptRequestHandler` ⇒ <code>Promise</code>
+Adds request interception handler in similar to `page.on('request', handler);` but in addition to that
+supports multiple parallel handlers.
+
+All the handlers are executed sequentially in the order as they were added.
+Each of the handlers must call one of `request.continue()`, `request.abort()` and `request.respond()`.
+In addition to that any of the handlers may modify the request object (method, postData, headers)
+by passing its overrides to `request.continue()`.
+If multiple handlers modify same property then the last one wins. Headers are merged separately so you can
+override only a value of specific header.
+
+If one the handlers calls `request.abort()` or `request.respond()` then request is not propagated further
+to any of the remaining handlers.
+
+
+**Example usage:**
+
+```javascript
+// Replace images with placeholder.
+await addInterceptRequestHandler(page, (request) => {
+ if (request.resourceType() === 'image') {
+ return request.respond({
+ statusCode: 200,
+ contentType: 'image/jpeg',
+ body: placeholderImageBuffer,
+ });
+ }
+ return request.continue();
+});
+
+// Abort all the scripts.
+await addInterceptRequestHandler(page, (request) => {
+ if (request.resourceType() === 'script') return request.abort();
+ return request.continue();
+});
+
+// Change requests to post.
+await addInterceptRequestHandler(page, (request) => {
+ return request.continue({
+ method: 'POST',
+ });
+});
+
+await page.goto('http://example.com');
+```
+
+<table>
+<thead>
+<tr>
+<th>Param</th><th>Type</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td><code>page</code></td><td><code>Page</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Puppeteer <a href="https://pptr.dev/#?product=Puppeteer&show=api-class-page" target="_blank"><code>Page</code></a> object.</p>
+</td></tr><tr>
+<td><code>handler</code></td><td><code>function</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Request interception handler.</p>
+</td></tr></tbody>
+</table>
+<a name="puppeteer.removeInterceptRequestHandler"></a>
+
+## `puppeteer.removeInterceptRequestHandler` ⇒ <code>Promise</code>
+Removes request interception handler for given page.
+
+<table>
+<thead>
+<tr>
+<th>Param</th><th>Type</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td><code>page</code></td><td><code>Page</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Puppeteer <a href="https://pptr.dev/#?product=Puppeteer&show=api-class-page" target="_blank"><code>Page</code></a> object.</p>
+</td></tr><tr>
+<td><code>handler</code></td><td><code>function</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Request interception handler.</p>
+</td></tr></tbody>
+</table>
+<a name="puppeteer.gotoExtended"></a>
+
+## `puppeteer.gotoExtended` ⇒ <code>Promise&lt;Response&gt;</code>
+Extended version of Puppeteer's `page.goto()` allowing to perform requests with HTTP method other than GET,
+with custom headers and POST payload. URL, method, headers and payload are taken from
+request parameter that must be an instance of Apify.Request class.
+
+<table>
+<thead>
+<tr>
+<th>Param</th><th>Type</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td><code>page</code></td><td><code>Page</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Puppeteer <a href="https://pptr.dev/#?product=Puppeteer&show=api-class-page" target="_blank"><code>Page</code></a> object.</p>
+</td></tr><tr>
+<td><code>request</code></td><td><code><a href="request">Request</a></code></td>
+</tr>
+<tr>
+<td colspan="3"></td></tr><tr>
+<td><code>gotoOptions</code></td><td><code>Object</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Custom options for <code>page.goto()</code>.</p>
+</td></tr></tbody>
+</table>
<a name="puppeteer.hideWebDriver"></a>
## `puppeteer.hideWebDriver(page)` ⇒ <code>Promise</code>
diff --git a/docs/api/utils.md b/docs/api/utils.md
index fb2080b87..81a07bedc 100644
--- a/docs/api/utils.md
+++ b/docs/api/utils.md
@@ -20,9 +20,11 @@ await Apify.utils.sleep(1500);
* [`utils`](#utils) : <code>object</code>
* [`.enqueueLinks`](#utils.enqueueLinks) ⇒ <code>Promise&lt;Array&lt;QueueOperationInfo&gt;&gt;</code>
+ * [`.requestExtended`](#utils.requestExtended) ⇒ <code>http.IncomingMessage</code>
+ * [`.requestLikeBrowser`](#utils.requestLikeBrowser) ⇒ <code>http.IncomingMessage</code>
+ * [`.sleep`](#utils.sleep) ⇒ <code>Promise</code>
* [`.URL_NO_COMMAS_REGEX`](#utils.URL_NO_COMMAS_REGEX)
* [`.URL_WITH_COMMAS_REGEX`](#utils.URL_WITH_COMMAS_REGEX)
- * [`.exports.sleep`](#utils.exports.sleep) ⇒ <code>Promise</code>
* [`.isDocker()`](#utils.isDocker) ⇒ <code>Promise</code>
* [`.downloadListOfUrls(options)`](#utils.downloadListOfUrls) ⇒ <code>Promise&lt;Array&lt;String&gt;&gt;</code>
* [`.extractUrls(string, [urlRegExp])`](#utils.extractUrls) ⇒ <code>Array&lt;String&gt;</code>
@@ -141,21 +143,193 @@ await Apify.utils.enqueueLinks({
}
</code></pre></td></tr></tbody>
</table>
-<a name="utils.URL_NO_COMMAS_REGEX"></a>
+<a name="utils.requestExtended"></a>
-## `utils.URL_NO_COMMAS_REGEX`
-Default regular expression to match URLs in a string that may be plain text, JSON, CSV or other. It supports common URL characters
-and does not support URLs containing commas or spaces. The URLs also may contain Unicode letters (not symbols).
+## `utils.requestExtended` ⇒ <code>http.IncomingMessage</code>
+Sends a HTTP request and returns the response.
+The function has similar functionality and options as the [request](https://www.npmjs.com/package/request) NPM package,
+but it brings several additional improvements and fixes:
-<a name="utils.URL_WITH_COMMAS_REGEX"></a>
+- It support not only Gzip compression, but also Brotli and Deflate. To activate this feature,
+ simply add `Accept-Encoding: gzip, deflate, br` to `options.headers` (or a combination).
+- Enables abortion of the request based on the response headers, before the data is downloaded.
+ See `options.abortFunction` parameter.
+- SSL connections over proxy do not leak sockets in CLOSE_WAIT state (https://github.com/request/request/issues/2440)
+- Gzip implementation doesn't fail (https://github.com/apifytech/apify-js/issues/266)
+- There is no tunnel-agent AssertionError (https://github.com/request/tunnel-agent/issues/20)
-## `utils.URL_WITH_COMMAS_REGEX`
-Regular expression that, in addition to the default regular expression `URL_NO_COMMAS_REGEX`, supports matching commas in URL path and query.
-Note, however, that this may prevent parsing URLs from comma delimited lists, or the URLs may become malformed.
+NOTE: Most of the options below are simply copied from NPM request. Perhaps we don't need to copy
+them here and can just pass them down. Well, we can decide later.
+
+<table>
+<thead>
+<tr>
+<th>Param</th><th>Default</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td><code>options.url</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>URL of the target endpoint. Supports both HTTP and HTTPS schemes.</p>
+</td></tr><tr>
+<td><code>[options.method]</code></td><td><code>GET</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>HTTP method.</p>
+</td></tr><tr>
+<td><code>[options.headers=]</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>HTTP headers.
+ Note that the function generates several headers itself, unless
+ they are defined in the <code>headers</code> parameter, in which case the function leaves them untouched.
+ For example, even if you define <code>{ &#39;Content-Length&#39;: null }</code>, the function doesn&#39;t define
+ the &#39;Content-Length&#39; header and the request will not contain it (due to the <code>null</code> value).</p>
+</td></tr><tr>
+<td><code>[options.body]</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>HTTP payload for PATCH, POST and PUT requests. Must be a <code>Buffer</code> or <code>String</code>.</p>
+</td></tr><tr>
+<td><code>[options.followRedirect]</code></td><td><code>true</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Follow HTTP 3xx responses as redirects (default: true).
+ OPTIONALLY: This property can also be implemented as function which gets response object as
+ a single argument and should return <code>true</code> if redirects should continue or <code>false</code> otherwise.</p>
+</td></tr><tr>
+<td><code>[options.maxRedirects]</code></td><td><code>10</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>The maximum number of redirects to follow.</p>
+</td></tr><tr>
+<td><code>[options.removeRefererHeader]</code></td><td><code>false</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Removes the referer header when a redirect happens.
+ If <code>true</code>, referer header set in the initial request is preserved during redirect chain.</p>
+</td></tr><tr>
+<td><code>[options.encoding]</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>Encoding to be used on <code>setEncoding</code> of response data.
+ If <code>null</code>, the body is returned as a <code>Buffer</code>.
+ Anything else (including the default value of undefined) will be passed as the encoding parameter to <code>toString()</code>,
+ (meaning this is effectively utf8 by default).
+ (Note: if you expect binary data, you should set encoding: null.)</p>
+</td></tr><tr>
+<td><code>[options.gzip]</code></td><td><code>true</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>If <code>true</code>, the function adds an <code>Accept-Encoding: gzip</code> header to request compressed content encodings from the server
+ (if not already present) and decode supported content encodings in the response.
+ Note that you can achieve the same effect by adding the <code>Accept-Encoding: gzip</code> header directly to <code>options.headers</code>,
+ similarly as <code>deflate</code> as <code>br</code> encodings.</p>
+</td></tr><tr>
+<td><code>[options.json]</code></td><td><code>false</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Sets body to JSON representation of value and adds <code>Content-type: application/json</code> header.
+ Additionally, parses the response body as JSON, i.e. the <code>body</code> property of the returned object
+ is the result of <code>JSON.parse()</code>. Throws an error if response cannot be parsed as JSON.</p>
+</td></tr><tr>
+<td><code>[options.timeout]</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>Integer containing the number of milliseconds to wait for a server to send
+ response headers (and start the response body) before aborting the request.
+ Note that if the underlying TCP connection cannot be established, the OS-wide
+ TCP connection timeout will overrule the timeout option (the default in Linux can be anywhere from 20-120 seconds).</p>
+</td></tr><tr>
+<td><code>[options.proxy]</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>An HTTP proxy to be used. Supports proxy authentication with Basic Auth.</p>
+</td></tr><tr>
+<td><code>[options.strictSSL]</code></td><td><code>true</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>If <code>true</code>, requires SSL/TLS certificates to be valid.</p>
+</td></tr><tr>
+<td><code>[options.abortFunction]</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>A function that determines whether the request should be aborted. It is called when the server
+ responds with the HTTP headers, but before the actual data is downloaded.
+ The function receives a single argument - an instance of Node&#39;s
+ <a href="https://nodejs.org/api/http.html#http_class_http_incomingmessage"><code>http.IncomingMessage</code></a>
+ class and it should return <code>true</code> if request should be aborted, or <code>false</code> otherwise.</p>
+</td></tr><tr>
+<td><code>[options.throwOnHttpError]</code></td><td><code>false</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>If set to true function throws and error on 4XX and 5XX response codes.</p>
+</td></tr></tbody>
+</table>
+<a name="utils.requestLikeBrowser"></a>
+
+## `utils.requestLikeBrowser` ⇒ <code>http.IncomingMessage</code>
+Sends a HTTP request that looks like a request sent by a web browser,
+fully emulating browser's HTTP headers.
+
+This function is useful for web scraping of websites that send the full HTML in the first response.
+Thanks to this function, the target web server has no simple way to find out the request
+hasn't been sent by a full web browser. Using a headless browser for such requests
+is an order of magnitude more resource-intensive than this function.
+
+Currently, the function sends requests the same way as Firefox web browser does.
+In the future, it might add support for other browsers too.
-<a name="utils.exports.sleep"></a>
+Internally, the function uses `requestBetter()` function to perform the request.
+All `options` not recognized by this function are passed to it,
+so see it for more details.
+
+<table>
+<thead>
+<tr>
+<th>Param</th><th>Default</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td><code>options.url</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>URL of the target endpoint. Supports both HTTP and HTTPS schemes.</p>
+</td></tr><tr>
+<td><code>[options.method]</code></td><td><code>GET</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>HTTP method.</p>
+</td></tr><tr>
+<td><code>[options.headers]</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>Additional HTTP headers to add. It&#39;s only recommended to use this option,
+ with headers that are typically added by websites, such as cookies. Overriding
+ default browser headers will remove the masking this function provides.</p>
+</td></tr><tr>
+<td><code>[options.languageCode]</code></td><td><code>en</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Two-letter ISO 639 language code.</p>
+</td></tr><tr>
+<td><code>[options.countryCode]</code></td><td><code>US</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>Two-letter ISO 3166 country code.</p>
+</td></tr><tr>
+<td><code>[options.isMobile]</code></td><td></td>
+</tr>
+<tr>
+<td colspan="3"><p>If <code>true</code>, the function uses User-Agent of a mobile browser.</p>
+</td></tr></tbody>
+</table>
+<a name="utils.sleep"></a>
-## `utils.exports.sleep` ⇒ <code>Promise</code>
+## `utils.sleep` ⇒ <code>Promise</code>
Returns a `Promise` that resolves after a specific period of time. This is useful to implement waiting
in your code, e.g. to prevent overloading of target website or to avoid bot detection.
@@ -184,6 +358,18 @@ await Apify.utils.sleep(1500);
<td colspan="3"><p>Period of time to sleep, in milliseconds. If not a positive number, the returned promise resolves immediately.</p>
</td></tr></tbody>
</table>
+<a name="utils.URL_NO_COMMAS_REGEX"></a>
+
+## `utils.URL_NO_COMMAS_REGEX`
+Default regular expression to match URLs in a string that may be plain text, JSON, CSV or other. It supports common URL characters
+and does not support URLs containing commas or spaces. The URLs also may contain Unicode letters (not symbols).
+
+<a name="utils.URL_WITH_COMMAS_REGEX"></a>
+
+## `utils.URL_WITH_COMMAS_REGEX`
+Regular expression that, in addition to the default regular expression `URL_NO_COMMAS_REGEX`, supports matching commas in URL path and query.
+Note, however, that this may prevent parsing URLs from comma delimited lists, or the URLs may become malformed.
+
<a name="utils.isDocker"></a>
## `utils.isDocker()` ⇒ <code>Promise</code>
diff --git a/docs/typedefs/LaunchPuppeteerOptions.md b/docs/typedefs/LaunchPuppeteerOptions.md
index 344c06e17..80096d6ba 100644
--- a/docs/typedefs/LaunchPuppeteerOptions.md
+++ b/docs/typedefs/LaunchPuppeteerOptions.md
@@ -73,27 +73,10 @@ function and in addition, all the options available below.
The identifier can only contain the following characters: <code>0-9</code>, <code>a-z</code>, <code>A-Z</code>, <code>&quot;.&quot;</code>, <code>&quot;_&quot;</code> and <code>&quot;~&quot;</code>.
Only applied if the <code>useApifyProxy</code> option is <code>true</code>.</p>
</td></tr><tr>
-<td><code>[liveView]</code></td><td><code>Boolean</code></td><td><code>false</code></td>
+<td><code>[puppeteerModule]</code></td><td><code>String</code></td><td></td>
</tr>
<tr>
-<td colspan="3"><p>If set to <code>true</code>, a <code>PuppeteerLiveViewServer</code> will be started to enable
- screenshot and html capturing of visited pages using <code>PuppeteerLiveViewBrowser</code>.</p>
-</td></tr><tr>
-<td><code>[liveViewOptions]</code></td><td><code>Object</code></td><td></td>
-</tr>
-<tr>
-<td colspan="3"><p>Settings for <code>PuppeteerLiveViewBrowser</code> started using <code>launchPuppeteer()</code>.</p>
-</td></tr><tr>
-<td><code>[liveViewOptions.liveViewId]</code></td><td><code>String</code></td><td></td>
-</tr>
-<tr>
-<td colspan="3"><p>Custom ID of a browser instance in live view.</p>
-</td></tr><tr>
-<td><code>[liveViewOptions.screenshotTimeoutMillis]</code></td><td><code>Number</code></td><td><code>3000</code></td>
-</tr>
-<tr>
-<td colspan="3"><p>Time in milliseconds before a screenshot capturing
- will time out and the actor continues with execution.
- Screenshot capturing pauses execution within the given page.</p>
+<td colspan="3"><p>Require path to a module to be used instead of default <code>puppeteer</code>. This enables usage
+ of various Puppeteer wrappers such as <code>puppeteer-extra</code>.</p>
</td></tr></tbody>
</table>
diff --git a/src/live_view/live_view_server.js b/src/live_view/live_view_server.js
index 6f3a344b9..2e8185df2 100644
--- a/src/live_view/live_view_server.js
+++ b/src/live_view/live_view_server.js
@@ -48,7 +48,7 @@ const ensureDir = promisify(fs.ensureDir);
* @param {number} [options.maxScreenshotFiles=10] Limits the number of screenshots stored
* by the server. This is to prevent using up too much disk space.
*/
-export default class LiveViewServer {
+class LiveViewServer {
constructor(options = {}) {
const {
screenshotDirectoryPath = DEFAULT_SCREENSHOT_DIRECTORY_PATH,
@@ -107,7 +107,7 @@ export default class LiveViewServer {
*
* ```json
* {
- * "pageUrl": "https://www.example.com,
+ * "pageUrl": "https://www.example.com",
* "htmlContent": "<html><body> ....",
* "screenshotIndex": 3
* }
@@ -143,8 +143,9 @@ export default class LiveViewServer {
* Returns an absolute path to the screenshot with the given index.
* @param {number} screenshotIndex
* @return {string}
+ * @ignore
*/
- getScreenshotPath(screenshotIndex) {
+ _getScreenshotPath(screenshotIndex) {
return path.join(this.screenshotDirectoryPath, `${screenshotIndex}.png`);
}
@@ -158,7 +159,7 @@ export default class LiveViewServer {
const screenshotIndex = this.lastScreenshotIndex++;
- await writeFile(this.getScreenshotPath(screenshotIndex), screenshot);
+ await writeFile(this._getScreenshotPath(screenshotIndex), screenshot);
if (screenshotIndex > this.maxScreenshotFiles - 1) {
this._deleteScreenshot(screenshotIndex - this.maxScreenshotFiles);
}
@@ -180,7 +181,7 @@ export default class LiveViewServer {
* @ignore
*/
_deleteScreenshot(screenshotIndex) {
- unlink(this.getScreenshotPath(screenshotIndex))
+ unlink(this._getScreenshotPath(screenshotIndex))
.catch(err => log.exception(err, 'Cannot delete live view screenshot.'));
}
@@ -208,7 +209,7 @@ export default class LiveViewServer {
// Serves JPEG with the last screenshot
app.get('/screenshot/:index', (req, res) => {
const screenshotIndex = req.params.index;
- const filePath = this.getScreenshotPath(screenshotIndex);
+ const filePath = this._getScreenshotPath(screenshotIndex);
res.sendFile(filePath);
});
@@ -230,3 +231,5 @@ export default class LiveViewServer {
});
}
}
+
+export default LiveViewServer;
diff --git a/src/puppeteer.js b/src/puppeteer.js
index 5b2a8683d..e231044f9 100644
--- a/src/puppeteer.js
+++ b/src/puppeteer.js
@@ -61,9 +61,9 @@ const LAUNCH_PUPPETEER_DEFAULT_VIEWPORT = {
* will use the same target proxy server (i.e. the same IP address).
* The identifier can only contain the following characters: `0-9`, `a-z`, `A-Z`, `"."`, `"_"` and `"~"`.
* Only applied if the `useApifyProxy` option is `true`.
- * @property ${String} [puppeteerModule]
- * Require path to a module to be used instead of default 'puppeteer'. This enables usage
- * of various Puppeteer wrappers such as 'puppeteer-extra'.
+ * @property {String} [puppeteerModule]
+ * Require path to a module to be used instead of default `puppeteer`. This enables usage
+ * of various Puppeteer wrappers such as `puppeteer-extra`.
*/
/**
diff --git a/src/puppeteer_request_interception.js b/src/puppeteer_request_interception.js
index 17ce44de9..ccf6ada19 100644
--- a/src/puppeteer_request_interception.js
+++ b/src/puppeteer_request_interception.js
@@ -106,6 +106,8 @@ const handleRequest = (request, interceptRequestHandlers) => {
* Puppeteer <a href="https://pptr.dev/#?product=Puppeteer&show=api-class-page" target="_blank"><code>Page</code></a> object.
* @param {Function} handler Request interception handler.
* @return {Promise}
+ * @memberOf puppeteer
+ * @name addInterceptRequestHandler
*/
export const addInterceptRequestHandler = async (page, handler) => {
checkParamOrThrow(page, 'page', 'Object');
@@ -139,6 +141,8 @@ export const addInterceptRequestHandler = async (page, handler) => {
* Puppeteer <a href="https://pptr.dev/#?product=Puppeteer&show=api-class-page" target="_blank"><code>Page</code></a> object.
* @param {Function} handler Request interception handler.
* @return {Promise}
+ * @memberOf puppeteer
+ * @name removeInterceptRequestHandler
*/
export const removeInterceptRequestHandler = async (page, handler) => {
const handlersArray = pageInterceptRequestHandlersMap
diff --git a/src/puppeteer_utils.js b/src/puppeteer_utils.js
index 28043e745..629d27812 100644
--- a/src/puppeteer_utils.js
+++ b/src/puppeteer_utils.js
@@ -128,8 +128,6 @@ const injectFile = async (page, filePath, options = {}) => {
*/
const injectJQuery = (page) => {
checkParamOrThrow(page, 'page', 'Object');
-
- // TODO: For better performance we could use minimized version of the script
return injectFile(page, jqueryPath, { surviveNavigations: true });
};
@@ -156,8 +154,6 @@ const injectJQuery = (page) => {
*/
const injectUnderscore = (page) => {
checkParamOrThrow(page, 'page', 'Object');
-
- // TODO: For better performance we could use minimized version of the script
return injectFile(page, underscorePath, { surviveNavigations: true });
};
@@ -346,17 +342,18 @@ const compileScript = (scriptString, context = Object.create(null)) => {
};
/**
- * Extended version of page.goto() allowing to perform requests with HTTP method other than GET,
+ * Extended version of Puppeteer's `page.goto()` allowing to perform requests with HTTP method other than GET,
* with custom headers and POST payload. URL, method, headers and payload are taken from
* request parameter that must be an instance of Apify.Request class.
*
* @param {Page} page
* Puppeteer <a href="https://pptr.dev/#?product=Puppeteer&show=api-class-page" target="_blank"><code>Page</code></a> object.
- * @param {Request} request {@link Request} object.
- * @param {Object} gotoOptions Custom options for page.goto().
+ * @param {Request} request
+ * @param {Object} gotoOptions Custom options for `page.goto()`.
* @return {Promise<Response>}
*
* @memberOf puppeteer
+ * @name gotoExtended
*/
export const gotoExtended = async (page, request, gotoOptions = {}) => {
checkParamOrThrow(page, 'page', 'Object');
diff --git a/src/utils.js b/src/utils.js
index 568653f4b..15054861f 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -391,6 +391,7 @@ export const isAtHome = () => !!process.env[ENV_VARS.IS_AT_HOME];
* ```
* @param {Number} millis Period of time to sleep, in milliseconds. If not a positive number, the returned promise resolves immediately.
* @memberof utils
+ * @name sleep
* @return {Promise}
*/
export const sleep = (millis) => {
diff --git a/src/utils_request.js b/src/utils_request.js
index 3ebe4f97f..58566b0b7 100644
--- a/src/utils_request.js
+++ b/src/utils_request.js
@@ -289,9 +289,10 @@ function decompress(response) {
* URL of the target endpoint. Supports both HTTP and HTTPS schemes.
* @param [options.method=GET]
* HTTP method.
- * @param [options.headers={}]
- * Additional HTTP headers to add. It's recommended not to use this option,
- * because it can ruin the signature of the web browser. TODO: Maybe let's remove this completely?
+ * @param [options.headers]
+ * Additional HTTP headers to add. It's only recommended to use this option,
+ * with headers that are typically added by websites, such as cookies. Overriding
+ * default browser headers will remove the masking this function provides.
* @param [options.languageCode=en]
* Two-letter ISO 639 language code.
* @param [options.countryCode=US]
diff --git a/website/i18n/en.json b/website/i18n/en.json
index 028a99071..69b0543b4 100644
--- a/website/i18n/en.json
+++ b/website/i18n/en.json
@@ -23,6 +23,9 @@
"api/keyvaluestore": {
"title": "KeyValueStore"
},
+ "api/liveviewserver": {
+ "title": "LiveViewServer"
+ },
"api/log": {
"title": "utils.log"
},
diff --git a/website/sidebars.json b/website/sidebars.json
index 19f31cd14..9d55c0fb0 100644
--- a/website/sidebars.json
+++ b/website/sidebars.json
@@ -16,6 +16,7 @@
"api/cheeriocrawler",
"api/dataset",
"api/keyvaluestore",
+ "api/liveviewserver",
"api/pseudourl",
"api/puppeteercrawler",
"api/puppeteerpool",