keeweb/app/scripts/comp/popup-notifier.js

142 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Backbone = require('backbone');
const Alerts = require('./alerts');
const Launcher = require('./launcher');
const AuthReceiver = require('./auth-receiver');
const Links = require('../const/links');
const Timeouts = require('../const/timeouts');
const Locale = require('../util/locale');
const Logger = require('../util/logger');
2016-03-26 21:12:56 +01:00
2017-01-31 07:50:28 +01:00
const PopupNotifier = {
logger: null,
2016-03-26 21:12:56 +01:00
init: function() {
2018-02-16 19:32:44 +01:00
this.logger = new Logger('popup-notifier');
2016-03-27 16:47:29 +02:00
if (Launcher) {
window.open = this._openLauncherWindow.bind(this);
2016-03-27 16:47:29 +02:00
} else {
2017-01-31 07:50:28 +01:00
const windowOpen = window.open;
2016-03-26 21:12:56 +01:00
window.open = function() {
2017-01-31 07:50:28 +01:00
const win = windowOpen.apply(window, arguments);
2016-03-26 21:12:56 +01:00
if (win) {
PopupNotifier.deferCheckClosed(win);
Backbone.trigger('popup-opened', win);
2016-03-27 15:36:07 +02:00
} else {
if (!Alerts.alertDisplayed) {
Alerts.error({
header: Locale.authPopupRequired,
body: Locale.authPopupRequiredBody
});
}
2016-03-26 21:12:56 +01:00
}
return win;
};
}
},
2016-03-27 16:47:29 +02:00
_openLauncherWindow: function(url, title, settings) {
2017-01-31 07:50:28 +01:00
const opts = {
2016-03-27 16:47:29 +02:00
show: false,
2016-07-12 07:05:07 +02:00
webPreferences: {
nodeIntegration: false,
webSecurity: false,
allowDisplayingInsecureContent: true,
allowRunningInsecureContent: true
2016-03-27 16:47:29 +02:00
}
};
if (settings) {
2017-01-31 07:50:28 +01:00
const settingsObj = {};
2016-07-17 13:30:38 +02:00
settings.split(',').forEach(part => {
2017-01-31 07:50:28 +01:00
const parts = part.split('=');
2016-03-27 16:47:29 +02:00
settingsObj[parts[0].trim()] = parts[1].trim();
});
2019-08-16 23:05:39 +02:00
if (settingsObj.width) {
opts.width = +settingsObj.width;
}
if (settingsObj.height) {
opts.height = +settingsObj.height;
}
if (settingsObj.top) {
opts.y = +settingsObj.top;
}
if (settingsObj.left) {
opts.x = +settingsObj.left;
}
2016-03-27 16:47:29 +02:00
}
2017-01-31 07:50:28 +01:00
let win = Launcher.openWindow(opts);
win.webContents.on('will-redirect', (e, url) => {
if (PopupNotifier.isOwnUrl(url)) {
2016-03-27 16:47:29 +02:00
win.webContents.stop();
win.close();
PopupNotifier.processReturnToApp(url);
2016-03-27 16:47:29 +02:00
}
});
win.webContents.on('will-navigate', (e, url) => {
if (PopupNotifier.isOwnUrl(url)) {
2016-03-27 16:47:29 +02:00
e.preventDefault();
win.close();
PopupNotifier.processReturnToApp(url);
2016-03-27 16:47:29 +02:00
}
});
win.webContents.on('crashed', (e, killed) => {
this.logger.debug('crashed', e, killed);
2018-02-16 19:32:44 +01:00
this.deferCheckClosed(win);
win.close();
win = null;
});
win.webContents.on('did-fail-load', (e, errorCode, errorDescription, validatedUrl, isMainFrame) => {
this.logger.debug('did-fail-load', e, errorCode, errorDescription, validatedUrl, isMainFrame);
2018-02-16 19:32:44 +01:00
this.deferCheckClosed(win);
win.close();
win = null;
});
win.once('page-title-updated', () => {
setTimeout(() => {
if (win) {
win.show();
win.focus();
}
}, Timeouts.PopupWaitTime);
});
2016-07-17 13:30:38 +02:00
win.on('closed', () => {
2016-03-27 16:47:29 +02:00
setTimeout(PopupNotifier.triggerClosed.bind(PopupNotifier, win), Timeouts.CheckWindowClosed);
win = null;
2016-03-27 16:47:29 +02:00
});
win.loadURL(url);
2016-03-27 16:47:29 +02:00
Backbone.trigger('popup-opened', win);
return win;
},
isOwnUrl(url) {
2019-08-16 23:05:39 +02:00
return url.lastIndexOf(Links.WebApp, 0) === 0 || url.lastIndexOf(location.origin + location.pathname, 0) === 0;
},
2016-03-27 16:47:29 +02:00
processReturnToApp: function(url) {
2017-01-31 07:50:28 +01:00
const returnMessage = AuthReceiver.urlArgsToMessage(url);
2016-03-27 16:47:29 +02:00
if (Object.keys(returnMessage).length > 0) {
2017-01-31 07:50:28 +01:00
const evt = new Event('message');
2016-03-27 16:47:29 +02:00
evt.data = returnMessage;
window.dispatchEvent(evt);
}
},
2016-03-26 21:12:56 +01:00
deferCheckClosed: function(win) {
setTimeout(PopupNotifier.checkClosed.bind(PopupNotifier, win), Timeouts.CheckWindowClosed);
},
checkClosed: function(win) {
if (win.closed) {
2016-03-27 13:54:35 +02:00
setTimeout(PopupNotifier.triggerClosed.bind(PopupNotifier, win), Timeouts.CheckWindowClosed);
2016-03-26 21:12:56 +01:00
} else {
PopupNotifier.deferCheckClosed(win);
}
2016-03-27 13:54:35 +02:00
},
triggerClosed: function(win) {
Backbone.trigger('popup-closed', win);
2016-03-26 21:12:56 +01:00
}
};
module.exports = PopupNotifier;