diff --git a/add-on/manifest.json b/add-on/manifest.json index 8234615f7..588330e33 100644 --- a/add-on/manifest.json +++ b/add-on/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "IPFS Gateway Redirect WX", - "version" : "2.0.0", + "version" : "2.0.0alpha1", "description": "Access IPFS resources via custom HTTP2IPFS gateway", "homepage_url": "https://github.com/lidel/ipfs-firefox-addon", @@ -55,6 +55,24 @@ "icons/ipfs-logo-off.svg" ], + "protocol_handlers": [ + { + "protocol": "web+fs", + "name": "IPFS Add-On: *FS protocol handler", + "uriTemplate": "https://ipfs.io/%s" + }, + { + "protocol": "web+ipns", + "name": "IPFS Add-On: IPNS protocol handler", + "uriTemplate": "https://ipfs.io/%s" + }, + { + "protocol": "web+ipfs", + "name": "IPFS Add-On: IPFS protocol handler", + "uriTemplate": "https://ipfs.io/%s" + } + ], + "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'; child-src 'self';", "default_locale": "en" diff --git a/add-on/src/lib/common.js b/add-on/src/lib/common.js index 637513083..ff089e69e 100644 --- a/add-on/src/lib/common.js +++ b/add-on/src/lib/common.js @@ -84,7 +84,22 @@ function redirectToCustomGateway (request) { return { redirectUrl: url.toString() } } +function redirectToNormalizedPath (request) { + const url = new URL(request.url) + let path = decodeURIComponent(url.pathname) + path = path.replace(/^\/web\+fs:[/]*/i, '/') // web+fs://ipfs/Qm → /ipfs/Qm + path = path.replace(/^\/web\+dweb:[/]*/i, '/') // web+dweb://ipfs/Qm → /ipfs/Qm + path = path.replace(/^\/web\+([^:]+):[/]*/i, '/$1/') // web+foo://Qm → /foo/Qm + path = path.replace(/^\/ip([^/]+)\/ip[^/]+\//, '/ip$1/') // /ipfs/ipfs/Qm → /ipfs/Qm + url.pathname = path + return { redirectUrl: url.toString() } +} + function onBeforeRequest (request) { + if (request.url.startsWith('https://ipfs.io/web%2B')) { + // fix path passed via custom protocol + return redirectToNormalizedPath(request) + } if (state.redirect) { // IPFS resources if (publicIpfsResource(request.url)) { diff --git a/add-on/src/popup/page-action.js b/add-on/src/popup/page-action.js index 32ba7d74d..ae9367b84 100644 --- a/add-on/src/popup/page-action.js +++ b/add-on/src/popup/page-action.js @@ -57,12 +57,12 @@ function initPageAction () { unpinResource.onclick = () => { bg.ipfs.pin.rm(currentPath) .then(result => { - console.log(`ipfs.pin.rm result: ${result}`) - bg.notify('Removed IPFS Pin', `${result}`) // TODO: i18 + console.log('ipfs.pin.rm result', result) + bg.notify('Removed IPFS Pin', currentPath) // TODO: i18 }) .catch(error => { - console.error(`Error while unpinning ${currentPath}: ${error}`) - bg.notify('Error while unpinning', `${error}`) // TODO: i18 + console.error(`Error while unpinning ${currentPath}`, error) + bg.notify('Error while unpinning', JSON.stringify(error)) // TODO: i18 }) .then(() => window.close()) } @@ -74,12 +74,12 @@ function initPageAction () { pinResource.onclick = () => { bg.ipfs.pin.add(currentPath) .then(result => { - console.log(`ipfs.pin.add result: ${result}`) - bg.notify('Pinned IPFS Resource', `${result}`) // TODO: i18 + console.log('ipfs.pin.add result', result) + bg.notify('Pinned IPFS Resource', currentPath) // TODO: i18 }) .catch(error => { - console.error(`Error while pinning ${currentPath}: ${error}`) - bg.notify('Error while pinning', `${error}`) // TODO: i18 + console.error(`Error while pinning ${currentPath}`, error) + bg.notify('Error while pinning', JSON.strinfigy(error)) // TODO: i18 }) .then(() => window.close()) }