如何从电子中的新BrowserWindow()获取窗口对象? [英] How can I get a window object from new BrowserWindow() in electron?

查看:41
本文介绍了如何从电子中的新BrowserWindow()获取窗口对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以重用窗口对象?可能有必要,因为各个窗口可能是动态生成的.

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:

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.

这篇关于如何从电子中的新BrowserWindow()获取窗口对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆