4

Windows 7 x64, nwjs 0.19.4

Minimize to tray works fine without setting window.location.href, but when set nwjs will not minimize to tray.

Revised Code Per Request:

index.html

<html>
<body>
<div></div>
<script>

  // Load library
  var gui = require('nw.gui');

  // Reference to window and tray
  var win = gui.Window.get();
  var tray;

  onload = function () {
    window.location.href = "http://iheartradio.com"
  };

  // Get the minimize event
  win.on('minimize', function () {
    // Hide window
    win.hide();

    var tray = new nw.Tray({
      title: 'Web Music Player',
      icon: 'img/music.png'
    });

    // Show window and remove tray when clicked
    tray.on('click', function () {
      win.show();
      this.remove();
      tray = null;
    });
  });


</script>
</body>
</html>

package.json

{
  "name": "webmusicplayer",
  "version": "0.1.0",
  "main": "index.html",
  "single-instance": true,
  "window": {
    "title": "webmusicplayer",
    "min_width": 1200,
    "min_height": 600
  },
  "webkit": {
    "plugin": true
  },
  "chromium-args": "--load-plugin=ffmpegsumo.dll --child-clean-exit --disable-direct-composition --allow-running-insecure-content --no-proxy-server --enable-video-player-chromecast-support"
  }
dan
  • 2,857
  • 6
  • 34
  • 60
  • If this keeps giving you trouble, you might want to try using [Electron](http://electron.atom.io/) instead of nw.js. I find it to be less buggy in general. – Seth Holladay Jan 08 '17 at 04:35
  • I have tried electron but in this case electron has some problems with certain websites where nwjs does not. I have tried various flags such as insecure content ect.. in electron but still had problems. I have nwjs working perfectly besides the minimize to tray. I am sure there is a way to get this to work – dan Jan 09 '17 at 16:49
  • i would try the `window.location.href` option later but i can *suggest you* to use the `window.open` event as used like ` var win = gui.Window.open('popup.html');` since according to this [**file**](https://github.com/nwjs/nw.js/blob/7267ee3cb006261e6946542ee532db234a754b0f/src/api/window/window.js) in the **node webktit** conveys the same functionality – Pritish Vaidya Jan 11 '17 at 06:11
  • That opens up a popup window. I want the main window to open the url not a new popup window – dan Jan 11 '17 at 15:13

2 Answers2

1

Main issue with your code is that you are registering maximize event on window object after that you are reloading using window.location, so your javascript code will be removed and garbage collected.

You need to inject your js code after every reload, you can use inject_js_start or inject_js_end config of package.json to make sure you script is preserved on every reload

Below is the full working code as per your requirement

home.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" /> 
    <title>Tray Demo</title>

    <script type="text/javascript">
    console.log('redirecting the page');
        window.location.href = 'http://www.microsoft.com';
    </script>
</head>
<body>
    <p>redirecting the page...</p>
</body>
</html>

package.json

{
  "main": "home.html",
  "name": "tray-demo",
  "description": "tray demo for windows",
  "version": "1.0",
  "inject_js_start": "NWInit.js",
  "window": {
    "title": "Tray Demo",
    "resizable": true,
    "show_in_taskbar": true
  },
  "webkit": {
    "plugin": true
  },
  "node-remote": "*://*"
}

NWInit.js

if(typeof nw != 'undefined') {
    NWInit = {
        initApp: function() {
            console.log('init app called');

            var win = nw.Window.get();
            win.showDevTools();

            win.on('minimize', function() {
                console.log('minimize called');

                if(typeof nw.Tray == 'undefined') {
                    return;
                }

                win.hide();

                var tray = new nw.Tray({
                    title: 'Web Music Player',
                    icon: 'img/music.png'
                });

                tray.on('click', function() {
                    console.log('tray clicked');

                    win.show();

                    tray.remove();
                    tray = null;
                });
            });
        }
    };

    NWInit.initApp();
}
Saket Patel
  • 6,573
  • 1
  • 27
  • 36
  • I tested your code and this does not minimize to tray. – dan Jan 15 '17 at 03:34
  • I take that back it does work, but what made it work is "node-remote": "*://*" in the package.json. I did not realize that command was needed. Thank you. – dan Jan 15 '17 at 04:04
  • If I add a menu item then it stops minimizing. – dan Jan 15 '17 at 05:02
  • Let me know if it is still not working for you, will check again – Saket Patel Jan 15 '17 at 07:23
  • Still having an issue the code seems to reload itself. Try using the url http://iheartradio.com. Look at your onload log and you will see it keeps firing. – dan Jan 15 '17 at 14:47
  • Yes that is true, nwjs injects this script specified in inject_js_start/end in every iframe, so if page has multiple iframes than log will appear multiple times – Saket Patel Jan 17 '17 at 18:54
  • Ok so what is the solution to get minimizing to work? Reloading the page every time is not a valid solution. I just wan't to minimize the app regardless of what page location.href loads. – dan Jan 17 '17 at 18:59
  • Not getting your point, are you saying that with url as iheartradio.com it is not working fine? if possible, can you give me your code, so I can check and debug it? – Saket Patel Jan 17 '17 at 19:22
  • It is working, but if I try to debug the app it reloads for as many iframes that are in the site. In my original version the page loaded only once and everything worked except minimizing to tray. It just does not seem right to fire for all those iframes just to minimize the app to tray. I have revised my code to the original and included the package.json – dan Jan 17 '17 at 20:06
  • Did you have a chance to take a look at this? I would think that minimizing to tray should be an os function and not something to do with the web page itself. It just does not make since to have to inject code into the webpage and iframes. I think there must be another way. – dan Jan 21 '17 at 21:05
  • Ok will check and reply back – Saket Patel Jan 24 '17 at 14:29
0

I had the same problem. Using the webview tag instead of iFrame seems to fix the reloading issue.

https://nwjs.readthedocs.io/en/nw13/References/Frames/