2018-05-24 18:08:02 +02:00
|
|
|
const extension = require('extensionizer')
|
|
|
|
const resolver = require('./resolver.js')
|
|
|
|
|
|
|
|
module.exports = function (provider) {
|
2018-07-15 20:37:16 +02:00
|
|
|
function ipfsContent (details) {
|
2018-07-15 20:34:48 +02:00
|
|
|
const name = details.url.substring(7, details.url.length - 1)
|
|
|
|
let clearTime = null
|
2018-08-09 12:14:09 +02:00
|
|
|
extension.tabs.query({active: true}, tab => {
|
2018-07-15 20:34:48 +02:00
|
|
|
extension.tabs.update(tab.id, { url: 'loading.html' })
|
2018-05-24 18:08:02 +02:00
|
|
|
|
2018-07-15 20:34:48 +02:00
|
|
|
clearTime = setTimeout(() => {
|
|
|
|
return extension.tabs.update(tab.id, { url: '404.html' })
|
|
|
|
}, 60000)
|
2018-05-24 18:08:02 +02:00
|
|
|
|
2018-07-15 20:34:48 +02:00
|
|
|
resolver.resolve(name, provider).then(ipfsHash => {
|
|
|
|
clearTimeout(clearTime)
|
|
|
|
let url = 'https://ipfs.infura.io/ipfs/' + ipfsHash
|
|
|
|
return fetch(url, { method: 'HEAD' }).then(response => response.status).then(statusCode => {
|
|
|
|
if (statusCode !== 200) return extension.tabs.update(tab.id, { url: '404.html' })
|
|
|
|
extension.tabs.update(tab.id, { url: url })
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
url = 'https://ipfs.infura.io/ipfs/' + ipfsHash
|
|
|
|
extension.tabs.update(tab.id, {url: url})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
clearTimeout(clearTime)
|
|
|
|
const url = err === 'unsupport' ? 'unsupport' : 'error'
|
|
|
|
extension.tabs.update(tab.id, {url: `${url}.html?name=${name}`})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return { cancel: true }
|
|
|
|
}
|
|
|
|
|
2018-08-09 12:20:51 +02:00
|
|
|
extension.webRequest.onErrorOccurred.addListener(ipfsContent, {urls: ['*://*.eth/', '*://*.test/']})
|
2018-07-15 20:34:48 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
remove () {
|
2018-08-09 12:20:51 +02:00
|
|
|
extension.webRequest.onErrorOccurred.removeListener(ipfsContent)
|
2018-07-15 20:34:48 +02:00
|
|
|
},
|
|
|
|
}
|
2018-05-24 18:08:02 +02:00
|
|
|
}
|