gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:50:32 26 4
gpt4 key购买 nike

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>

包.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 重新加载之后在窗口对象上注册了最大化事件,因此您的 javascript 代码将被删除并被垃圾收集。

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

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

主页.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>

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

关于如果设置了 window.location.href,node-webkit 不会最小化到托盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41529379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com