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?