Skip to content

Commit

Permalink
feat(contextMenus): support image, video, audio, link
Browse files Browse the repository at this point in the history
When right-clicked on image or link, copied address will be
of that element instead of entire page.

This closes #294
  • Loading branch information
lidel committed Oct 16, 2017
1 parent abab537 commit 12c5526
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
97 changes: 48 additions & 49 deletions add-on/src/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ function notify (titleKey, messageKey, messageParam) {
const contextMenuUploadToIpfs = 'contextMenu_UploadToIpfs'
const contextMenuCopyIpfsAddress = 'panelCopy_currentIpfsAddress'
const contextMenuCopyPublicGwUrl = 'panel_copyCurrentPublicGwUrl'
// TODO const contextMenuPinUnpinAddress = 'panel_pinCurrentIpfsAddress'

browser.contextMenus.create({
id: contextMenuUploadToIpfs,
Expand All @@ -399,6 +398,20 @@ browser.contextMenus.create({
enabled: false,
onclick: addFromURL
})
browser.contextMenus.create({
id: contextMenuCopyIpfsAddress,
title: browser.i18n.getMessage(contextMenuCopyIpfsAddress),
contexts: ['page', 'image', 'video', 'audio', 'link'],
documentUrlPatterns: ['*://*/ipfs/*', '*://*/ipns/*'],
onclick: copyCanonicalAddress
})
browser.contextMenus.create({
id: contextMenuCopyPublicGwUrl,
title: browser.i18n.getMessage(contextMenuCopyPublicGwUrl),
contexts: ['page', 'image', 'video', 'audio', 'link'],
documentUrlPatterns: ['*://*/ipfs/*', '*://*/ipns/*'],
onclick: copyAddressAtPublicGw
})

function inFirefox () {
return !!navigator.userAgent.match('Firefox')
Expand Down Expand Up @@ -454,21 +467,44 @@ function uploadResultHandler (err, result) {
})
}

async function copyCanonicalAddressOfCurrentTab () {
async function findUrlForContext (context) {
if (context) {
console.log(context)
if (context.linkUrl) {
// present when clicked on a link
return context.linkUrl
}
if (context.srcUrl) {
// present when clicked on page element such as image or video
return context.srcUrl
}
if (context.pageUrl) {
// pageUrl is the root frame
return context.pageUrl
}
}
// falback to the url of current tab
const currentTab = await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0])
const rawIpfsAddress = currentTab.url.replace(/^.+(\/ip(f|n)s\/.+)/, '$1')
copyTextToClipboard(rawIpfsAddress, currentTab.id)
return currentTab.url
}

async function copyCanonicalAddress (context) {
const url = await findUrlForContext(context)
const rawIpfsAddress = url.replace(/^.+(\/ip(f|n)s\/.+)/, '$1')
copyTextToClipboard(rawIpfsAddress)
notify('notify_copiedCanonicalAddressTitle', rawIpfsAddress)
}

async function copyPublicGwAddressOfCurrentTab () {
const currentTab = await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0])
const urlAtPubGw = currentTab.url.replace(state.gwURLString, state.pubGwURLString)
copyTextToClipboard(urlAtPubGw, currentTab.id)
async function copyAddressAtPublicGw (context) {
const url = await findUrlForContext(context)
const urlAtPubGw = url.replace(state.gwURLString, state.pubGwURLString)
copyTextToClipboard(urlAtPubGw)
notify('notify_copiedPublicURLTitle', urlAtPubGw)
}

async function copyTextToClipboard (copyText, tabId) {
async function copyTextToClipboard (copyText) {
const currentTab = await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0])
const tabId = currentTab.id
// Lets take a moment and ponder on the state of copying a string in 2017:
const copyToClipboardIn2017 = `function copyToClipboardIn2017(text) {
function oncopy(event) {
Expand Down Expand Up @@ -502,46 +538,9 @@ async function updateContextMenus (changedTabId) {
// recalculate tab-dependant menu items
const currentTab = await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0])
if (currentTab.id === changedTabId) {
try {
// we can't hide items due to https://bugzilla.mozilla.org/show_bug.cgi?id=1397249
// so we remove items for previous tab and then create new ones if needed
await browser.contextMenus.remove(contextMenuCopyIpfsAddress)
await browser.contextMenus.remove(contextMenuCopyPublicGwUrl)
// await browser.contextMenus.remove(contextMenuPinUnpinAddress)
} catch (e) {
// ignore
// console.error(e)
}
if (window.IsIpfs.url(currentTab.url)) {
// console.log('enabling ipfs context menus', currentTab)
try {
await browser.contextMenus.create({
id: contextMenuCopyIpfsAddress,
title: browser.i18n.getMessage(contextMenuCopyIpfsAddress),
contexts: ['page'],
onclick: copyCanonicalAddressOfCurrentTab
})
await browser.contextMenus.create({
id: contextMenuCopyPublicGwUrl,
title: browser.i18n.getMessage(contextMenuCopyPublicGwUrl),
contexts: ['page'],
onclick: copyPublicGwAddressOfCurrentTab
})
/*
await browser.contextMenus.create({
id: contextMenuPinUnpinAddress,
title: browser.i18n.getMessage(contextMenuPinUnpinAddress),
contexts: ['page'],
onclick: TODO // TODO
})
*/
} catch (e) {
// ignore
// console.error(e)
}
} else {
// console.log('no ipfs context menus', currentTab)
}
const ipfsContext = isIpfsPageActionsContext(currentTab.url)
browser.contextMenus.update(contextMenuCopyIpfsAddress, {enabled: ipfsContext})
browser.contextMenus.update(contextMenuCopyPublicGwUrl, {enabled: ipfsContext})
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions add-on/src/popup/browser-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ function notify (title, message) {

async function copyCurrentPublicGwAddress () {
const bg = await getBackgroundPage()
await bg.copyPublicGwAddressOfCurrentTab()
await bg.copyAddressAtPublicGw()
window.close()
}

async function copyCurrentCanonicalAddress () {
const bg = await getBackgroundPage()
await bg.copyCanonicalAddressOfCurrentTab()
await bg.copyCanonicalAddress()
window.close()
}

Expand Down

0 comments on commit 12c5526

Please sign in to comment.