|
| 1 | +const {clipboard, nativeImage, Menu, MenuItem, shell} = require('electron'); |
| 2 | +const url = require('url'); |
| 3 | + |
| 4 | +const PERMITTED_URL_SCHEMES = [ |
| 5 | + 'http:', |
| 6 | + 'https:', |
| 7 | + 'mailto:', |
| 8 | +]; |
| 9 | + |
| 10 | +function safeOpenURL(target) { |
| 11 | + // openExternal passes the target to open/start/xdg-open, |
| 12 | + // so put fairly stringent limits on what can be opened |
| 13 | + // (for instance, open /bin/sh does indeed open a terminal |
| 14 | + // with a shell, albeit with no arguments) |
| 15 | + const parsedUrl = url.parse(target); |
| 16 | + if (PERMITTED_URL_SCHEMES.indexOf(parsedUrl.protocol) > -1) { |
| 17 | + // explicitly use the URL re-assembled by the url library, |
| 18 | + // so we know the url parser has understood all the parts |
| 19 | + // of the input string |
| 20 | + const newTarget = url.format(parsedUrl); |
| 21 | + shell.openExternal(newTarget); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function onWindowOrNavigate(ev, target) { |
| 26 | + // always prevent the default: if something goes wrong, |
| 27 | + // we don't want to end up opening it in the electron |
| 28 | + // app, as we could end up opening any sort of random |
| 29 | + // url in a window that has node scripting access. |
| 30 | + ev.preventDefault(); |
| 31 | + safeOpenURL(target); |
| 32 | +} |
| 33 | + |
| 34 | +function onLinkContextMenu(ev, params) { |
| 35 | + const url = params.linkURL || params.srcURL; |
| 36 | + |
| 37 | + const popupMenu = new Menu(); |
| 38 | + popupMenu.append(new MenuItem({ |
| 39 | + label: url, |
| 40 | + click() { |
| 41 | + safeOpenURL(url); |
| 42 | + }, |
| 43 | + })); |
| 44 | + |
| 45 | + if (params.mediaType && params.mediaType === 'image' && !url.startsWith('file://')) { |
| 46 | + popupMenu.append(new MenuItem({ |
| 47 | + label: 'Copy Image', |
| 48 | + click() { |
| 49 | + if (url.startsWith('data:')) { |
| 50 | + clipboard.writeImage(nativeImage.createFromDataURL(url)); |
| 51 | + } else { |
| 52 | + ev.sender.copyImageAt(params.x, params.y); |
| 53 | + } |
| 54 | + }, |
| 55 | + })); |
| 56 | + } |
| 57 | + |
| 58 | + popupMenu.append(new MenuItem({ |
| 59 | + label: 'Copy Link Address', |
| 60 | + click() { |
| 61 | + clipboard.writeText(url); |
| 62 | + }, |
| 63 | + })); |
| 64 | + popupMenu.popup(); |
| 65 | + ev.preventDefault(); |
| 66 | +} |
| 67 | + |
| 68 | +function _CutCopyPasteSelectContextMenus(params) { |
| 69 | + return [{ |
| 70 | + role: 'cut', |
| 71 | + enabled: params.editFlags.canCut, |
| 72 | + }, { |
| 73 | + role: 'copy', |
| 74 | + enabled: params.editFlags.canCopy, |
| 75 | + }, { |
| 76 | + role: 'paste', |
| 77 | + enabled: params.editFlags.canPaste, |
| 78 | + }, { |
| 79 | + role: 'pasteandmatchstyle', |
| 80 | + enabled: params.editFlags.canPaste, |
| 81 | + }, { |
| 82 | + role: 'selectall', |
| 83 | + enabled: params.editFlags.canSelectAll, |
| 84 | + }]; |
| 85 | +} |
| 86 | + |
| 87 | +function onSelectedContextMenu(ev, params) { |
| 88 | + const items = _CutCopyPasteSelectContextMenus(params); |
| 89 | + const popupMenu = Menu.buildFromTemplate(items); |
| 90 | + |
| 91 | + popupMenu.popup(); |
| 92 | + ev.preventDefault(); |
| 93 | +} |
| 94 | + |
| 95 | +function onEditableContextMenu(ev, params) { |
| 96 | + const items = [ |
| 97 | + { role: 'undo' }, |
| 98 | + { role: 'redo', enabled: params.editFlags.canRedo }, |
| 99 | + { type: 'separator' }, |
| 100 | + ].concat(_CutCopyPasteSelectContextMenus(params)); |
| 101 | + |
| 102 | + const popupMenu = Menu.buildFromTemplate(items); |
| 103 | + |
| 104 | + popupMenu.popup(); |
| 105 | + ev.preventDefault(); |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +module.exports = (webContents) => { |
| 110 | + webContents.on('new-window', onWindowOrNavigate); |
| 111 | + webContents.on('will-navigate', onWindowOrNavigate); |
| 112 | + |
| 113 | + webContents.on('context-menu', function(ev, params) { |
| 114 | + if (params.linkURL || params.srcURL) { |
| 115 | + onLinkContextMenu(ev, params); |
| 116 | + } else if (params.selectionText) { |
| 117 | + onSelectedContextMenu(ev, params); |
| 118 | + } else if (params.isEditable) { |
| 119 | + onEditableContextMenu(ev, params); |
| 120 | + } |
| 121 | + }); |
| 122 | +}; |
0 commit comments