如果设置了window.location.href,则node-webkit不会最小化到托盘

时间:2017-01-08 04:10:33

标签: node-webkit nwjs

Windows 7 x64,nwjs 0.19.4

最小化到托盘工作正常而不设置window.location.href,但是当设置nwjs时不会最小化到托盘。

每个请求修订的代码:

的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 个答案:

答案 0 :(得分:1)

您的代码的主要问题是您在使用window.location重新加载之后在窗口对象上注册maxim事件,因此您的javascript代码将被删除并进行垃圾回收。

您需要在每次重新加载后注入js代码,您可以使用 package.json的 inject_js_start inject_js_end 配置,以确保每次重新加载都会保留脚本

以下是根据您的要求的完整工作代码

<强> 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();
}

答案 1 :(得分:0)

我有同样的问题。使用webview标记代替iFrame似乎可以解决重新加载问题。

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