mattermost-desktop/src/main/allowProtocolDialog.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
const {
app,
dialog,
2016-06-12 06:56:22 -07:00
ipcMain,
shell
} = require('electron');
const path = require('path');
const fs = require('fs');
2016-09-25 07:14:01 -07:00
const allowedProtocolFile = path.resolve(app.getPath('userData'), 'allowedProtocols.json');
var allowedProtocols = [];
function init(mainWindow) {
fs.readFile(allowedProtocolFile, 'utf-8', (err, data) => {
if (!err) {
allowedProtocols = JSON.parse(data);
}
initDialogEvent(mainWindow);
});
}
function initDialogEvent(mainWindow) {
ipcMain.on('confirm-protocol', (event, protocol, URL) => {
if (allowedProtocols.indexOf(protocol) !== -1) {
2016-06-12 06:56:22 -07:00
shell.openExternal(URL);
return;
}
dialog.showMessageBox(mainWindow, {
title: 'Non http(s) protocol',
message: `${protocol} link requires an external application.`,
detail: `The requested link is ${URL} . Do you want to continue?`,
type: 'warning',
buttons: [
'Yes',
`Yes (Save ${protocol} as allowed)`,
'No'
],
cancelId: 2,
noLink: true
}, (response) => {
switch (response) {
2016-09-25 07:14:01 -07:00
case 1: {
allowedProtocols.push(protocol);
function handleError(err) {
if (err) {
console.error(err);
}
}
fs.writeFile(allowedProtocolFile, JSON.stringify(allowedProtocols), handleError);
shell.openExternal(URL);
break;
}
case 0:
shell.openExternal(URL);
break;
default:
break;
}
});
});
}
module.exports = {
2016-09-25 07:14:01 -07:00
init
};