nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

Setting the window icon dynamically

Open lucasdomanico opened this issue 4 years ago • 5 comments

NWJS Version : 0.48.0 beta Hello, is there a way to dynamically set the window icon?

lucasdomanico avatar Aug 25 '20 07:08 lucasdomanico

It seems that can set the window.icon subfield

http://docs.nwjs.io/en/latest/References/Manifest%20Format/#icon

I want to find a way that remove the icon field but anyone know how to do that ?

ltaoist avatar Jan 26 '21 10:01 ltaoist

try

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);
}
changeFavicon('http://www.google.com/favicon.ico')

edit: i think you cant.

jonlepage avatar Jan 26 '21 12:01 jonlepage

Finally I give up "remove" the icon,but use a normally replace ...

ltaoist avatar Jan 27 '21 05:01 ltaoist

When attempting in NW.js 0.47.1 I could not find any means of modifying the window icon displayed in the top left of the native Windows 10 title bar, or in the task bar.

Attempted accessing it via nw.Window.get(), in various places in the nw object, and also with @djmisterjon's approach:

<!DOCTYPE html>
<html>
  <head>
    <link id="dynamic-favicon" rel="icon" type="image/x-icon" href="favicon.ico">
  </head>
  <body>
    <script>
      function changeFavicon (src) {
        let head = document.head || document.getElementsByTagName('head')[0];

        let newLink = document.createElement('link');
        newLink.id = 'dynamic-favicon';
        newLink.rel = 'icon';
        newLink.type = 'image/x-icon';
        newLink.href = src;

        let currentLink = document.getElementById('dynamic-favicon');
        if (currentLink) {
          head.removeChild(currentLink);
        }

        head.appendChild(newLink);
      }

      setTimeout(function () {
        changeFavicon('http://www.google.com/favicon.ico');
      }, 5000);
    </script>
  </body>
</html>

None worked. I don't think modifying the icon after the window has launched is possible without closing and launching a new window.

@rogerwang can you confirm?

TheJaredWilcurt avatar Jan 27 '21 14:01 TheJaredWilcurt

@rogerwang I have verified on Windows that this feature broke between 0.42.3 (last known good) and 0.42.4 (first known bad).

Attached is a reproduction that works on all versions before 0.42.4 and breaks on all versions after 0.42.3.

This change occurred between November 7th and 13th 2019.

Code Changes that caused issue:

  • https://github.com/nwjs/nw.js/compare/nw-v0.42.3...nw-v0.42.4

TheJaredWilcurt avatar Feb 24 '21 13:02 TheJaredWilcurt