如何在桌面模式下运行 lighthouse-puppeteer?

发布于 2025-01-17 06:40:32 字数 1874 浏览 4 评论 0原文

我正在尝试使用灯塔的基本代码进行测试。 但如果我运行此代码,则会显示“移动”模式。(m.naver.com) (我写了一个“www.naver.com”,但是,m.naver.com 被重定向)

我想在桌面上运行。我在哪里修改代码? 我尽力寻找解决方案,但我做不到。所以请分享您的解决方案。谢谢。

代码主页: https://github.com/addyosmani/puppeteer-webperf

import lighthouse from 'lighthouse';
import puppeteer from 'puppeteer';
import chromeLauncher from 'chrome-launcher';
import reportGenerator from 'lighthouse/report/generator/report-generator.js';
import request from 'request';
import util from 'util';
import fs from 'fs';


const options = {
  logLevel: 'info',
  disableDeviceEmulation: true,
  chromeFlags: ['--emulated-form-factor=desktop']
};

async function lighthouseFromPuppeteer(url, options, config = null) {
  // Launch chrome using chrome-launcher
  const chrome = await chromeLauncher.launch(options);
  options.port = chrome.port;

  // Connect chrome-launcher to puppeteer
  const resp = await util.promisify(request)(`http://localhost:${options.port}/json/version`);
  const { webSocketDebuggerUrl } = JSON.parse(resp.body);
  const browser = await puppeteer.connect({ browserWSEndpoint: webSocketDebuggerUrl });

  // Run Lighthouse
  const { lhr } = await lighthouse(url, options, config);
  await browser.disconnect();
  await chrome.kill();

  const html = reportGenerator.generateReport(lhr, 'html');
  fs.writeFile('report.html', html, function (err) {
    if (err) throw err;
  });

  const json = reportGenerator.generateReport(lhr, 'json');

  const audits = JSON.parse(json).audits; // Lighthouse audits
  const first_contentful_paint = audits['first-contentful-paint'].displayValue;
  const total_blocking_time = audits['total-blocking-time'].displayValue;
  const time_to_interactive = audits['interactive'].displayValue;

  console.log(`\n
     Lighthouse metrics: 
     
              

I'm trying to test using this basic code of lighthouse.
but If I run this code, There are shown with "Mobile" mode.(m.naver.com)
(I wrote a "www.naver.com" but, m.naver.com was redirected)

I want to run at Desktop. Where do I revise the code?
I try to do my best to find a solution, but I couldn't. So Please share your solution. Thanks.

code homepage : https://github.com/addyosmani/puppeteer-webperf

import lighthouse from 'lighthouse';
import puppeteer from 'puppeteer';
import chromeLauncher from 'chrome-launcher';
import reportGenerator from 'lighthouse/report/generator/report-generator.js';
import request from 'request';
import util from 'util';
import fs from 'fs';


const options = {
  logLevel: 'info',
  disableDeviceEmulation: true,
  chromeFlags: ['--emulated-form-factor=desktop']
};

async function lighthouseFromPuppeteer(url, options, config = null) {
  // Launch chrome using chrome-launcher
  const chrome = await chromeLauncher.launch(options);
  options.port = chrome.port;

  // Connect chrome-launcher to puppeteer
  const resp = await util.promisify(request)(`http://localhost:${options.port}/json/version`);
  const { webSocketDebuggerUrl } = JSON.parse(resp.body);
  const browser = await puppeteer.connect({ browserWSEndpoint: webSocketDebuggerUrl });

  // Run Lighthouse
  const { lhr } = await lighthouse(url, options, config);
  await browser.disconnect();
  await chrome.kill();

  const html = reportGenerator.generateReport(lhr, 'html');
  fs.writeFile('report.html', html, function (err) {
    if (err) throw err;
  });

  const json = reportGenerator.generateReport(lhr, 'json');

  const audits = JSON.parse(json).audits; // Lighthouse audits
  const first_contentful_paint = audits['first-contentful-paint'].displayValue;
  const total_blocking_time = audits['total-blocking-time'].displayValue;
  const time_to_interactive = audits['interactive'].displayValue;

  console.log(`\n
     Lighthouse metrics: 
     ???? First Contentful Paint: ${first_contentful_paint}, 
     ⌛️ Total Blocking Time: ${total_blocking_time},
     ???? Time To Interactive: ${time_to_interactive}`);
}

lighthouseFromPuppeteer("https://www.naver.com", options);

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2025-01-24 06:40:32
const config = require('lighthouse/lighthouse-core/config/desktop-config');


const {lhr} = await lighthouse(url, {
            port: (new URL(browser.wsEndpoint())).port,
            output: 'json',
            logLevel: 'info',
        }, config);
const config = require('lighthouse/lighthouse-core/config/desktop-config');


const {lhr} = await lighthouse(url, {
            port: (new URL(browser.wsEndpoint())).port,
            output: 'json',
            logLevel: 'info',
        }, config);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文