Skip to content

Commit

Permalink
feat: separate context action for links
Browse files Browse the repository at this point in the history
Closes #579
  • Loading branch information
lidel committed Oct 1, 2018
1 parent 329199b commit f517a3f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
10 changes: 7 additions & 3 deletions add-on/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
36 changes: 25 additions & 11 deletions add-on/src/lib/context-menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -43,25 +47,34 @@ function createContextMenus (getState, runtime, ipfsPathValidator, { onAddToIpfs
contexts: ['selection'],
documentUrlPatterns: ['<all_urls>'],
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: ['<all_urls>'],
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: ['<all_urls>'],
enabled: false,
onclick: (context) => onAddFromContext(context, 'srcUrl', { wrapWithDirectory: true })
})

browser.contextMenus.create({
id: contextMenuAddToIpfsLink,
title: browser.i18n.getMessage(contextMenuAddToIpfsLink),
contexts: ['link'],
documentUrlPatterns: ['<all_urls>'],
enabled: false,
onclick: onAddToIpfsKeepFilename
onclick: (context) => onAddFromContext(context, 'linkUrl', { wrapWithDirectory: true })
})

browser.contextMenus.create({
Expand Down Expand Up @@ -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 })
Expand Down
17 changes: 8 additions & 9 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit f517a3f

Please sign in to comment.