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

拥有回忆 提交于 2021-01-27 15:12:53

问题


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

回答1:


Still there isn't a way to directly access window object, however the method BrowserWindow.webContents.executeJavaScript allows to do it indirect:

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.



来源:https://stackoverflow.com/questions/41350911/how-can-i-get-a-window-object-from-new-browserwindow-in-electron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!