最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I get a window object from new BrowserWindow() in electron? - Stack Overflow

programmeradmin3浏览0评论

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

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
});
Share Improve this question edited Dec 28, 2016 at 8:16 Jens Habegger 5,45643 silver badges58 bronze badges asked Dec 27, 2016 at 19:15 KiochanKiochan 4872 gold badges7 silver badges12 bronze badges 2
  • electron.atom.io/docs/api/… – Saar Commented Dec 27, 2016 at 19:27
  • sry, but I don't find which instance methods I can use it to get window object . – Kiochan Commented Dec 28, 2016 at 22:14
Add a ment  | 

1 Answer 1

Reset to default 8

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.

发布评论

评论列表(0)

  1. 暂无评论