From 85dfe78e1e55138e15cab575323b05e735157b79 Mon Sep 17 00:00:00 2001 From: Vijay Raghavan Aravamudhan Date: Wed, 26 May 2021 18:36:46 +0530 Subject: Added new entry in context menu: 'Download image' (#1449) --- src/webview/contextMenuBuilder.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/webview/contextMenuBuilder.js b/src/webview/contextMenuBuilder.js index a1a733a75..eac383fbd 100644 --- a/src/webview/contextMenuBuilder.js +++ b/src/webview/contextMenuBuilder.js @@ -11,7 +11,7 @@ import { isMac } from '../environment'; import { SEARCH_ENGINE_NAMES, SEARCH_ENGINE_URLS } from '../config'; const { - clipboard, nativeImage, remote, shell, + clipboard, ipcRenderer, nativeImage, remote, shell, } = require('electron'); const { URL } = require('url'); @@ -36,6 +36,7 @@ const contextMenuStringTable = { copyLinkUrl: () => 'Copy Link', copyImageUrl: () => 'Copy Image Address', copyImage: () => 'Copy Image', + downloadImage: () => 'Download Image', addToDictionary: () => 'Add to Dictionary', goBack: () => 'Go Back', goForward: () => 'Go Forward', @@ -192,6 +193,7 @@ module.exports = class ContextMenuBuilder { this.addInspectElement(menu, menuInfo); this.processMenu(menu, menuInfo); + this.addSeparator(menu); this.goBack(menu); this.goForward(menu); this.copyPageUrl(menu); @@ -214,6 +216,7 @@ module.exports = class ContextMenuBuilder { this.addInspectElement(menu, menuInfo); this.processMenu(menu, menuInfo); + this.addSeparator(menu); this.goBack(menu); this.goForward(menu); this.copyPageUrl(menu); @@ -337,6 +340,32 @@ module.exports = class ContextMenuBuilder { }); menu.append(copyImageUrl); + + // TODO: This doesn't seem to work on linux, so, limiting to Mac for now + if (isMac && menuInfo.srcURL.startsWith('blob:')) { + const downloadImage = new MenuItem({ + label: this.stringTable.downloadImage(), + click: () => { + const urlWithoutBlob = menuInfo.srcURL.substr(5); + this.convertImageToBase64(menuInfo.srcURL, + (dataURL) => { + const url = new window.URL(urlWithoutBlob); + const fileName = url.pathname.substr(1); + ipcRenderer.send('download-file', { + content: dataURL, + fileOptions: { + name: fileName, + mime: 'image/png', + }, + }); + }); + this.sendNotificationOnClipboardEvent(`Image downloaded: ${urlWithoutBlob}`); + }, + }); + + menu.append(downloadImage); + } + return menu; } -- cgit v1.2.3-54-g00ecf