Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle Chrome Extension Icon based on light or dark mode browser?

I've tried searching for this, and the closest related question that I could find was from 3+ years ago and had to do with the incognito window being dark, while a normal chrome window was light back then.

Now that we have the ability to have a light or dark mode browser, it's hard to find an icon design and color that looks good for both light and dark modes. Here's an example:

Examples of icons on dark-mode theme

In the image above you can see that the first and third icons are black, so they are hard to see when using dark-mode. The middle icon (the one I'm using for my extension)looks great on dark mode, but terrible on light mode. See below:

light icons are hard to see unless on dark mode

So does anyone know if there is there a way to detect the browser mode (light or dark) and swap out the icon?

like image 964
Tyler Youngblood Avatar asked Nov 15 '19 15:11

Tyler Youngblood


1 Answers

Thanks to wOxxOm I was able to figure this out.

First, I needed to create a content script (which I called toggleIcon.js) and add it to the manifist.json file.

enter image description here

Then I added the following to toggleIcon.js - which sends scheme: "dark" to my background.js file if window.matchMedia matches prefers-color-scheme: dark.

enter image description here

Then in my background.js file I listen for that message, and if request.scheme == "dark" I use chrome.broserAction.setIcon to change the paths for each of my icons to the dark version.

enter image description here

This effectively overrides my original icon paths as declared in the manifest.json file (as shown below).

enter image description here

The only downside I see is that this requires a content_script, which if you want your extension to work on any page, requires you to also add "matches": ["<all_urls>"] to your extension, which slows down the approval process. Which is why in my comments above I mentioned I had been avoiding using a content_script.

Also, I think it makes sense to use the version of your icons that work best on light-mode as your default, because I think the chrome extension page will pull from these for some of the icons they use (and that page has a white background). As an example, here's how my old icon looked (not enough contrast).

enter image description here

Hopefully this helps someone else!!

like image 96
Tyler Youngblood Avatar answered Oct 05 '22 08:10

Tyler Youngblood