如何通过命令行强制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
相关产品推荐
相关产品推荐

