How to close popup window on file download automatically?


#1

Our company build web application client on top of electron.
The main window of our electron application has <webclient> tag inside of it where pages of our web application are loaded.
The tag has an attribute allowpopups to allow download files (reports from remote server in our case) with pop up windows. When we click download link on our application page (loaded into <webclient> tag) this js code (on client side) is executed:

$("a#downloadReport").click(function(){
    var reportParams = $('form#reportParams').serialize();

    var win = window.open('http://ourdomain.com/admin/report?export=1&'+reportParams, '_blank');
    return false; // prevent default action
});

How to close popup window automatically when download begin? Google Chrome can do this.

How even get list of pop-up windows with electron api? How to handle download start which began on popup window?
Also it would be perfect if popup were hidden and close itself after
some timeout or on download start event.

electron v. 0.36.11


#2

Are you talking about Electron, the application framework? Or something built on Electron?

To my knowledge, Electron doesn’t do pop-up windows on file downloads unless you make it do that. If you program it to create a pop-up window when downloading a file, then I would think you can keep a list of them.

I guess I’m not understanding the context of your questions.


#3

thanks, @leedohm! I’ve edited the question.


#4

Sorry @d9k, I’m not enough of a web programmer to know the answers to this question. It sounds like essentially you’re allowing Chromium to do the downloads and then you want programmatic access to that process? I’ll ping the developers and see if they have an answer.


#5

If you want to manage the downloading of a file, there is the DownloadItem API that should allow you to do it. You would have to open the window using the standard Electron APIs though from what the developers are saying.


#6

@leedohm thanks anyway.
We implemented our hack on top of window.open client api (not complete code just to explain the idea. download handling not implemented yet, just printing from client page):

main window:
<webview src="" nodeintegration preload="./inject.js"></webview>

inject.js:

function PopupWindow(uid){
    this.uid = uid;
}
PopupWindow.prototype.print = function(){
    ipcRenderer.send('windowPrint', {uid: this.uid});
};
PopupWindow.prototype.close = function(){
    ipcRenderer.send('windowClose', {uid: this.uid});
};
PopupWindow.prototype.focus = function(){
    ipcRenderer.send('windowFocus', {uid: this.uid});
};
PopupWindow.prototype.blur = function(){
    ipcRenderer.send('windowBlur', {uid: this.uid});
};

window.open = function(arg1, arg2){
    var windowInfo = ipcRenderer.sendSync('windowOpen', {
        url: arg1,
        target: arg2,
        domain: window.location.hostname
    });
    var windowUid = windowInfo.uid;
    return new PopupWindow(windowUid);
}

electron server server.js:

const ipcMain = require('ipc-main');
var uuid = require('node-uuid');
var BrowserWindow = require('browser-window');

var mainWindow = null;
var popupWindows = {};

// . . . . . . various stuff like main window init . . . . . .

function composeUrl(domainUrl, endUrl){
    endUrl = endUrl.trim();
    var re = /:\/\//;
    if (!endUrl.match(re)){
        // trim function from http://www.code4copy.com/jquery/javascript-trim-function-with-defined-characters/
        return 'http://' + domainUrl + '/' + stringsHelper.ltrim(endUrl, '/');
    }
    return endUrl;
}

ipcMain.on(ipcEvents.windowPrint, function(event, args){
    var popupWindow = popupWindows[args.uid];
    if (!popupWindow){
        return;
    }
    var popupPage = popupWindow.webContents;
    if (popupPage.isLoading()){
        popupPage.on('did-finish-load', function(){
            console.log('loaded');
            popupPage.print();
        });
    } else {
        popupWindow.print();
    }
});

ipcMain.on(ipcEvents.windowClose, function(event, args){
    var popupWindow = popupWindows[args.uid];
    if (!popupWindow){
        return;
    }
    var popupPage = popupWindow.webContents;
    popupWindow.close();
    delete popupWindows[args.uid];
});

ipcMain.on(ipcEvents.windowOpen, function(event, args){
    console.log('windowOpen');
    var popupWindow = new BrowserWindow({});
    var newWindowUid = uuid.v4();
    var url = composeUrl(args.domain, args.url);
    popupWindow.loadURL(url);
    popupWindow.setTitle(url);
    popupWindows[newWindowUid] = popupWindow;
    event.returnValue = {uid: newWindowUid};
});