如何使用Puppeteer设置,更改或阻止时区(系统时间)? [英] How to set, change or block timezone(systemtime) with Puppeteer?

查看:210
本文介绍了如何使用Puppeteer设置,更改或阻止时区(系统时间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 操作系统:Windows 10 Pro x64型PC
  • 节点-v:v8.12.0
  • npm list puppeteer:`-puppeteer@1.9.0

我尝试过:

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

不为我工作. whoer.net看到了我的实际系统时间.

Dont work for me. whoer.net sees my actual systemtime.

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

此代码在Linux上运行良好,但在Windows上不运行:

This code works well on Linux but does not work on Windows:

process.env.TZ = 'UTC';

console.log(new Date());

在Windows上,我从操作系统中看到时区.

On Windows, I see timezone from my OS.

推荐答案

要隐藏所有网站的信息,必须使用不同设置的组合.这不限于

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

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

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

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

结果:

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

这篇关于如何使用Puppeteer设置,更改或阻止时区(系统时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆