2019-03-29 21:57:02 +01:00
|
|
|
const {clipboard, nativeImage, Menu, MenuItem, shell, dialog} = require('electron');
|
2017-05-25 13:42:53 +02:00
|
|
|
const url = require('url');
|
2019-03-29 21:57:02 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
const request = require('request');
|
2017-05-25 13:42:53 +02:00
|
|
|
|
2019-01-15 09:30:07 +01:00
|
|
|
const MAILTO_PREFIX = "mailto:";
|
|
|
|
|
2017-05-25 13:42:53 +02:00
|
|
|
const PERMITTED_URL_SCHEMES = [
|
|
|
|
'http:',
|
|
|
|
'https:',
|
2019-01-15 09:30:07 +01:00
|
|
|
MAILTO_PREFIX,
|
2017-05-25 13:42:53 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
function safeOpenURL(target) {
|
|
|
|
// openExternal passes the target to open/start/xdg-open,
|
|
|
|
// so put fairly stringent limits on what can be opened
|
|
|
|
// (for instance, open /bin/sh does indeed open a terminal
|
|
|
|
// with a shell, albeit with no arguments)
|
|
|
|
const parsedUrl = url.parse(target);
|
|
|
|
if (PERMITTED_URL_SCHEMES.indexOf(parsedUrl.protocol) > -1) {
|
|
|
|
// explicitly use the URL re-assembled by the url library,
|
|
|
|
// so we know the url parser has understood all the parts
|
|
|
|
// of the input string
|
|
|
|
const newTarget = url.format(parsedUrl);
|
|
|
|
shell.openExternal(newTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onWindowOrNavigate(ev, target) {
|
|
|
|
// always prevent the default: if something goes wrong,
|
|
|
|
// we don't want to end up opening it in the electron
|
|
|
|
// app, as we could end up opening any sort of random
|
|
|
|
// url in a window that has node scripting access.
|
|
|
|
ev.preventDefault();
|
|
|
|
safeOpenURL(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLinkContextMenu(ev, params) {
|
2019-06-27 21:33:15 +02:00
|
|
|
let url = params.linkURL || params.srcURL;
|
|
|
|
|
|
|
|
if (url.startsWith('vector://vector/webapp')) {
|
|
|
|
url = "https://riot.im/app/" + url.substring(23);
|
|
|
|
}
|
2017-05-25 13:42:53 +02:00
|
|
|
|
|
|
|
const popupMenu = new Menu();
|
2018-05-18 13:00:55 +02:00
|
|
|
// No point trying to open blob: URLs in an external browser: it ain't gonna work.
|
|
|
|
if (!url.startsWith('blob:')) {
|
|
|
|
popupMenu.append(new MenuItem({
|
|
|
|
label: url,
|
|
|
|
click() {
|
|
|
|
safeOpenURL(url);
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
2017-05-25 13:42:53 +02:00
|
|
|
|
2019-03-29 21:57:02 +01:00
|
|
|
let addSaveAs = false;
|
2017-05-25 13:42:53 +02:00
|
|
|
if (params.mediaType && params.mediaType === 'image' && !url.startsWith('file://')) {
|
|
|
|
popupMenu.append(new MenuItem({
|
2019-12-19 12:16:33 +01:00
|
|
|
label: '&Copy image',
|
2017-05-25 13:42:53 +02:00
|
|
|
click() {
|
|
|
|
if (url.startsWith('data:')) {
|
|
|
|
clipboard.writeImage(nativeImage.createFromDataURL(url));
|
|
|
|
} else {
|
|
|
|
ev.sender.copyImageAt(params.x, params.y);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}));
|
2019-03-29 21:57:02 +01:00
|
|
|
|
|
|
|
// We want the link to be ordered below the copy stuff, but don't want to duplicate
|
|
|
|
// the `if` statement, so use a flag.
|
|
|
|
addSaveAs = true;
|
2017-05-25 13:42:53 +02:00
|
|
|
}
|
|
|
|
|
2019-01-15 09:30:07 +01:00
|
|
|
// No point offering to copy a blob: URL either
|
2018-05-18 13:00:55 +02:00
|
|
|
if (!url.startsWith('blob:')) {
|
2019-01-15 09:30:07 +01:00
|
|
|
// Special-case e-mail URLs to strip the `mailto:` like modern browsers do
|
|
|
|
if (url.startsWith(MAILTO_PREFIX)) {
|
|
|
|
popupMenu.append(new MenuItem({
|
2019-12-19 12:16:33 +01:00
|
|
|
label: 'Copy email &address',
|
2019-01-15 09:30:07 +01:00
|
|
|
click() {
|
|
|
|
clipboard.writeText(url.substr(MAILTO_PREFIX.length));
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
popupMenu.append(new MenuItem({
|
2019-12-19 12:16:33 +01:00
|
|
|
label: 'Copy link &address',
|
2019-01-15 09:30:07 +01:00
|
|
|
click() {
|
|
|
|
clipboard.writeText(url);
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
2018-05-18 13:00:55 +02:00
|
|
|
}
|
2019-03-29 21:57:02 +01:00
|
|
|
|
|
|
|
if (addSaveAs) {
|
|
|
|
popupMenu.append(new MenuItem({
|
2019-12-19 12:16:33 +01:00
|
|
|
label: 'Sa&ve image as...',
|
2019-03-29 21:57:02 +01:00
|
|
|
click() {
|
|
|
|
const targetFileName = params.titleText || "image.png";
|
|
|
|
const filePath = dialog.showSaveDialog({
|
|
|
|
defaultPath: targetFileName,
|
|
|
|
});
|
|
|
|
|
2019-04-01 21:24:17 +02:00
|
|
|
if (!filePath) return; // user cancelled dialog
|
|
|
|
|
2019-03-29 21:57:02 +01:00
|
|
|
try {
|
|
|
|
if (url.startsWith("data:")) {
|
|
|
|
fs.writeFileSync(filePath, nativeImage.createFromDataURL(url));
|
|
|
|
} else {
|
|
|
|
request.get(url).pipe(fs.createWriteStream(filePath));
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
dialog.showMessageBox({
|
|
|
|
type: "error",
|
|
|
|
title: "Failed to save image",
|
|
|
|
message: "The image failed to save",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-05-18 12:42:40 +02:00
|
|
|
// popup() requires an options object even for no options
|
2018-05-18 12:30:05 +02:00
|
|
|
popupMenu.popup({});
|
2017-05-25 13:42:53 +02:00
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
function _CutCopyPasteSelectContextMenus(params) {
|
|
|
|
return [{
|
|
|
|
role: 'cut',
|
2019-12-19 12:16:33 +01:00
|
|
|
label: 'Cu&t',
|
2017-05-25 13:42:53 +02:00
|
|
|
enabled: params.editFlags.canCut,
|
|
|
|
}, {
|
|
|
|
role: 'copy',
|
2019-12-19 12:16:33 +01:00
|
|
|
label: '&Copy',
|
2017-05-25 13:42:53 +02:00
|
|
|
enabled: params.editFlags.canCopy,
|
|
|
|
}, {
|
|
|
|
role: 'paste',
|
2019-12-19 12:16:33 +01:00
|
|
|
label: '&Paste',
|
2017-05-25 13:42:53 +02:00
|
|
|
enabled: params.editFlags.canPaste,
|
|
|
|
}, {
|
|
|
|
role: 'pasteandmatchstyle',
|
|
|
|
enabled: params.editFlags.canPaste,
|
|
|
|
}, {
|
|
|
|
role: 'selectall',
|
2019-12-19 12:16:33 +01:00
|
|
|
label: "Select &All",
|
2017-05-25 13:42:53 +02:00
|
|
|
enabled: params.editFlags.canSelectAll,
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
function onSelectedContextMenu(ev, params) {
|
|
|
|
const items = _CutCopyPasteSelectContextMenus(params);
|
|
|
|
const popupMenu = Menu.buildFromTemplate(items);
|
|
|
|
|
2018-05-18 12:42:40 +02:00
|
|
|
// popup() requires an options object even for no options
|
2018-05-18 12:30:05 +02:00
|
|
|
popupMenu.popup({});
|
2017-05-25 13:42:53 +02:00
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onEditableContextMenu(ev, params) {
|
|
|
|
const items = [
|
|
|
|
{ role: 'undo' },
|
|
|
|
{ role: 'redo', enabled: params.editFlags.canRedo },
|
|
|
|
{ type: 'separator' },
|
|
|
|
].concat(_CutCopyPasteSelectContextMenus(params));
|
|
|
|
|
|
|
|
const popupMenu = Menu.buildFromTemplate(items);
|
|
|
|
|
2018-05-18 12:42:40 +02:00
|
|
|
// popup() requires an options object even for no options
|
2018-05-18 12:30:05 +02:00
|
|
|
popupMenu.popup({});
|
2017-05-25 13:42:53 +02:00
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-25 13:47:36 +02:00
|
|
|
module.exports = (webContents) => {
|
2017-05-25 13:42:53 +02:00
|
|
|
webContents.on('new-window', onWindowOrNavigate);
|
2020-03-02 12:09:06 +01:00
|
|
|
// XXX: https://github.com/vector-im/riot-web/issues/8247
|
|
|
|
// webContents.on('will-navigate', onWindowOrNavigate);
|
2017-05-25 13:42:53 +02:00
|
|
|
|
|
|
|
webContents.on('context-menu', function(ev, params) {
|
|
|
|
if (params.linkURL || params.srcURL) {
|
|
|
|
onLinkContextMenu(ev, params);
|
|
|
|
} else if (params.selectionText) {
|
|
|
|
onSelectedContextMenu(ev, params);
|
|
|
|
} else if (params.isEditable) {
|
|
|
|
onEditableContextMenu(ev, params);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|