Kis*_*Gut 5 javascript google-chrome node.js google-chrome-devtools puppeteer
我试过这个:
puppeteer.launch({
env: {
TZ: 'Australia/Melbourne',
...process.env
}
});
Run Code Online (Sandbox Code Playgroud)
别为我工作。whoer.net 看到我的实际系统时间。
编辑:有关https://github.com/nodejs/node/issues/4230的一些信息
此代码在 Linux 上运行良好,但在 Windows 上不起作用:
process.env.TZ = 'UTC';
console.log(new Date());
Run Code Online (Sandbox Code Playgroud)
在 Windows 上,我从操作系统中看到时区。
根据Puppeteer 文档,您可以使用page.emulateTimezone():
\n\n\npage.emulateTimezone()
\n\n\n\n
timezoneId<? string > 更改页面的时区。有关支持的时区 ID 的列表,请参阅ICU\xe2\x80\x99s metaZones.txt 。通过null将禁用时区模拟。返回: <承诺>
\n
例如:
\n\nawait page.emulateTimezone(\'America/Chicago\');\nRun Code Online (Sandbox Code Playgroud)\n
要隐藏所有网站的该信息,您必须使用不同设置的组合。这不限于,
\n\n因为每个网站都会有自己的查找方法。仅更改时区并不意味着网站会认为您来自其他地方。
\n\n但是,以下代码对我来说非常有效。
\n\nconst puppeteer = require('puppeteer');\n\nasync function timeZoneChecker({ timeZone }) {\n // all kind of config to pass to browser\n const launchConfig = {};\n\n if (timeZone) {\n launchConfig.env = {\n TZ: timeZone,\n ...process.env,\n };\n }\n const browser = await puppeteer.launch(launchConfig);\n const page = await browser.newPage();\n\n await page.goto('https://whoer.net/');\n const detectedTimezone = await page.$eval('.ico-timesystem', e => e.parentNode.innerText);\n await page.screenshot({ path: `screenshots/timeZone_${timeZone.replace('/', '-')}.png`, fullPage: true });\n\n await browser.close();\n\n return { timeZone, detectedTimezone };\n}\n\nPromise.all([\n timeZoneChecker({ timeZone: 'Australia/Melbourne' }),\n timeZoneChecker({ timeZone: 'Asia/Singapore' }),\n]).then(console.log);\nRun Code Online (Sandbox Code Playgroud)\n\n结果:
\n\n\xe2\x9e\x9c change-timezone node app/timezone.js\n[ { timeZone: 'Australia/Melbourne',\n detectedTimezone: 'Sun Oct 28 2018 22:44:35 GMT+1100 (Australian Eastern Daylight Time)' },\n { timeZone: 'Asia/Singapore',\n detectedTimezone: 'Sun Oct 28 2018 19:44:36 GMT+0800 (Singapore Standard Time)' } ]\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
7201 次 |
| 最近记录: |