How to find a window by name (adding custom property to a window)


#1

I am new to electron and is trying to write some simple app based on it.

And now I am having some trouble on handing the windows I create.

Say this is the first window I create in main.js:

let mainWindow;
function createWindow () {
    mainWindow = new BrowserWindow(
        {
            width: 600, 
            height: 600
        }
    );
    // add two properties for later use
    mainWindow.__name = "main_window";
    mainWindow.__tag = "main";
    mainWindow.loadURL('file://' + __dirname + '/src/render/main_menu/index.html');

    mainWindow.on('closed', function() {
        mainWindow = null;
    });
}

and in the “main_menu/index.html” I created a second window:

var BrowserWindow = require('remote').BrowserWindow;
var debuggerPanel = new BrowserWindow(
    {
        width: 200, 
        height: 300
    }
);
debuggerPanel.__name = "debugger_panel";
debuggerPanel.__tag = "panel";
debuggerPanel.loadURL('file://' + __dirname + '/../debugger_panel/debugger_panel_index.html');

As you can see I added two properties to the window I created so that I could find the window I want using the “__name” property instead of using the “BrowserWindow.fromId(id)” function.

But it seems like the properties I added to the second window is missing:

dumpWindowList = function() {
	var BrowserWindow = require('remote').BrowserWindow;
	var windowObjectArray = BrowserWindow.getAllWindows();
	for (var i = 0, len = windowObjectArray.length; i < len; i ++) {
		var windowObject = windowObjectArray[i];
		console.log("window id: " + windowObject.id);
		console.log("name: " + windowObject.__name);
		console.log("tag: " + windowObject.__tag);
	}
};

and the output is:

window id: 2
name: undefined
tag: undefined

window id: 1
name: main_window
tag: main

I am not sure where I have been doing wrong. Hope someone can help me out here, thanks :slightly_smiling: