How to set, change or block timezone(systemtime) with Puppeteer?

后端 未结 2 875
别那么骄傲
别那么骄傲 2021-01-29 02:25
  • OS: Windows 10 Pro x64-based PC
  • node -v: v8.12.0
  • npm list puppeteer: `-- puppeteer@1.9.0

I tried this:

puppeteer.launch({
         


        
相关标签:
2条回答
  • 2021-01-29 02:44

    To hide that info for all website, you must use combination of different settings. This is not limited to,

    • Proxies
    • Location
    • Timezone
    • WebRTC leak

    Because every website will have their own method of finding out. Just changing timezone does not mean the website will think you are from somewhere else.

    However, the following code worked perfectly for me.

    const puppeteer = require('puppeteer');
    
    async function timeZoneChecker({ timeZone }) {
      // all kind of config to pass to browser
      const launchConfig = {};
    
      if (timeZone) {
        launchConfig.env = {
          TZ: timeZone,
          ...process.env,
        };
      }
      const browser = await puppeteer.launch(launchConfig);
      const page = await browser.newPage();
    
      await page.goto('https://whoer.net/');
      const detectedTimezone = await page.$eval('.ico-timesystem', e => e.parentNode.innerText);
      await page.screenshot({ path: `screenshots/timeZone_${timeZone.replace('/', '-')}.png`, fullPage: true });
    
      await browser.close();
    
      return { timeZone, detectedTimezone };
    }
    
    Promise.all([
      timeZoneChecker({ timeZone: 'Australia/Melbourne' }),
      timeZoneChecker({ timeZone: 'Asia/Singapore' }),
    ]).then(console.log);
    

    Result:

    ➜  change-timezone node app/timezone.js
    [ { timeZone: 'Australia/Melbourne',
        detectedTimezone: 'Sun Oct 28 2018 22:44:35 GMT+1100 (Australian Eastern Daylight Time)' },
      { timeZone: 'Asia/Singapore',
        detectedTimezone: 'Sun Oct 28 2018 19:44:36 GMT+0800 (Singapore Standard Time)' } ]
    
    0 讨论(0)
  • 2021-01-29 02:50

    According to the Puppeteer documentation, you can use page.emulateTimezone():

    page.emulateTimezone()

    timezoneId <?string> Changes the timezone of the page. See ICU’s metaZones.txt for a list of supported timezone IDs. Passing null disables timezone emulation.

    returns: <Promise>

    For example:

    await page.emulateTimezone('America/Chicago');
    
    0 讨论(0)
提交回复
热议问题