From 0afb11348c527acb4c9a70bd09be69b12d2b463a Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Sun, 21 May 2017 20:42:26 +0200 Subject: [PATCH] fix(wx): Origin mismatch under Chromium Turns out workaround introduced for Firefox in #227 broke right-click "Upload to IPFS" function under Chromium ;-) This commit adds userAgent check and enables workaround only under Firefox, making Chromium (#218) happy. --- add-on/src/lib/common.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/add-on/src/lib/common.js b/add-on/src/lib/common.js index 0092fd2e4..fc112e41c 100644 --- a/add-on/src/lib/common.js +++ b/add-on/src/lib/common.js @@ -296,23 +296,30 @@ browser.contextMenus.create({ onclick: addFromURL }) +function inFirefox() { + return !!navigator.userAgent.match('Firefox') +} + 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) + if (inFirefox()) { + // workaround due to https://github.com/lidel/ipfs-firefox-addon/issues/227 + 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()) + } else { + ipfs.util.addFromURL(info.srcUrl, uploadResultHandler) } - reader.readAsArrayBuffer(await response.blob()) } catch (error) { console.error(`Error for ${contextMenuUploadToIpfs}`, error) if (error.message === 'NetworkError when attempting to fetch resource.') {