如果设置了 window.location.href,node-webkit 不会最小化到托盘
node-webkit not minimizing to tray if window.location.href is set
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"
}
您的代码的主要问题是您在使用 window.location 重新加载之后在 window 对象上注册最大化事件,因此您的 javascript 代码将被删除并被垃圾收集.
你需要在每次reload后注入你的js代码,你可以使用
inject_js_start 或 inject_js_end 配置 package.json 以确保在每次重新加载时保留脚本
以下是根据您的要求提供的完整工作代码
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();
}
我遇到了同样的问题。使用 webview 标签而不是 iFrame 似乎可以解决重新加载问题。
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"
}
您的代码的主要问题是您在使用 window.location 重新加载之后在 window 对象上注册最大化事件,因此您的 javascript 代码将被删除并被垃圾收集.
你需要在每次reload后注入你的js代码,你可以使用 inject_js_start 或 inject_js_end 配置 package.json 以确保在每次重新加载时保留脚本
以下是根据您的要求提供的完整工作代码
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();
}
我遇到了同样的问题。使用 webview 标签而不是 iFrame 似乎可以解决重新加载问题。