diff --git a/add-on/manifest.json b/add-on/manifest.json index a1016e64d..8234615f7 100644 --- a/add-on/manifest.json +++ b/add-on/manifest.json @@ -23,6 +23,7 @@ "notifications", "alarms", "storage", + "contextMenus", "webRequest", "webRequestBlocking" ], diff --git a/add-on/src/lib/common.js b/add-on/src/lib/common.js index ad24aeba6..65354a24d 100644 --- a/add-on/src/lib/common.js +++ b/add-on/src/lib/common.js @@ -203,6 +203,7 @@ function handleAlarm (alarm) { .then(updatePeerCountState) .then(updateAutomaticModeRedirectState) .then(updateBrowserActionBadge) + .then(updateContextMenus) } } @@ -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) diff --git a/add-on/src/popup/page-action.js b/add-on/src/popup/page-action.js index ae04f42fa..0a92eac71 100644 --- a/add-on/src/popup/page-action.js +++ b/add-on/src/popup/page-action.js @@ -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()