Created
October 11, 2020 17:35
-
-
Save seanosullivanuk/3e8944343e66f808e69854e1e6d15436 to your computer and use it in GitHub Desktop.
Changing the icon within a Chromium extension, depending upon the OS' dark mode status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Functions to change the app icon depending upon light or dark mode | |
function updateIconLightMode() { | |
chrome.browserAction.setIcon({ | |
path : { | |
"32": "assets/appicons/32.png", | |
"24": "assets/appicons/24.png", | |
"16": "assets/appicons/16.png" | |
} | |
}); | |
} | |
function updateIconDarkMode() { | |
chrome.browserAction.setIcon({ | |
path : { | |
"32": "assets/appicons/32-white.png", | |
"24": "assets/appicons/24-white.png", | |
"16": "assets/appicons/16-white.png" | |
} | |
}); | |
} | |
// Icon changing | |
if (window.matchMedia && | |
window.matchMedia('(prefers-color-scheme: dark)').matches) { | |
// SET DARK MODE ICON | |
updateIconDarkMode(); | |
} else { | |
// SET LIGHT MODE ICON | |
updateIconLightMode(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<script src="assets/js/changeIcon.js"></script>
Of course, be sure the icon paths are valid. To see this in action, change your OS dark mode off and on and click upon the extension icon. (Within macOS, go to System Preferences > General and change the Appearance).