Skip to content

Commit

Permalink
feat(contextMenu): Image Rehosting
Browse files Browse the repository at this point in the history
- adds image, video or audio to IPFS via context menu
- opens pinned resource in a new tab
- closes #59
  • Loading branch information
lidel committed Feb 2, 2017
1 parent 93f8eda commit 3be8b27
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions add-on/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"notifications",
"alarms",
"storage",
"contextMenus",
"webRequest",
"webRequestBlocking"
],
Expand Down
29 changes: 28 additions & 1 deletion add-on/src/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ function handleAlarm (alarm) {
.then(updatePeerCountState)
.then(updateAutomaticModeRedirectState)
.then(updateBrowserActionBadge)
.then(updateContextMenus)
}
}

Expand Down Expand Up @@ -247,11 +248,37 @@ function notify (title, message) {
})
}

// contextMenus
// -------------------------------------------------------------------
const contextMenuUploadToIpfs = 'upload-to-ipfs'

browser.contextMenus.create({
id: contextMenuUploadToIpfs,
title: contextMenuUploadToIpfs, // TODO: i18
contexts: ['image', 'video', 'audio'],
onclick: (info, tab) => {
ipfs.util.addFromURL(info.srcUrl, (err, result) => {
if (err) {
notify('Unable to upload to IPFS API', `${err}`)
return
}
browser.tabs.create({
'url': new URL(state.gwURLString + '/ipfs/' + result[0].hash).toString()
})
})
}
})

function updateContextMenus () {
browser.contextMenus.update(contextMenuUploadToIpfs, {enabled: state.peerCount > 0})
}

// pageAction
// -------------------------------------------------------------------

function onUpdatedTab (tabId, changeInfo, tab) {
if (window.IsIpfs.url(tab.url)) {
const ipfsContext = window.IsIpfs.url(tab.url)
if (ipfsContext) {
browser.pageAction.show(tab.id)
} else {
browser.pageAction.hide(tab.id)
Expand Down
2 changes: 1 addition & 1 deletion add-on/src/popup/page-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function initPageAction () {
return browser.runtime.getBackgroundPage()
.then(bg => {
copyPublicGwAddress.onclick = () => {
const publicGwAddress = currentTab.url.replace(bg.gwURLString, 'https://ipfs.io')
const publicGwAddress = new URL(currentTab.url.replace(bg.state.gwURLString, 'https://ipfs.io')).toString()
copyTextToClipboard(publicGwAddress)
bg.notify('Copied Public URL', publicGwAddress) // TODO: i18
window.close()
Expand Down

0 comments on commit 3be8b27

Please sign in to comment.