Skip to content

Commit

Permalink
feat(protocol): support web+foo protocols
Browse files Browse the repository at this point in the history
- implements simplified handlers described in #164
  • Loading branch information
lidel committed Mar 18, 2017
1 parent d5435fd commit 20436d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
20 changes: 19 additions & 1 deletion add-on/manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions add-on/src/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
16 changes: 8 additions & 8 deletions add-on/src/popup/page-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down

0 comments on commit 20436d6

Please sign in to comment.