简体   繁体   English

如何检查 Chrome 扩展程序中是否启用了暗模式

[英]How to check if Dark Mode is enabled in Chrome extension

I would like to check if chrome is currently in dark mode to theme my extension accordingly.我想检查 chrome 当前是否处于暗模式以相应地为我的扩展设置主题。

I've already checked the chrome api but did not find any suitable option.我已经检查了 chrome api,但没有找到任何合适的选项。 Maybe there is some trick to find out the status.也许有一些技巧可以找出状态。 JavaScript or CSS should be fine. JavaScript 或 CSS 应该没问题。

In CSS (possible values are: light , dark , no-preference css docs ) works with all modern browsers :在 CSS 中(可能的值是: lightdarkno-preference css docs适用于所有现代浏览器

@media (prefers-color-scheme: light) {  
  .themed {  
     background: white;    
     color: black;  
  }
}

Follow up with JS ( all modern browsers support this ):跟进 JS(所有现代浏览器都支持这个):

window.matchMedia('(prefers-color-scheme: dark)')

在此处输入图像描述

Bonus:奖金:

In electron:在电子中:

const { systemPreferences } = require('electron')
console.log(systemPreferences.isDarkMode())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native - Expo - 如何检查是否启用了暗模式? - React Native - Expo - How to check if Dark mode is enabled? Chrome 扩展程序 - 如何正确应用暗模式 CSS(尽快)? - Chrome extension - how to apply dark mode CSS correctly (as soon as possible)? 如何检测我的网站是否启用了暗模式? - How can I detect if Dark Mode is enabled on my website? Chrome打包应用:检查是否启用了“实验扩展API”标志 - Chrome Packaged Apps: Check if 'Experimental Extension APIs' flag is enabled Google Chrome 同步检查是否通过 API/扩展启用? - Google Chrome Sync check if enabled via API/Extension? 如何检查Chrome扩展程序的真实性? - How to check the authenticity of a Chrome extension? Google Chrome - 我如何以编程方式启用 chrome://flags 某些模块从禁用模式到启用模式? - Google Chrome - how can i programmatically enable chrome://flags some of the modules from disable mode to enabled mode? 启用暗模式时更改 iframe 源,禁用暗模式时将其更改回来 - Change iframe source when dark mode is enabled and change it back when dark mode is disabled 更改浏览器点击图标取决于是否启用了暗模式 - changing browser tap icon depended on if dark-mode is enabled or not 如何在div上单击chrome扩展名中的复选框 - how to check checkbox on div click in chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM