Skip to content

Commit

Permalink
Merge branch 'master' into feat/add-landing-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
fsdiogo committed Sep 13, 2018
2 parents 63f581e + 812aac2 commit 6839dcf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions add-on/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
"message": "Non-IPFS resource",
"description": "Default label for icon hidden in Page Action menu (pageAction_titleNonIpfs)"
},
"contextMenu_AddToIpfsSelection": {
"message": "Add selected text to IPFS",
"description": "An item in right-click context menu (contextMenu_AddToIpfsSelection)"
},
"contextMenu_AddToIpfsRawCid": {
"message": "Add to IPFS",
"description": "An item in right-click context menu (contextMenu_AddToIpfsRawCid)"
Expand Down
23 changes: 19 additions & 4 deletions add-on/src/lib/context-menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,35 @@ async function findUrlForContext (context) {

module.exports.findUrlForContext = findUrlForContext

const contextMenuAddToIpfsSelection = 'contextMenu_AddToIpfsSelection'
const contextMenuAddToIpfsRawCid = 'contextMenu_AddToIpfsRawCid'
const contextMenuAddToIpfsKeepFilename = 'contextMenu_AddToIpfsKeepFilename'
const contextMenuCopyCanonicalAddress = 'panelCopy_currentIpfsAddress'
const contextMenuCopyAddressAtPublicGw = 'panel_copyCurrentPublicGwUrl'

function createContextMenus (getState, runtime, ipfsPathValidator, { onAddToIpfsRawCid, onAddToIpfsKeepFilename, onCopyCanonicalAddress, onCopyAddressAtPublicGw }) {
function createContextMenus (getState, runtime, ipfsPathValidator, { onAddToIpfs, onAddToIpfsKeepFilename, onCopyCanonicalAddress, onCopyAddressAtPublicGw }) {
let copyAddressContexts = ['page', 'image', 'video', 'audio', 'link']
if (runtime.isFirefox) {
// https://github.com/ipfs-shipyard/ipfs-companion/issues/398
copyAddressContexts.push('page_action')
}
try {
browser.contextMenus.create({
id: contextMenuAddToIpfsSelection,
title: browser.i18n.getMessage(contextMenuAddToIpfsSelection),
contexts: ['selection'],
documentUrlPatterns: ['<all_urls>'],
enabled: false,
onclick: onAddToIpfs
})

browser.contextMenus.create({
id: contextMenuAddToIpfsRawCid,
title: browser.i18n.getMessage(contextMenuAddToIpfsRawCid),
contexts: ['image', 'video', 'audio', 'link'],
documentUrlPatterns: ['<all_urls>'],
enabled: false,
onclick: onAddToIpfsRawCid
onclick: onAddToIpfs
})

browser.contextMenus.create({
Expand Down Expand Up @@ -87,8 +97,13 @@ function createContextMenus (getState, runtime, ipfsPathValidator, { onAddToIpfs
async update (changedTabId) {
try {
const canUpload = getState().peerCount > 0
await browser.contextMenus.update(contextMenuAddToIpfsRawCid, { enabled: canUpload })
await browser.contextMenus.update(contextMenuAddToIpfsKeepFilename, { enabled: canUpload })
const items = [ contextMenuAddToIpfsSelection,
contextMenuAddToIpfsRawCid,
contextMenuAddToIpfsKeepFilename
]
for (let item of items) {
await browser.contextMenus.update(item, { enabled: canUpload })
}
if (changedTabId) {
// recalculate tab-dependant menu items
const currentTab = await browser.tabs.query({ active: true, currentWindow: true }).then(tabs => tabs[0])
Expand Down
18 changes: 10 additions & 8 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ module.exports = async function init () {
dnslinkResolver = createDnslinkResolver(getState)
ipfsPathValidator = createIpfsPathValidator(getState, dnslinkResolver)
contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, {
onAddToIpfsRawCid: addFromURL,
onAddToIpfsKeepFilename: (info) => addFromURL(info, { wrapWithDirectory: true }),
onAddToIpfs: addFromContext,
onAddToIpfsKeepFilename: (info) => addFromContext(info, { wrapWithDirectory: true }),
onCopyCanonicalAddress: () => copier.copyCanonicalAddress(),
onCopyAddressAtPublicGw: () => copier.copyAddressAtPublicGw()
})
Expand Down Expand Up @@ -251,21 +251,23 @@ module.exports = async function init () {
})
}

// URL Uploader
// Context Menu Uploader
// -------------------------------------------------------------------

async function addFromURL (info, options) {
const srcUrl = await findUrlForContext(info)
async function addFromContext (info, options) {
let result
try {
if (runtime.isFirefox) {
const srcUrl = await findUrlForContext(info)
if (info.selectionText) {
result = await ipfs.files.add(Buffer.from(info.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
}
// console.log('addFromURL.info', info)
// console.log('addFromURL.fetchOptions', fetchOptions)
// console.log('addFromContext.info', info)
// console.log('addFromContext.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 6839dcf

Please sign in to comment.