2016-05-23 07:16:26 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {
|
|
|
|
app,
|
|
|
|
dialog,
|
2016-06-12 06:56:22 -07:00
|
|
|
ipcMain,
|
|
|
|
shell
|
2016-05-23 07:16:26 -07:00
|
|
|
} = 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');
|
2016-05-23 07:16:26 -07:00
|
|
|
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);
|
2016-05-23 07:16:26 -07:00
|
|
|
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;
|
2016-05-23 07:16:26 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2016-09-25 07:14:01 -07:00
|
|
|
init
|
2016-05-23 07:16:26 -07:00
|
|
|
};
|