From 6e50669da6538d783821395c7cee9b319ecaba41 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 19 Sep 2018 01:39:18 +0200 Subject: [PATCH] feat: separate context action for links Closes #579 --- add-on/_locales/en/messages.json | 10 ++++++--- add-on/src/lib/context-menus.js | 36 ++++++++++++++++++++++---------- add-on/src/lib/ipfs-companion.js | 17 +++++++-------- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/add-on/_locales/en/messages.json b/add-on/_locales/en/messages.json index 04339dc04..e964f7e8a 100644 --- a/add-on/_locales/en/messages.json +++ b/add-on/_locales/en/messages.json @@ -84,17 +84,21 @@ "description": "Default label for icon hidden in Page Action menu (pageAction_titleNonIpfs)" }, "contextMenu_AddToIpfsSelection": { - "message": "Add selected text to IPFS", + "message": "Add Selected Text to IPFS", "description": "An item in right-click context menu (contextMenu_AddToIpfsSelection)" }, "contextMenu_AddToIpfsRawCid": { - "message": "Add to IPFS", + "message": "Add This Object to IPFS", "description": "An item in right-click context menu (contextMenu_AddToIpfsRawCid)" }, "contextMenu_AddToIpfsKeepFilename": { - "message": "Add to IPFS (Keep Filename)", + "message": "Add This Object to IPFS (Keep Filename)", "description": "An item in right-click context menu (contextMenu_AddToIpfsKeepFilename)" }, + "contextMenu_AddToIpfsLink": { + "message": "Add This Link to IPFS", + "description": "An item in right-click context menu (contextMenu_AddToIpfsLink)" + }, "notify_addonIssueTitle": { "message": "IPFS Add-on Issue", "description": "A title of system notification (notify_addonIssueTitle)" diff --git a/add-on/src/lib/context-menus.js b/add-on/src/lib/context-menus.js index 9e5de833b..0c7213886 100644 --- a/add-on/src/lib/context-menus.js +++ b/add-on/src/lib/context-menus.js @@ -2,16 +2,19 @@ const browser = require('webextension-polyfill') -async function findUrlForContext (context) { +async function findUrlForContext (context, contextField) { if (context) { - if (context.linkUrl) { - // present when clicked on a link - return context.linkUrl + if (contextField) { + return context[contextField] } if (context.srcUrl) { // present when clicked on page element such as image or video return context.srcUrl } + if (context.linkUrl) { + // present when clicked on a link + return context.linkUrl + } if (context.pageUrl) { // pageUrl is the root frame return context.pageUrl @@ -27,10 +30,11 @@ module.exports.findUrlForContext = findUrlForContext const contextMenuAddToIpfsSelection = 'contextMenu_AddToIpfsSelection' const contextMenuAddToIpfsRawCid = 'contextMenu_AddToIpfsRawCid' const contextMenuAddToIpfsKeepFilename = 'contextMenu_AddToIpfsKeepFilename' +const contextMenuAddToIpfsLink = 'contextMenu_AddToIpfsLink' const contextMenuCopyCanonicalAddress = 'panelCopy_currentIpfsAddress' const contextMenuCopyAddressAtPublicGw = 'panel_copyCurrentPublicGwUrl' -function createContextMenus (getState, runtime, ipfsPathValidator, { onAddToIpfs, onAddToIpfsKeepFilename, onCopyCanonicalAddress, onCopyAddressAtPublicGw }) { +function createContextMenus (getState, runtime, ipfsPathValidator, { onAddFromContext, onCopyCanonicalAddress, onCopyAddressAtPublicGw }) { let copyAddressContexts = ['page', 'image', 'video', 'audio', 'link'] if (runtime.isFirefox) { // https://github.com/ipfs-shipyard/ipfs-companion/issues/398 @@ -43,25 +47,34 @@ function createContextMenus (getState, runtime, ipfsPathValidator, { onAddToIpfs contexts: ['selection'], documentUrlPatterns: [''], enabled: false, - onclick: onAddToIpfs + onclick: (context) => onAddFromContext(context, 'selectionText') }) browser.contextMenus.create({ id: contextMenuAddToIpfsRawCid, title: browser.i18n.getMessage(contextMenuAddToIpfsRawCid), - contexts: ['image', 'video', 'audio', 'link'], + contexts: ['image', 'video', 'audio'], documentUrlPatterns: [''], enabled: false, - onclick: onAddToIpfs + onclick: (context) => onAddFromContext(context, 'srcUrl') }) browser.contextMenus.create({ id: contextMenuAddToIpfsKeepFilename, title: browser.i18n.getMessage(contextMenuAddToIpfsKeepFilename), - contexts: ['image', 'video', 'audio', 'link'], + contexts: ['image', 'video', 'audio'], + documentUrlPatterns: [''], + enabled: false, + onclick: (context) => onAddFromContext(context, 'srcUrl', { wrapWithDirectory: true }) + }) + + browser.contextMenus.create({ + id: contextMenuAddToIpfsLink, + title: browser.i18n.getMessage(contextMenuAddToIpfsLink), + contexts: ['link'], documentUrlPatterns: [''], enabled: false, - onclick: onAddToIpfsKeepFilename + onclick: (context) => onAddFromContext(context, 'linkUrl', { wrapWithDirectory: true }) }) browser.contextMenus.create({ @@ -99,7 +112,8 @@ function createContextMenus (getState, runtime, ipfsPathValidator, { onAddToIpfs const canUpload = getState().peerCount > 0 const items = [ contextMenuAddToIpfsSelection, contextMenuAddToIpfsRawCid, - contextMenuAddToIpfsKeepFilename + contextMenuAddToIpfsKeepFilename, + contextMenuAddToIpfsLink ] for (let item of items) { await browser.contextMenus.update(item, { enabled: canUpload }) diff --git a/add-on/src/lib/ipfs-companion.js b/add-on/src/lib/ipfs-companion.js index 39d132ec0..09be9d842 100644 --- a/add-on/src/lib/ipfs-companion.js +++ b/add-on/src/lib/ipfs-companion.js @@ -59,8 +59,7 @@ module.exports = async function init () { dnslinkResolver = createDnslinkResolver(getState) ipfsPathValidator = createIpfsPathValidator(getState, dnslinkResolver) contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, { - onAddToIpfs: addFromContext, - onAddToIpfsKeepFilename: (info) => addFromContext(info, { wrapWithDirectory: true }), + onAddFromContext, onCopyCanonicalAddress: copier.copyCanonicalAddress, onCopyAddressAtPublicGw: copier.copyAddressAtPublicGw }) @@ -256,20 +255,20 @@ module.exports = async function init () { // Context Menu Uploader // ------------------------------------------------------------------- - async function addFromContext (info, options) { + async function onAddFromContext (context, contextField, options) { let result try { - const srcUrl = await findUrlForContext(info) - if (info.selectionText) { - result = await ipfs.files.add(Buffer.from(info.selectionText), options) + const srcUrl = await findUrlForContext(context, contextField) + if (context.selectionText) { + result = await ipfs.files.add(Buffer.from(context.selectionText), options) } else if (runtime.isFirefox) { // workaround due to https://github.com/ipfs/ipfs-companion/issues/227 const fetchOptions = { cache: 'force-cache', - referrer: info.pageUrl + referrer: context.pageUrl } - // console.log('addFromContext.info', info) - // console.log('addFromContext.fetchOptions', fetchOptions) + // console.log('onAddFromContext.context', context) + // console.log('onAddFromContext.fetchOptions', fetchOptions) const response = await fetch(srcUrl, fetchOptions) const blob = await response.blob() const buffer = await new Promise((resolve, reject) => {