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.

Replace includeDefaults with extraUrlPatterns option

Ondra Urban10778a06b3ca
diff --git a/docs/api/puppeteer.md b/docs/api/puppeteer.md
index 1fd017d48..df2ed7841 100644
--- a/docs/api/puppeteer.md
+++ b/docs/api/puppeteer.md
@@ -403,7 +403,7 @@ If the `options.urlPatterns` parameter is not provided,
by default the function blocks URLs that include these patterns:
```json
-[".css", ".jpg", ".jpeg", ".png", ".svg", ".woff", ".pdf", ".zip"]
+[".css", ".jpg", ".jpeg", ".png", ".svg", ".gif", ".woff", ".pdf", ".zip"]
```
The defaults will be concatenated with the patterns you provide in `options.urlPatterns`.
@@ -425,7 +425,7 @@ const page = await browser.newPage();
// Block all requests to URLs that include `adsbygoogle.js` and also all defaults.
await Apify.utils.puppeteer.blockRequests(page, {
- urlPatterns: ['adsbygoogle.js'],
+ extraUrlPatterns: ['adsbygoogle.js'],
});
await page.goto('https://cnn.com');
@@ -456,10 +456,11 @@ await page.goto('https://cnn.com');
and end of the pattern. This limitation is enforced by the DevTools protocol.
<code>.png</code> is the same as <code>*.png*</code>.</p>
</td></tr><tr>
-<td><code>[options.includeDefaults]</code></td><td><code>boolean</code></td>
+<td><code>[options.extraUrlPatterns]</code></td><td><code>boolean</code></td>
</tr>
<tr>
-<td colspan="3"></td></tr></tbody>
+<td colspan="3"><p>If you just want to append to the default blocked patterns, use this property.</p>
+</td></tr></tbody>
</table>
<a name="puppeteer.cacheResponses"></a>
diff --git a/src/puppeteer_utils.js b/src/puppeteer_utils.js
index 617fbb378..a7dbd833d 100644
--- a/src/puppeteer_utils.js
+++ b/src/puppeteer_utils.js
@@ -220,7 +220,7 @@ const enqueueRequestsFromClickableElements = async (page, selector, purls, reque
*
* // Block all requests to URLs that include `adsbygoogle.js` and also all defaults.
* await Apify.utils.puppeteer.blockRequests(page, {
- * urlPatterns: ['adsbygoogle.js'],
+ * extraUrlPatterns: ['adsbygoogle.js'],
* });
*
* await page.goto('https://cnn.com');
@@ -234,7 +234,8 @@ const enqueueRequestsFromClickableElements = async (page, selector, purls, reque
* Only `*` can be used as a wildcard. It is also automatically added to the beginning
* and end of the pattern. This limitation is enforced by the DevTools protocol.
* `.png` is the same as `*.png*`.
- * @param {boolean} [options.includeDefaults]
+ * @param {boolean} [options.extraUrlPatterns]
+ * If you just want to append to the default blocked patterns, use this property.
* @return {Promise}
* @memberOf puppeteer
*/
@@ -242,18 +243,23 @@ const blockRequests = async (page, options = {}) => {
checkParamOrThrow(page, 'page', 'Object');
checkParamOrThrow(options, 'options', 'Object');
+ if (options.includeDefaults === false) {
+ log.deprecated('Apify.utils.puppeteer.blockRequests() includeDefaults option '
+ + 'has been replaced by a more clear extraUrlPatterns option. See docs.');
+ }
+
const {
- urlPatterns = [],
+ urlPatterns = DEFAULT_BLOCK_REQUEST_URL_PATTERNS,
+ extraUrlPatterns = [],
includeDefaults = true,
} = options;
- checkParamOrThrow(page, 'options.page', 'Object');
checkParamOrThrow(urlPatterns, 'options.urlPatterns', '[String]');
- checkParamOrThrow(includeDefaults, 'options.includeDefaults', 'Boolean');
+ checkParamOrThrow(extraUrlPatterns, 'options.extraUrlPatterns', '[String]');
const patternsToBlock = includeDefaults
- ? [...DEFAULT_BLOCK_REQUEST_URL_PATTERNS, ...urlPatterns]
- : urlPatterns;
+ ? [...DEFAULT_BLOCK_REQUEST_URL_PATTERNS, ...urlPatterns, ...extraUrlPatterns]
+ : [...urlPatterns, ...extraUrlPatterns];
await page._client.send('Network.setBlockedURLs', { urls: patternsToBlock }); // eslint-disable-line no-underscore-dangle
};