2018-05-24 18:08:02 +02:00
|
|
|
const extension = require('extensionizer')
|
|
|
|
const resolver = require('./resolver.js')
|
|
|
|
|
|
|
|
module.exports = function (provider) {
|
|
|
|
extension.webRequest.onBeforeRequest.addListener(details => {
|
2018-06-29 07:57:41 +02:00
|
|
|
const urlhttpreplace = details.url.replace(/\w+?:\/\//, "")
|
|
|
|
const url = urlhttpreplace.replace(/[\\\/].*/g, "")
|
|
|
|
let domainhtml = urlhttpreplace.match(/[\\\/].*/g)
|
2018-05-24 18:08:02 +02:00
|
|
|
let clearTime = null
|
2018-06-29 07:57:41 +02:00
|
|
|
let name = url.replace(/\/$/g, "")
|
|
|
|
if (domainhtml === null) domainhtml = [""]
|
2018-05-24 18:08:02 +02:00
|
|
|
extension.tabs.getSelected(null, tab => {
|
|
|
|
extension.tabs.update(tab.id, { url: 'loading.html' })
|
|
|
|
|
|
|
|
clearTime = setTimeout(() => {
|
|
|
|
return extension.tabs.update(tab.id, { url: '404.html' })
|
|
|
|
}, 60000)
|
|
|
|
|
|
|
|
resolver.resolve(name, provider).then(ipfsHash => {
|
|
|
|
clearTimeout(clearTime)
|
2018-06-29 07:57:41 +02:00
|
|
|
let url = 'https://ipfs.infura.io/ipfs/' + ipfsHash + domainhtml[0]
|
2018-05-24 18:08:02 +02:00
|
|
|
return fetch(url, { method: 'HEAD' }).then(response => response.status).then(statusCode => {
|
2018-05-29 13:29:12 +02:00
|
|
|
if (statusCode !== 200) return extension.tabs.update(tab.id, { url: '404.html' })
|
2018-05-24 18:08:02 +02:00
|
|
|
extension.tabs.update(tab.id, { url: url })
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2018-06-29 07:57:41 +02:00
|
|
|
url = 'https://ipfs.infura.io/ipfs/' + ipfsHash + domainhtml[0]
|
2018-05-24 18:08:02 +02:00
|
|
|
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-06-29 07:57:41 +02:00
|
|
|
}, {urls: ['*://*.eth/', '*://*.eth/*']})
|
2018-05-24 18:08:02 +02:00
|
|
|
}
|