1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 10:30:04 +01:00
metamask-extension/app/scripts/lib/ipfsContent.js

41 lines
1.8 KiB
JavaScript
Raw Normal View History

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-07-03 20:29:36 +02:00
const urlhttpreplace = details.url.replace(/\w+?:\/\//, '')
const url = urlhttpreplace.replace(/[\\/].*/g, '') // eslint-disable-line no-useless-escape
let domainhtml = urlhttpreplace.match(/[\\/].*/g) // eslint-disable-line no-useless-escape
2018-05-24 18:08:02 +02:00
let clearTime = null
2018-07-03 20:29:36 +02:00
const 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)
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 => {
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 }
}, {urls: ['*://*.eth/', '*://*.eth/*']})
2018-05-24 18:08:02 +02:00
}