node-webkit not minimizing to tray if window.location.href is set

919 views Asked by At

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"
  }
2

There are 2 answers

11
Saket Patel On BEST ANSWER

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();
}
0
Michael Haston On

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/