简体   繁体   English

如何从电子中的新BrowserWindow()获取窗口对象?

[英]How can I get a window object from new BrowserWindow() in electron?

Is there a way to reuse the window object? 有没有一种方法可以重用窗口对象? It may be necessary because the respective window may be generated dynamically. 可能有必要,因为各个窗口可能会动态生成。

var electron = require('electron');
var app = electron.app
var BrowserWindow = electron.BrowserWindow

app.on('ready', function(){
  var win = new BrowserWindow();
  win.loadURL 'file://' + __dirname + '/index.html';

  // now i want use the window object in my BroserWindow win
 window = win.getWindowObject; // like this
  window.document.write(); // i can use window object here
});

Still there isn't a way to directly access window object, however the method BrowserWindow.webContents.executeJavaScript allows to do it indirect: 仍然没有直接访问窗口对象的方法,但是BrowserWindow.webContents.executeJavaScript方法允许间接执行此操作:

let myWindow = new BrowserWindow(params);
myWindow.webContents.executeJavaScript('window.anyWantedProperty')
    .then(result => console.log(result));

Be careful if you pass user input as this method allow code injection. 如果传递用户输入,请小心,因为此方法允许代码注入。

with what I know of electron, when you use 用我对电子的了解,当您使用

var win = new BrowserWindow();

the value of win is the window object. win的值是窗口对象。 Once you load the main page using win.loadUrl, you really shouldn't have to write to it again. 一旦使用win.loadUrl加载了主页,您实际上就不必再次写入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM