title: How can I disable javascript in puppeteer?
body: <p>I am trying to disable javascript so that websites know that the javascript is disabled on puppeteer (ie: <code>&lt;noscript&gt;</code> tags) in a base class made to crawl websites however my script fail to so as it's not disabling javascript when I go to any websites.</p>
  <p>Here is my code:</p>
  <pre class="lang-js prettyprint-override"><code>// https://stackoverflow.com/questions/39134419/run-tor-browser-with-selenium-webdriver
  import puppeteer, { puppeteerErrors, Target, Browser } from &quot;puppeteer&quot;;
  import { readFileSync } from &quot;fs&quot;
  import { helpers } from &quot;./helpers&quot;;
  import _ from &quot;lodash&quot;
  
  /**
   * Base class for all crawler
   */
  abstract class BaseCrawler {
    public static readonly TOR_PATH = process.env.TOR_PATH ?? &quot;&quot;;
    public static readonly TOR_PROFILE_PATH = process.env.TOR_PROFILE_PATH ?? &quot;&quot;;
    public static readonly TORRC_PATH = process.env.TORRC_PATH;
    public static headless = false
    public readonly browser: Promise&lt;puppeteer.Browser&gt;;
    private readonly jsEnabled: boolean;
  
    /**
     * get the active page
     * @returns null if it couldn't get the active
     */
    public async activePage(timeout = 30_000): Promise&lt;puppeteer.Page | null&gt; {
      const browser = await this.browser;
      var start = new Date().getTime();
      while (new Date().getTime() - start &lt; timeout) {
        var pages = await browser.pages();
        var arr = [];
        for (const p of pages) {
          if (await p.evaluate(() =&gt; { return document.visibilityState == 'visible' })) {
            arr.push(p);
          }
        }
  
        if (arr.length == 1)
          return arr[0];
      }
      return null;
    }
  
    constructor(jsEnabled = false, website = &quot;https://google.com&quot;) {
      console.log(Browser)
      
      this.browser = puppeteer.launch({
        headless: BaseCrawler.headless,
        //args: [&quot;--proxy-server=socks5://127.0.0.1:9050&quot;],
        userDataDir: &quot;./.headless-data&quot;
      });
  
      this.jsEnabled = jsEnabled;
      this.browser.then(async (b) =&gt; {
        b.on(&quot;targetcreated&quot;, async (e: Target) =&gt; {
  
          const page = await e.page();
          // set a tor useragent
          page?.setUserAgent(`Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/${_.random(60, 100)}.0`);
  
          // disable script if it's aksed
          if (page?.url()) {
            // console.log(page.url().search(&quot;chrome://&quot;))
            if (page.url().search(&quot;chrome://&quot;) &lt; 0)
              page?.setJavaScriptEnabled(this.jsEnabled)
          }
  
          page?.on('request', request =&gt; {
            if (request.resourceType() === 'script')
              request.abort();
            else
              request.continue();
          })
        })
      })
  
      this.browser.then(async (b) =&gt; {
        b.newPage()
        const page = await b.newPage();
        page.goto(website);
      })
    }
  }
  /** Bland tor window just made to browse tor */
  export class TorWindow extends BaseCrawler {
  };
  </code></pre>
  <p>I tried to hook the <code>newPage</code> function however it doesn't work as it's giving me the following error:</p>
  <pre><code>PS C:\Users\vince\project\js\crawler-project&gt; yarn run browser
  yarn run v1.22.10
  $ node . --tor-window
  undefined
  C:\Users\vince\project\js\crawler-project\\dist\Crawler.js:35
          const old_newpage = puppeteer_1.default.Browser.prototype.newPage;
                                                          ^
  
  TypeError: Cannot read property 'prototype' of undefined
      at new BaseCrawler (C:\Users\vince\project\js\crawler-project\dist\Crawler.js:35:57)
      at new TorWindow (C:\Users\vince\project\js\crawler-project\dist\Crawler.js:98:1)
      at Object.&lt;anonymous&gt; (C:\Users\vince\project\js\crawler-project\dist\index.js:27:5)
      at Module._compile (node:internal/modules/cjs/loader:1092:14)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
      at Module.load (node:internal/modules/cjs/loader:972:32)
      at Function.Module._load (node:internal/modules/cjs/loader:813:14)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
      at node:internal/main/run_main_module:17:47
  error Command failed with exit code 1.
  info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>// https://stackoverflow.com/questions/39134419/run-tor-browser-with-selenium-webdriver
  import puppeteer, { puppeteerErrors, Target, Browser } from &quot;puppeteer&quot;;
  import { readFileSync } from &quot;fs&quot;
  import { helpers } from &quot;./helpers&quot;;
  import _ from &quot;lodash&quot;
  
  /**
   * Base class for all crawler
   */
  abstract class BaseCrawler {
    public static readonly TOR_PATH = process.env.TOR_PATH ?? &quot;&quot;;
    public static readonly TOR_PROFILE_PATH = process.env.TOR_PROFILE_PATH ?? &quot;&quot;;
    public static readonly TORRC_PATH = process.env.TORRC_PATH;
    public static headless = false
    public readonly browser: Promise&lt;puppeteer.Browser&gt;;
    private readonly jsEnabled: boolean;
  
    /**
     * get the active page
     * @returns null if it couldn't get the active
     */
    public async activePage(timeout = 30_000): Promise&lt;puppeteer.Page | null&gt; {
      const browser = await this.browser;
      var start = new Date().getTime();
      while (new Date().getTime() - start &lt; timeout) {
        var pages = await browser.pages();
        var arr = [];
        for (const p of pages) {
          if (await p.evaluate(() =&gt; { return document.visibilityState == 'visible' })) {
            arr.push(p);
          }
        }
  
        if (arr.length == 1)
          return arr[0];
      }
      return null;
    }
  
    constructor(jsEnabled = false, website = &quot;https://check.torproject.org&quot;) {
      console.log(Browser)
      const old_newpage = puppeteer.Browser.prototype.newPage
      puppeteer.Browser.prototype.newPage = async () =&gt; {
        const page = await old_newpage()
        page.setJavaScriptEnabled(this.jsEnabled)
        return page;
      }
      this.browser = puppeteer.launch({
        headless: BaseCrawler.headless,
        args: [&quot;--proxy-server=socks5://127.0.0.1:9050&quot;],
        userDataDir: &quot;./.headless-data&quot;
      });
  
      this.jsEnabled = jsEnabled;
      this.browser.then(async (b) =&gt; {
        b.on(&quot;targetcreated&quot;, async (e: Target) =&gt; {
  
          const page = await e.page();
          // set a tor useragent
          page?.setUserAgent(`Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/${_.random(60, 100)}.0`);
  
          // disable script if it's aksed
          if (page?.url()) {
            // console.log(page.url().search(&quot;chrome://&quot;))
            if (page.url().search(&quot;chrome://&quot;) &lt; 0)
              page?.setJavaScriptEnabled(this.jsEnabled)
          }
  
          page?.on('request', request =&gt; {
            if (request.resourceType() === 'script')
              request.abort();
            else
              request.continue();
          })
        })
      })
  
      this.browser.then(async (b) =&gt; {
        b.newPage()
        const page = await b.newPage();
        page.goto(website);
      })
    }
  }
  /** Bland tor window just made to browse tor */
  export class TorWindow extends BaseCrawler {
  };
  </code></pre>