How to get the url of the BrowserWindow?

…衆ロ難τιáo~ 提交于 2020-01-15 05:11:12

问题


This is a pretty basic question. In electron, how do I check the url/filename of a BrowserWindow?

let win;

app.on('ready', () => {
    win = new BrowserWindow();

    win.loadFile(path.join(__dirname, 'public', 'main.html'));

    win.on('closed', () => {
        app.quit();
    })
});

Ok, so say, this is the code, say after a certain event happens in a different (add:insert) BrowserWindow, I want to get the url from the main BrowserWindow (win).

let addWin;

ipcMain.on('createAddWin', ()=>{

    addWin = new BrowserWindow();

    addWin.loadFile(path.join(__dirname, 'public', 'add.html'));

    addWin.on('closed', () => {
        addWin = null
    });

ipcMain.on('add:insert', (e,insertObject) => {
//some event happens
//retrieve url from main window (win)
});

}); 

How do I go about this, I prefer to not have to send an event to the win to then send the url through ipcRenderer, though this is definitely possible.


回答1:


Getting the window's URL is possible by using the getURL() instance method of the window's webContents instance property:

let currentURL = win.webContents.getURL();


来源:https://stackoverflow.com/questions/54185293/how-to-get-the-url-of-the-browserwindow

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