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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
Improved documentation.
Petr Patekc30e32736e15
diff --git a/docs/examples/PuppeteerCrawler.md b/docs/examples/PuppeteerCrawler.mdindex 23b40fd81..f2b348500 100644--- a/docs/examples/PuppeteerCrawler.md+++ b/docs/examples/PuppeteerCrawler.md@@ -27,8 +27,10 @@ Apify.main(async () => {requestQueue,// Here you can set options that are passed to the Apify.launchPuppeteer() function.- // For example, you can set "slowMo" to slow down Puppeteer operations to simplify debugging- launchPuppeteerOptions: { slowMo: 500 },+ launchPuppeteerOptions: {+ // For example, by adding "slowMo" you'll slow down Puppeteer operations to simplify debugging+ // slowMo: 500,+ },// Stop crawling after several pagesmaxRequestsPerCrawl: 10,diff --git a/docs/typedefs/LaunchPuppeteerOptions.md b/docs/typedefs/LaunchPuppeteerOptions.mdindex 80096d6ba..dc6b92a42 100644--- a/docs/typedefs/LaunchPuppeteerOptions.md+++ b/docs/typedefs/LaunchPuppeteerOptions.md@@ -78,5 +78,11 @@ function and in addition, all the options available below.<tr><td colspan="3"><p>Require path to a module to be used instead of default <code>puppeteer</code>. This enables usageof various Puppeteer wrappers such as <code>puppeteer-extra</code>.</p>+</td></tr><tr>+<td><code>[stealthOptions]</code></td><td><code><a href="../typedefs/stealthoptions">StealthOptions</a></code></td><td></td>+</tr>+<tr>+<td colspan="3"><p>Using this configuration, you can make a headless chrome nearly undetectable.+ It is recommended to use it together with the <code>useChrome</code> set to <code>true</code>.</p></td></tr></tbody></table>diff --git a/docs/typedefs/stealthOptions.md b/docs/typedefs/stealthOptions.mdnew file mode 100644index 000000000..3a0e6480e--- /dev/null+++ b/docs/typedefs/stealthOptions.md@@ -0,0 +1,68 @@+---+id: stealthoptions+title: StealthOptions+---+<a name="StealthOptions"></a>++Configuration of stealth tricks for a proper hiding effect all of them should be set to true++**Properties**+<table>+<thead>+<tr>+<th>Param</th><th>Type</th><th>Default</th>+</tr>+</thead>+<tbody>+<tr>+<td><code>[addPlugins]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>if plugins should be added to the navigator</p>+</td></tr><tr>+<td><code>[emulateWindowFrame]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>Emulates window Iframe</p>+</td></tr><tr>+<td><code>[emulateWebGL]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>Emulates graphic card</p>+</td></tr><tr>+<td><code>[emulateConsoleDebug]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>Emulates console.debug to return null</p>+</td></tr><tr>+<td><code>[addLanguage]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>Adds languages to the navigator</p>+</td></tr><tr>+<td><code>[hideWebDriver]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>Hides the webdriver by changing the navigator proto</p>+</td></tr><tr>+<td><code>[hackPermissions]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>Fakes interaction with permissions</p>+</td></tr><tr>+<td><code>[mockChrome]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>Adds the chrome runtime properties</p>+</td></tr><tr>+<td><code>[mocksChromeInIframe]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>Adds the chrome runtime properties inside the every newly created iframe</p>+</td></tr><tr>+<td><code>[mockDeviceMemory]</code></td><td><code>boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>Sets device memory to other value than 0</p>+</td></tr></tbody>+</table>diff --git a/src/puppeteer.js b/src/puppeteer.jsindex c5d38303c..923853860 100644--- a/src/puppeteer.js+++ b/src/puppeteer.js@@ -65,6 +65,9 @@ const LAUNCH_PUPPETEER_DEFAULT_VIEWPORT = {* @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 {StealthOptions} [stealthOptions]+ * Using this configuration, you can make a headless chrome nearly undetectable.+ * It is recommended to use it together with the `useChrome` set to `true`.*//**@@ -242,8 +245,8 @@ export const launchPuppeteer = async (options = {}) => {}// Add stealth- if (optsCopy.stealth) {- browser = applyStealthFromBrowser(browser, optsCopy.stealth);+ if (optsCopy.stealthOptions) {+ browser = applyStealthFromBrowser(browser, optsCopy.stealthOptions);}log.info('Launching Puppeteer', _.omit(optsCopy, LAUNCH_PUPPETEER_LOG_OMIT_OPTS));return browser;diff --git a/src/stealth/stealth.js b/src/stealth/stealth.jsindex 5f91900ae..55395115b 100644--- a/src/stealth/stealth.js+++ b/src/stealth/stealth.js@@ -1,17 +1,49 @@+import _ from 'underscore';import hidingTricks from './hiding_tricks';/**- * The main purpose of this function is to override newPage function and attached selected tricks.- * @param {Browser} browser - puppeteer browser instance- * @returns {Promise<Browser>} - Instance of Browser from puppeteer package- */+ * Configuration of stealth tricks for a proper hiding effect all of them should be set to true+ * @typedef {Object} StealthOptions+ * @property {boolean} [addPlugins=false] - If plugins should be added to the navigator.+ * @property {boolean} [emulateWindowFrame=false] - Emulates window Iframe.+ * @property {boolean} [emulateWebGL=false] - Emulates graphic card.+ * @property {boolean} [emulateConsoleDebug=false] - Emulates console.debug to return null.+ * @property {boolean} [addLanguage=false] - Adds languages to the navigator.+ * @property {boolean} [hideWebDriver=false] - Hides the webdriver by changing the navigator proto.+ * @property {boolean} [hackPermissions=false] - Fakes interaction with permissions.+ * @property {boolean} [mockChrome=false] - Adds the chrome runtime properties.+ * @property {boolean} [mocksChromeInIframe=false] - Adds the chrome runtime properties inside the every newly created iframe.+ * @property {boolean} [mockDeviceMemory=false] - Sets device memory to other value than 0.+ */++const DEFAULT_STEALTH_OPTIONS = {+ addPlugins: false,+ emulateWindowFrame: false,+ emulateWebGL: false,+ emulateConsoleDebug: false,+ addLanguage: false,+ hideWebDriver: false,+ hackPermissions: false,+ mockChrome: false,+ mocksChromeInIframe: false,+ mockDeviceMemory: false,+};++/**+ * The main purpose of this function is to override newPage function and attached selected tricks.+ * @param {Browser} browser - puppeteer browser instance+ * @param {StealthOptions} options+ * @returns {Promise<Browser>} - Instance of Browser from puppeteer package+ */+export default function applyStealthToBrowser(browser, options) {const modifiedBrowser = browser;+ const opts = _.defaults(options, DEFAULT_STEALTH_OPTIONS);const prevNewPage = browser.newPage;modifiedBrowser.newPage = async (...args) => {const page = await prevNewPage.bind(browser)(...args);- await applyStealthTricks(page, options);+ await applyStealthTricks(page, opts);return Promise.resolve(page);};@@ -20,12 +52,14 @@ export default function applyStealthToBrowser(browser, options) {}/**- * Applies stealth tricks to the puppeteer page- * @param {Page} page- * @returns {Promise}- * @private- * @ignore- */+ * Applies stealth tricks to the puppeteer page+ * @param {Page} page+ * @param {StealthOptions} options+ * @returns {Promise}+ * @private+ * @ignore+ */+function applyStealthTricks(page, options) {const functions = Object.keys(options).filter((key) => {@@ -36,7 +70,7 @@ function applyStealthTricks(page, options) {/* istanbul ignore next */const addFunctions = (functionsArr) => {for (const func of functionsArr) {- eval(func)(); // eslint-disable-line+ eval(func)(); // eslint-disable-line}};diff --git a/test/stealth/stealth.js b/test/stealth/stealth.jsindex f7fef7169..c25b26aec 100644--- a/test/stealth/stealth.js+++ b/test/stealth/stealth.js@@ -11,10 +11,11 @@ const getFingerPrint = async (page) => {return page.evaluate(() => fpCollect.generateFingerprint()); // eslint-disable-line};+// we can speed up the test to make the requests to the local static htmldescribe('Stealth - testing headless chrome hiding tricks', () => {it('it adds plugins, mimeTypes and passes', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {addPlugins: true,},headless: true,@@ -34,7 +35,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it hides webDriver', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {hideWebDriver: true,},headless: true,@@ -52,7 +53,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it hacks permissions', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {hackPermissions: true,},headless: true,@@ -70,7 +71,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it adds language to navigator', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {addLanguage: true,},headless: true,@@ -89,7 +90,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it emulates WebGL', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {emulateWebGL: true,},headless: true,@@ -108,7 +109,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it emulates windowFrame', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {emulateWindowFrame: true,},headless: true,@@ -126,7 +127,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it emulates console.debug', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {emulateConsoleDebug: true,},headless: true,@@ -143,7 +144,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {});it('it should mock window.chrome to plain object', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {mockChrome: true,},headless: true,@@ -163,7 +164,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it should mock chrome when iframe is created', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {mocksChromeInIframe: true,},headless: true,@@ -181,7 +182,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it should not break iframe ', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {mocksChromeInIframe: true,},headless: true,@@ -208,7 +209,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it should mock device memory', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {mockDeviceMemory: true,},headless: true,@@ -226,7 +227,7 @@ describe('Stealth - testing headless chrome hiding tricks', () => {it('it should bypass all of the known tests for browser fingerprinting', async () => {const browser = await Apify.launchPuppeteer({- stealth: {+ stealthOptions: {addPlugins: true,emulateWindowFrame: true,hideWebDriver: true,diff --git a/website/i18n/en.json b/website/i18n/en.jsonindex 69b0543b4..c7d8fd418 100644--- a/website/i18n/en.json+++ b/website/i18n/en.json@@ -130,6 +130,9 @@},"typedefs/queueoperationinfo": {"title": "QueueOperationInfo"+ },+ "typedefs/stealthoptions": {+ "title": "StealthOptions"}},"links": {