如何使用Puppeteer设置、更改或阻止系统时间(时区)?

5
  • 操作系统: Windows 10专业版 x64-based PC
  • node -v版本号: v8.12.0
  • npm列表puppeteer版本号: `-- puppeteer@1.9.0

我尝试了这个:

puppeteer.launch({
  env: {
    TZ: 'Australia/Melbourne',
    ...process.env
  }
});

对我无效。whoer.net看到了我的实际系统时间。

编辑:有关https://github.com/nodejs/node/issues/4230的一些信息

这段代码在Linux上运行良好,但在Windows上不起作用:

process.env.TZ = 'UTC';

console.log(new Date());

在Windows上,我可以从我的操作系统中看到时区。

你住在哪里?肯定不是靠近澳大利亚/墨尔本吧? - Md. Abu Taher
不 =),这段代码来自 https://github.com/GoogleChrome/puppeteer/issues/3325#issuecomment-429685007。我来自白俄罗斯的莫吉廖夫。 - KisaGut
很遗憾,我认为这是一个操作系统的 bug,所以我没有解决方案。几乎所有的解决方案都提到直接在 Windows 上更改操作系统时区。 - Md. Abu Taher
1
没问题,我已经标记了你的解决方案,感谢你的帮助。我会尝试一些扩展,比如 https://chrome.google.com/webstore/detail/change-timezone-time-shif/nbofeaabhknfdcpoddmfckpokmncimpj。 - KisaGut
2个回答

7

如何在TypeScript中使用这个?这个函数对我不可用。 - Ruben

0

要隐藏所有网站的信息,您必须使用不同设置的组合。这不仅限于以下内容:

  • 代理
  • 位置
  • 时区
  • WebRTC泄漏

因为每个网站都有自己的查找方法。仅更改时区并不意味着网站会认为您来自其他地方。

然而,以下代码对我完美地起作用了。

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);

结果:

➜  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)' } ]

谢谢!但是它还不起作用。可能是因为我的操作系统是Win 10?我在这个问题上找到了一些信息https://github.com/nodejs/node/issues/4230,但它仍然没有解决=( 对不起,我再次没有在我的问题中提供足够的细节。 - KisaGut
@KisaGut 是的,在Windows上它不起作用。我在Linux上进行了测试,来自已接受解决方案的时区代码运行良好。 - rock stone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接