Borrowing from this answer Changing website favicon dynamically
You could add this function to your JS
/*!
* Dynamically changing favicons with JavaScript
* Works in all A-grade browsers except Safari and Internet Explorer
* Demo: http://mathiasbynens.be/demo/dynamic-favicons
*/
// HTML5™, baby! http://mathiasbynens.be/notes/document-head
document.head = document.head || document.getElementsByTagName('head')[0];
function changeFavicon(src) {
var link = document.createElement('link'),
oldLink = document.getElementById('dynamic-favicon');
link.id = 'dynamic-favicon';
link.rel = 'shortcut icon';
link.href = src;
if (oldLink) {
document.head.removeChild(oldLink);
}
document.head.appendChild(link);
}
and then you can call this function only when dark mode is enabled like
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
changeFavicon('http://www.google.com/favicon.ico');
}
If you wanted to simply add any element to the head in dark mode you could use mostly the same code
document.head = document.head || document.getElementsByTagName('head')[0];
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.head.insertAdjacentHTML('beforeend', '<link rel="icon" href="dark">');
} else {
document.head.insertAdjacentHTML('beforeend', '<link rel="icon" href="light">');
}