You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过命令行强制Chrome为所有网站启用暗黑模式?

命令行启动Chrome启用强制深色模式

直接在Chrome启动命令后追加对应参数即可,不同操作系统的命令示例如下:

  • Windows:
    "C:\Program Files\Google\Chrome\Application\chrome.exe" --enable-features=ForceDark
    
  • macOS:
    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --enable-features=ForceDark
    
  • Linux:
    google-chrome --enable-features=ForceDark
    
CI环境适配方案

1. pa11y-ci 配置

pa11y-ci 通过 Puppeteer 调用 Chrome,只需在配置文件(默认是 .pa11yci)中添加 Chrome 启动参数:

{
  "defaults": {
    "launchOptions": {
      "args": [
        "--enable-features=ForceDark",
        "--headless",
        "--no-sandbox"
      ]
    },
    "urls": [
      "https://your-target-site.com"
    ]
  }
}

其中 --headless 和 --no-sandbox 是 CI 无头环境的必备参数,用于规避权限与界面渲染问题。

2. Lighthouse CI 配置

方式一:通过配置文件(lighthouserc.js)

在配置中指定 chromeFlags 数组,加入强制深色模式参数:

module.exports = {
  ci: {
    collect: {
      url: ["https://your-target-site.com"],
      numberOfRuns: 1,
      chromeFlags: [
        "--enable-features=ForceDark",
        "--headless",
        "--no-sandbox"
      ]
    },
    upload: {
      target: "temporary-public-storage"
    }
  }
};

方式二:命令行直接传递参数

执行 lhci collect 命令时追加 --chrome-flags 参数:

lhci collect --chrome-flags="--enable-features=ForceDark --headless --no-sandbox"

内容的提问来源于stack exchange,提问作者Patrick Kenny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 04:02:16