Detect if the browser is using dark mode and use a different favicon

后端 未结 4 858
不思量自难忘°
不思量自难忘° 2021-01-30 08:33

Google Chrome 73 has been released, and it adds \"dark mode\" support to the browser. I notice that a lot of favicons look bad now.

Is there a way to d

4条回答
  •  独厮守ぢ
    2021-01-30 09:31

    Adding and removing an icon from the document’s head works in Firefox but not Safari:

    • Demo: https://zesty-soybean.glitch.me/
    • Source: https://glitch.com/edit/#!/zesty-soybean

    Chrome is still implementing (prefers-color-scheme: dark), so the jury’s still out. https://crbug.com/889087. In Chrome 76 with --enable-blink-features=MediaQueryPrefersColorScheme, this correctly sets the icon when the page is loaded, but does not respond dynamically to changes in dark mode.

    Safari adds a grey background to dark icons in dark mode (for example, Wikimedia Foundation, Github), so this workaround isn't necessary for legibility.

    1. Add two link rel=icon elements with ids for later:

      
      
      
    2. Create a CSS media matcher:

      matcher = window.matchMedia('(prefers-color-scheme: dark)');
      matcher.addListener(onUpdate);
      onUpdate();
      
    3. Add/remove the elements from the document's head:

      lightSchemeIcon = document.querySelector('link#light-scheme-icon');
      darkSchemeIcon = document.querySelector('link#dark-scheme-icon');
      
      function onUpdate() {
        if (matcher.matches) {
          lightSchemeIcon.remove();
          document.head.append(darkSchemeIcon);
        } else {
          document.head.append(lightSchemeIcon);
          darkSchemeIcon.remove();
        }
      }
      

提交回复
热议问题