Skip to content

Commit

Permalink
fix(contextMenu): display help when upload fails due to Tracking Prot…
Browse files Browse the repository at this point in the history
…ection

This is a workaround until #227 gets a proper fix.
  • Loading branch information
lidel committed Mar 29, 2017
1 parent 2f8b6d7 commit f44f564
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions add-on/src/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,40 @@ browser.contextMenus.create({
id: contextMenuUploadToIpfs,
title: browser.i18n.getMessage(contextMenuUploadToIpfs),
contexts: ['image', 'video', 'audio'],
onclick: (info, tab) => {
ipfs.util.addFromURL(info.srcUrl, uploadResultHandler)
}
onclick: addFromURL
})

async function addFromURL (info) {
// disabled due to https://github.com/lidel/ipfs-firefox-addon/issues/227
// ipfs.util.addFromURL(info.srcUrl, uploadResultHandler)
try {
const fetchOptions = {
cache: 'force-cache',
referrer: info.pageUrl
}
//console.log('addFromURL.info', info)
//console.log('addFromURL.fetchOptions', fetchOptions)
const response = await fetch(info.srcUrl, fetchOptions)
const reader = new FileReader()
reader.onloadend = () => {
const buffer = ipfs.Buffer.from(reader.result)
ipfs.add(buffer, uploadResultHandler)
}
reader.readAsArrayBuffer(await response.blob())
} catch (error) {
console.error(`Error for ${contextMenuUploadToIpfs}`, error)
if (error.message === 'NetworkError when attempting to fetch resource.') {
notify('Unable to upload to IPFS', 'Try disabling Tracking Protection (press ctrl+shift+j for more details)')
console.warn('IPFS upload often fails because remote file can not be downloaded due to Tracking Protection. See details at: https://github.com/lidel/ipfs-firefox-addon/issues/227')
browser.tabs.create({
'url': 'https://github.com/lidel/ipfs-firefox-addon/issues/227'
})
} else {
notify('Unable to upload to IPFS', `${error.message} (press ctrl+shift+j for more details)`)
}
}
}

function uploadResultHandler (err, result) {
if (err || !result) {
console.error('ipfs add error', err, result)
Expand Down

0 comments on commit f44f564

Please sign in to comment.