1

I have an extension and I want to detect dark mode in Chrome.

In the toggleIcon.js I try to detect theme by this code:

if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
  chrome.runtime.sendMessage({
     scheme: "dark"
  })
}
else{
  chrome.runtime.sendMessage({
    scheme: "light"
  })
}

And In the background.js I try to get requests with this code:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  console.log(request.scheme)
  sendResponse();
}); 

But I always get light mode even when using Bits + Pieces theme for chrome. How can I fix my problem?

Hanie Asemi
  • 1,318
  • 2
  • 9
  • 23
  • @wOxxOm Could you please explain more? – Hanie Asemi Jul 31 '21 at 16:50
  • That theme package is not what constitutes a `dark` theme. Only the dark theme in the OS settings does that. Then it is automatically detected by Chrome. – wOxxOm Jul 31 '21 at 17:49
  • see also [Toggle Chrome Extension Icon based on light or dark mode browser?](https://stackoverflow.com/questions/58880234/toggle-chrome-extension-icon-based-on-light-or-dark-mode-browser) – milahu Dec 28 '22 at 11:01

0 Answers0