Menu 更改window.location.href后nwjs菜单停止工作

Menu 更改window.location.href后nwjs菜单停止工作,menu,window,reload,nwjs,Menu,Window,Reload,Nwjs,在我导航到另一个url后,菜单停止工作。 现在我添加了菜单: var menu = new nw.Menu({ type: 'menubar' }); var menuItems = new nw.Menu(); menuItems.append( new nw.MenuItem({ label: 'Surf Google', click: function() { window.location.href = 'https://google.com';

在我导航到另一个url后,菜单停止工作。 现在我添加了菜单:

var menu = new nw.Menu({
  type: 'menubar'
});
var menuItems = new nw.Menu();

menuItems.append(
  new nw.MenuItem({
    label: 'Surf Google',
    click: function() {
      window.location.href = 'https://google.com';
    }
  })
);

menuItems.append(
  new nw.MenuItem({
    label: 'Surf Github',
    click: function() {
      window.location.href = 'https://github.com';
    }
  })
);

menu.append(
  new nw.MenuItem({
    label: "The Menu",
    submenu: menuItems
  })
);

var w = nw.Window.get();

w.menu = menu
我创建了一个github分支来显示此问题:

以下是如何重现此问题:

git clone https://github.com/wvary/nwjs-firewall-test.git
cd nwjs-firewall-test
git checkout tested-menu
npm run build
./dist/nwjs-firewall-test-1.0.0-mac-x64/nwjs-firewall-test.app/Contents/MacOS/nwjs
应用程序启动后,点击谷歌菜单项浏览谷歌网站。
然后单击Github菜单项。它应该在Github上冲浪,但它不会。

您需要创建一个iframe,它占据整个页面并显示它,并将它的src属性设置为您想要访问的url

编辑:您需要使用网络视图而不是iframe

HTML:


这是官方的方法还是解决办法?默认菜单工作正常。只有自定义菜单无法使用。这不可能是相同的。有些网站无法处理iFrame。当您在窗口上创建菜单时,如果您创建菜单的页面被替换,菜单将停止工作。这样做的方式不是替换创建菜单的原始页面,而是在iframe中只显示您要导航到的页面。同样,如果使用NWJS插入来宾内容,出于安全原因,您应该始终使用iframe。我认为您可以使用webview标记,我已经用github测试过,它可以正常工作。我已相应地更新了我的答案
<webview style="display:none; position:fixed; width: 100%; height:100%; bottom:0; right:0; top:0; left:0;" id="view_iframe"></webview>
var menu = new nw.Menu({
  type: 'menubar'
});
var menuItems = new nw.Menu();

menuItems.append(
  new nw.MenuItem({
    label: 'Surf Google',
    click: function() {
      document.getElementById("view_iframe").style.display = "block";
      document.getElementById("view_iframe").src = "https://google.com";
    }
  })
);

menuItems.append(
  new nw.MenuItem({
    label: 'Surf Github',
    click: function() {
      document.getElementById("view_iframe").style.display = "block";
      document.getElementById("view_iframe").src = "https://github.com";
    }
  })
);

menu.append(
  new nw.MenuItem({
    label: "The Menu",
    submenu: menuItems
  })
);

var w = nw.Window.get();

w.menu = menu